mirror of
https://github.com/elyby/oauth2-server.git
synced 2024-12-27 23:40:22 +05:30
Updated examples
This commit is contained in:
parent
79791e5848
commit
70e9d7b699
2
.gitignore
vendored
2
.gitignore
vendored
@ -4,3 +4,5 @@ phpunit.xml
|
||||
.idea
|
||||
/examples/vendor
|
||||
/tests/_output
|
||||
examples/public.key
|
||||
examples/private.key
|
||||
|
@ -1,3 +1,54 @@
|
||||
# Example implementations
|
||||
|
||||
Just run `composer install --no-dev` in this directory to get started.
|
||||
## Installation
|
||||
|
||||
0. Run `composer install --no-dev` in this directory to install dependencies
|
||||
0. Create a private key `openssl genrsa -out private.key 1024`
|
||||
0. Create a public key `openssl rsa -in private.key -pubout > public.key`
|
||||
0. `cd` into the public directory
|
||||
0. Start a PHP server `php -S localhost:4444`
|
||||
|
||||
## Testing the client credentials grant example
|
||||
|
||||
Send the following cURL request:
|
||||
|
||||
```
|
||||
curl -X "POST" "http://localhost:4444/client_credentials.php/access_token" \
|
||||
-H "Content-Type: application/x-www-form-urlencoded" \
|
||||
-H "Accept: 1.0" \
|
||||
--data-urlencode "client_id=myawesomeapp" \
|
||||
--data-urlencode "scope=basic email" \
|
||||
--data-urlencode "client_secret=abc123" \
|
||||
--data-urlencode "grant_type=client_credentials"
|
||||
```
|
||||
|
||||
## Testing the password grant example
|
||||
|
||||
Send the following cURL request:
|
||||
|
||||
```
|
||||
curl -X "POST" "http://localhost:4444/password.php/access_token" \
|
||||
-H "Content-Type: application/x-www-form-urlencoded" \
|
||||
-H "Accept: 1.0" \
|
||||
--data-urlencode "client_id=myawesomeapp" \
|
||||
--data-urlencode "scope=basic email" \
|
||||
--data-urlencode "username=alex" \
|
||||
--data-urlencode "password=whisky" \
|
||||
--data-urlencode "client_secret=abc123" \
|
||||
--data-urlencode "grant_type=password"
|
||||
```
|
||||
|
||||
## Testing the refresh token grant example
|
||||
|
||||
Send the following cURL request. Replace `{{REFRESH_TOKEN}}` with a refresh token from another grant above:
|
||||
|
||||
```
|
||||
curl -X "POST" "http://localhost:4444/refresh_token.php/access_token" \
|
||||
-H "Content-Type: application/x-www-form-urlencoded" \
|
||||
-H "Accept: 1.0" \
|
||||
--data-urlencode "grant_type=refresh_token" \
|
||||
--data-urlencode "client_id=myawesomeapp" \
|
||||
--data-urlencode "client_secret=abc123" \
|
||||
--data-urlencode "refresh_token={{REFRESH_TOKEN}}"
|
||||
```
|
||||
|
||||
|
56
examples/public/refresh_token.php
Normal file
56
examples/public/refresh_token.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
use League\OAuth2\Server\Exception\OAuthServerException;
|
||||
use League\OAuth2\Server\Grant\RefreshTokenGrant;
|
||||
use League\OAuth2\Server\Server;
|
||||
|
||||
use OAuth2ServerExamples\Repositories\AccessTokenRepository;
|
||||
use OAuth2ServerExamples\Repositories\ClientRepository;
|
||||
use OAuth2ServerExamples\Repositories\RefreshTokenRepository;
|
||||
use OAuth2ServerExamples\Repositories\ScopeRepository;
|
||||
use OAuth2ServerExamples\Repositories\UserRepository;
|
||||
|
||||
use Slim\App;
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
|
||||
include(__DIR__ . '/../vendor/autoload.php');
|
||||
|
||||
// Setup the authorization server
|
||||
$server = new Server('file://' . __DIR__ . '/../private.key');
|
||||
|
||||
// Init our repositories
|
||||
$userRepository = new UserRepository();
|
||||
$clientRepository = new ClientRepository();
|
||||
$scopeRepository = new ScopeRepository();
|
||||
$accessTokenRepository = new AccessTokenRepository();
|
||||
$refreshTokenRepository = new RefreshTokenRepository();
|
||||
|
||||
// Enable the client credentials grant on the server
|
||||
$refreshTokenGrant = new RefreshTokenGrant(
|
||||
'file://' . __DIR__ . '/../public.key',
|
||||
$clientRepository,
|
||||
$scopeRepository,
|
||||
$accessTokenRepository,
|
||||
$refreshTokenRepository
|
||||
);
|
||||
$server->enableGrantType($refreshTokenGrant);
|
||||
|
||||
// App
|
||||
$app = new App([Server::class => $server]);
|
||||
|
||||
$app->post('/access_token', function (Request $request, Response $response) {
|
||||
/** @var Server $server */
|
||||
$server = $this->get(Server::class);
|
||||
try {
|
||||
return $server->respondToRequest($request);
|
||||
} catch (OAuthServerException $e) {
|
||||
return $e->generateHttpResponse();
|
||||
} catch (\Exception $e) {
|
||||
return $response->withStatus(500)->write(
|
||||
sprintf('<h1>%s</h1><p>%s</p>', get_class($e), $e->getMessage())
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
$app->run();
|
@ -2,51 +2,39 @@
|
||||
namespace OAuth2ServerExamples\Repositories;
|
||||
|
||||
use League\OAuth2\Server\Entities\Interfaces\AccessTokenEntityInterface;
|
||||
use League\OAuth2\Server\Entities\Interfaces\ScopeEntityInterface;
|
||||
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
|
||||
|
||||
class AccessTokenRepository implements AccessTokenRepositoryInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getAccessTokenEntityByTokenString($tokenIdentifier)
|
||||
{
|
||||
// TODO: Implement get() method.
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getScopeEntitiesAssociatedWithAccessToken(AccessTokenEntityInterface $token)
|
||||
{
|
||||
// TODO: Implement getScopes() method.
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
* Persists a new access token to permanent storage
|
||||
*
|
||||
* @param \League\OAuth2\Server\Entities\Interfaces\AccessTokenEntityInterface $accessTokenEntity
|
||||
*/
|
||||
public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity)
|
||||
{
|
||||
// TODO: Implement create() method.
|
||||
// TODO: Implement persistNewAccessToken() method.
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
* Revoke an access token
|
||||
*
|
||||
* @param string $tokenId
|
||||
*/
|
||||
public function associateScopeWithAccessToken(
|
||||
AccessTokenEntityInterface $accessTokenEntityInterface,
|
||||
ScopeEntityInterface $scope
|
||||
) {
|
||||
// TODO: Implement associateScope() method.
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function deleteAccessToken(AccessTokenEntityInterface $accessToken)
|
||||
public function revokeAccessToken($tokenId)
|
||||
{
|
||||
// TODO: Implement delete() method.
|
||||
// TODO: Implement revokeAccessToken() method.
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the access token has been revoked
|
||||
*
|
||||
* @param string $tokenId
|
||||
*
|
||||
* @return bool Return true if this token has been revoked
|
||||
*/
|
||||
public function isAccessTokenRevoked($tokenId)
|
||||
{
|
||||
// TODO: Implement isAccessTokenRevoked() method.
|
||||
}
|
||||
}
|
||||
|
42
examples/src/Repositories/RefreshTokenRepository.php
Normal file
42
examples/src/Repositories/RefreshTokenRepository.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace OAuth2ServerExamples\Repositories;
|
||||
|
||||
use League\OAuth2\Server\Entities\Interfaces\RefreshTokenEntityInterface;
|
||||
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
|
||||
|
||||
class RefreshTokenRepository implements RefreshTokenRepositoryInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* Create a new refresh token_name
|
||||
*
|
||||
* @param \League\OAuth2\Server\Entities\Interfaces\RefreshTokenEntityInterface $refreshTokenEntityInterface
|
||||
*/
|
||||
public function persistNewRefreshToken(RefreshTokenEntityInterface $refreshTokenEntityInterface)
|
||||
{
|
||||
// TODO: Implement persistNewRefreshToken() method.
|
||||
}
|
||||
|
||||
/**
|
||||
* Revoke the refresh token
|
||||
*
|
||||
* @param string $tokenId
|
||||
*/
|
||||
public function revokeRefreshToken($tokenId)
|
||||
{
|
||||
// TODO: Implement revokeRefreshToken() method.
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the refresh token has been revoked
|
||||
*
|
||||
* @param string $tokenId
|
||||
*
|
||||
* @return bool Return true if this token has been revoked
|
||||
*/
|
||||
public function isRefreshTokenRevoked($tokenId)
|
||||
{
|
||||
// TODO: Implement isRefreshTokenRevoked() method.
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user