From 4ebf3f838f9647f8d748c4ce3020931ad26e6b78 Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Fri, 23 May 2014 16:25:09 +0100 Subject: [PATCH] Added initial examples --- .../relational/Storage/AuthCodeStorage.php | 28 ++++++- .../Storage/RefreshTokenStorage.php | 4 +- examples/relational/auth.php | 79 +++++++++++++++++++ 3 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 examples/relational/auth.php diff --git a/examples/relational/Storage/AuthCodeStorage.php b/examples/relational/Storage/AuthCodeStorage.php index d4d76304..c5dbe41f 100644 --- a/examples/relational/Storage/AuthCodeStorage.php +++ b/examples/relational/Storage/AuthCodeStorage.php @@ -4,13 +4,39 @@ namespace RelationalExample\Storage; use League\OAuth2\Server\Storage\AuthCodeInterface; use League\OAuth2\Server\Storage\Adapter; +use League\OAuth2\Server\Entity\AuthCodeEntity; +use League\OAuth2\Server\Entity\ScopeEntity; class AuthCodeStorage extends Adapter implements AuthCodeInterface { /** * {@inheritdoc} */ - public function get($token) + public function get($code) + { + die(var_dump(__METHOD__, func_get_args())); + } + + /** + * {@inheritdoc} + */ + public function getScopes(AuthCodeEntity $token) + { + die(var_dump(__METHOD__, func_get_args())); + } + + /** + * {@inheritdoc} + */ + public function associateScope(AuthCodeEntity $token, ScopeEntity $scope) + { + die(var_dump(__METHOD__, func_get_args())); + } + + /** + * {@inheritdoc} + */ + public function delete(AuthCodeEntity $token) { die(var_dump(__METHOD__, func_get_args())); } diff --git a/examples/relational/Storage/RefreshTokenStorage.php b/examples/relational/Storage/RefreshTokenStorage.php index 27b81dc7..4a32f92d 100644 --- a/examples/relational/Storage/RefreshTokenStorage.php +++ b/examples/relational/Storage/RefreshTokenStorage.php @@ -4,6 +4,7 @@ namespace RelationalExample\Storage; use League\OAuth2\Server\Storage\RefreshTokenInterface; use League\OAuth2\Server\Storage\Adapter; +use League\OAuth2\Server\Entity\RefreshTokenEntity; class RefreshTokenStorage extends Adapter implements RefreshTokenInterface { @@ -26,8 +27,9 @@ class RefreshTokenStorage extends Adapter implements RefreshTokenInterface /** * {@inheritdoc} */ - public function delete($token) + public function delete(RefreshTokenEntity $token) { die(var_dump(__METHOD__, func_get_args())); } + } diff --git a/examples/relational/auth.php b/examples/relational/auth.php new file mode 100644 index 00000000..d99b38e9 --- /dev/null +++ b/examples/relational/auth.php @@ -0,0 +1,79 @@ +createFromGlobals(); +$router = new \Orno\Route\RouteCollection; +$router->setStrategy(\Orno\Route\RouteStrategyInterface::RESTFUL_STRATEGY); + +// Set up the OAuth 2.0 resource server +$sessionStorage = new Storage\SessionStorage(); +$accessTokenStorage = new Storage\AccessTokenStorage(); +$clientStorage = new Storage\ClientStorage(); +$scopeStorage = new Storage\ScopeStorage(); +$accessTokenStorage = new Storage\AccessTokenStorage(); +$refreshTokenStorage = new Storage\RefreshTokenStorage(); +$authCodeStorage = new Storage\AuthCodeStorage(); + +$server = new AuthorizationServer(); +$server->setSessionStorage($sessionStorage); +$server->setAccessTokenStorage($accessTokenStorage); +$server->setRefreshTokenStorage($refreshTokenStorage); +$server->setClientStorage($clientStorage); +$server->setScopeStorage($scopeStorage); +$server->setAuthCodeStorage($authCodeStorage); + +$authCodeGrant = new Grant\AuthCodeGrant(); +$server->addGrantType($authCodeGrant); + +$server->setRequest($request); + +// GET /authorize +$router->get('/authorize', function (Request $request) use ($server) { + + // First ensure the parameters in the query string are correct + + try { + $authParams = $server->getGrantType('authorization_code')->checkAuthorizeParams(); + } catch (\Exception $e) { + echo json_encode([ + 'error' => $e->errorType, + 'message' => $e->getMessage() + ]); + + exit; + } + + // Normally at this point you would show the user a sign-in screen and ask them to authorize the requested scopes + + // ... + + // Create a new authorize request which will respond with a redirect URI that the user will be redirected to + + $redirectUri = $server->newAuthorizeRequest('user', 1, $authParams); + + $response = new Response('', 200, [ + 'Location' => $redirectUri + ]); + + return $response; +}); + +$dispatcher = $router->getDispatcher(); +$response = $dispatcher->dispatch($request->getMethod(), $request->getPathInfo()); +$response->send(); + +// var_dump(Capsule::getQueryLog());