diff --git a/.gitignore b/.gitignore index 1bfc2315..3b5992e5 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ phpunit.xml .idea /examples/vendor /tests/_output +examples/public.key +examples/private.key diff --git a/examples/README.md b/examples/README.md index 884da28b..e4a30a73 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,3 +1,54 @@ # Example implementations -Just run `composer install --no-dev` in this directory to get started. \ No newline at end of file +## 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}}" +``` + diff --git a/examples/public/refresh_token.php b/examples/public/refresh_token.php new file mode 100644 index 00000000..75c08139 --- /dev/null +++ b/examples/public/refresh_token.php @@ -0,0 +1,56 @@ +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('

%s

%s

', get_class($e), $e->getMessage()) + ); + } +}); + +$app->run(); diff --git a/examples/src/Repositories/AccessTokenRepository.php b/examples/src/Repositories/AccessTokenRepository.php index 16d8dc2b..bc8ada68 100644 --- a/examples/src/Repositories/AccessTokenRepository.php +++ b/examples/src/Repositories/AccessTokenRepository.php @@ -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. } } diff --git a/examples/src/Repositories/RefreshTokenRepository.php b/examples/src/Repositories/RefreshTokenRepository.php new file mode 100644 index 00000000..a7a4e079 --- /dev/null +++ b/examples/src/Repositories/RefreshTokenRepository.php @@ -0,0 +1,42 @@ +