More updates to relational example

This commit is contained in:
Alex Bilbie 2014-06-23 08:20:34 +01:00
parent 92639fbbd6
commit 33c68a2103
8 changed files with 149 additions and 34 deletions

View File

@ -7,6 +7,8 @@ use League\OAuth2\Server\Storage\Adapter;
use League\OAuth2\Server\Entity\AuthCodeEntity; use League\OAuth2\Server\Entity\AuthCodeEntity;
use League\OAuth2\Server\Entity\ScopeEntity; use League\OAuth2\Server\Entity\ScopeEntity;
use Illuminate\Database\Capsule\Manager as Capsule;
class AuthCodeStorage extends Adapter implements AuthCodeInterface class AuthCodeStorage extends Adapter implements AuthCodeInterface
{ {
/** /**
@ -14,7 +16,29 @@ class AuthCodeStorage extends Adapter implements AuthCodeInterface
*/ */
public function get($code) public function get($code)
{ {
die(var_dump(__METHOD__, func_get_args())); $result = Capsule::table('oauth_auth_codes')
->where('auth_code', $code)
->where('expire_time', '>=', time())
->get();
if (count($result) === 1) {
$token = new AuthCodeEntity($this->server);
$token->setToken($result[0]['auth_code']);
return $token;
}
return null;
}
public function create($token, $$expireTime, $sessionId)
{
Capsule::table('oauth_auth_codes')
->insert([
'auth_code' => $token,
'client_redirect_uri' => $redirectUri,
'session_id' => $sessionId,
'expire_time' => $expireTime
]);
} }
/** /**
@ -30,7 +54,11 @@ class AuthCodeStorage extends Adapter implements AuthCodeInterface
*/ */
public function associateScope(AuthCodeEntity $token, ScopeEntity $scope) public function associateScope(AuthCodeEntity $token, ScopeEntity $scope)
{ {
die(var_dump(__METHOD__, func_get_args())); Capsule::table('oauth_auth_code_scopes')
->insert([
'auth_code' => $token->getToken(),
'scope' => $scope->getId()
]);
} }
/** /**

View File

@ -16,7 +16,31 @@ class ClientStorage extends Adapter implements ClientInterface
*/ */
public function get($clientId, $clientSecret = null, $redirectUri = null, $grantType = null) public function get($clientId, $clientSecret = null, $redirectUri = null, $grantType = null)
{ {
die(var_dump(__METHOD__, func_get_args())); $query = Capsule::table('oauth_clients')
->select('oauth_clients.*')
->where('oauth_clients.id', $clientId);
if ($clientSecret !== null) {
$query->where('oauth_clients.secret', $clientSecret);
}
if ($redirectUri) {
$query->join('oauth_client_redirect_uris', 'oauth_clients.id', '=', 'oauth_client_redirect_uris.client_id')
->select(['oauth_clients.*', 'oauth_client_redirect_uris.*'])
->where('oauth_client_redirect_uris.redirect_uri', $redirectUri);
}
$result = $query->get();
if (count($result) === 1) {
$client = new ClientEntity($this->server);
$client->setId($result[0]['id']);
$client->setName($result[0]['name']);
return $client;
}
return null;
} }
/** /**
@ -37,5 +61,7 @@ class ClientStorage extends Adapter implements ClientInterface
return $client; return $client;
} }
return null;
} }
} }

View File

@ -4,6 +4,9 @@ namespace RelationalExample\Storage;
use League\OAuth2\Server\Storage\ScopeInterface; use League\OAuth2\Server\Storage\ScopeInterface;
use League\OAuth2\Server\Storage\Adapter; use League\OAuth2\Server\Storage\Adapter;
use League\OAuth2\Server\Entity\ScopeEntity;
use Illuminate\Database\Capsule\Manager as Capsule;
class ScopeStorage extends Adapter implements ScopeInterface class ScopeStorage extends Adapter implements ScopeInterface
{ {
@ -12,6 +15,16 @@ class ScopeStorage extends Adapter implements ScopeInterface
*/ */
public function get($scope, $grantType = null) public function get($scope, $grantType = null)
{ {
die(var_dump(__METHOD__, func_get_args())); $result = Capsule::table('oauth_scopes')
->where('id', $scope)
->get();
if (count($result) === 0) {
return null;
}
return (new ScopeEntity($this->server))
->setId($result[0]['id'])
->setDescription($result[0]['description']);
} }
} }

View File

@ -57,7 +57,22 @@ class SessionStorage extends Adapter implements SessionInterface
*/ */
public function getScopes(SessionEntity $session) public function getScopes(SessionEntity $session)
{ {
die(var_dump(__CLASS__.'::'.__METHOD__, func_get_args())); $result = Capsule::table('oauth_sessions')
->select('oauth_scopes.*')
->join('oauth_session_scopes', 'oauth_sessions.id', '=', 'oauth_session_scopes.session_id')
->join('oauth_scopes', 'oauth_scopes.id', '=', 'oauth_session_scopes.scope')
->where('oauth_sessions.id', $session->getId())
->get();
$scopes = [];
foreach ($result as $scope) {
$scopes[] = (new ScopeEntity($this->server))
->setId($scope['id'])
->setDescription($scope['description']);
}
return $scopes;
} }
/** /**
@ -65,7 +80,14 @@ class SessionStorage extends Adapter implements SessionInterface
*/ */
public function create($ownerType, $ownerId, $clientId, $clientRedirectUri = null) public function create($ownerType, $ownerId, $clientId, $clientRedirectUri = null)
{ {
die(var_dump(__CLASS__.'::'.__METHOD__, func_get_args())); $id = Capsule::table('oauth_sessions')
->insert([
'owner_type' => $ownerType,
'owner_id' => $ownerId,
'client_id' => $clientId
]);
return $id;
} }
/** /**

View File

@ -5,40 +5,32 @@ namespace OAuth2Server\RelationalExample;
use \Orno\Http\Request; use \Orno\Http\Request;
use \Orno\Http\Response; use \Orno\Http\Response;
use \Orno\Http\JsonResponse; use \Orno\Http\JsonResponse;
use \League\OAuth2\Server\AuthorizationServer; use \Orno\Http\Exception\MethodNotAllowedException;
use \League\OAuth2\Server\Exception;
use \League\OAuth2\Server\Grant; use Illuminate\Database\Capsule\Manager as Capsule;
// use \League\OAuth2\Server\Exception;
use \RelationalExample\Storage; use \RelationalExample\Storage;
use \RelationalExample\Model; use \RelationalExample\Model;
use Illuminate\Database\Capsule\Manager as Capsule;
include __DIR__.'/vendor/autoload.php'; include __DIR__.'/vendor/autoload.php';
// Routing setup // Routing setup
$request = (new Request)->createFromGlobals();
$router = new \Orno\Route\RouteCollection; $router = new \Orno\Route\RouteCollection;
$router->setStrategy(\Orno\Route\RouteStrategyInterface::RESTFUL_STRATEGY);
// Set up the OAuth 2.0 resource server // Set up the OAuth 2.0 resource server
$sessionStorage = new Storage\SessionStorage(); $server = new \League\OAuth2\Server\AuthorizationServer;
$accessTokenStorage = new Storage\AccessTokenStorage(); $server->setSessionStorage(new Storage\SessionStorage);
$clientStorage = new Storage\ClientStorage(); $server->setAccessTokenStorage(new Storage\AccessTokenStorage);
$scopeStorage = new Storage\ScopeStorage(); $server->setRefreshTokenStorage(new Storage\RefreshTokenStorage);
$accessTokenStorage = new Storage\AccessTokenStorage(); $server->setClientStorage(new Storage\ClientStorage);
$refreshTokenStorage = new Storage\RefreshTokenStorage(); $server->setScopeStorage(new Storage\ScopeStorage);
$authCodeStorage = new Storage\AuthCodeStorage(); $server->setAuthCodeStorage(new Storage\AuthCodeStorage);
$server = new AuthorizationServer(); $authCodeGrant = new \League\OAuth2\Server\Grant\AuthCodeGrant();
$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->addGrantType($authCodeGrant);
$request = (new Request)->createFromGlobals();
$server->setRequest($request); $server->setRequest($request);
// GET /authorize // GET /authorize
@ -61,9 +53,13 @@ $router->get('/authorize', function (Request $request) use ($server) {
// ... // ...
// ...
// ...
// Create a new authorize request which will respond with a redirect URI that the user will be redirected to // 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); $redirectUri = $server->getGrantType('authorization_code')->newAuthorizeRequest('user', 1, $authParams);
$response = new Response('', 200, [ $response = new Response('', 200, [
'Location' => $redirectUri 'Location' => $redirectUri
@ -72,6 +68,22 @@ $router->get('/authorize', function (Request $request) use ($server) {
return $response; return $response;
}); });
// /access_token
$router->post('/access_token', function (Request $request) use ($server) {
try {
$response = $server->getGrantType('authorization_code')->completeFlow();
} catch (\Exception $e) {
echo json_encode([
'error' => $e->errorType,
'message' => $e->getMessage()
]);
exit;
}
});
$dispatcher = $router->getDispatcher(); $dispatcher = $router->getDispatcher();
$response = $dispatcher->dispatch($request->getMethod(), $request->getPathInfo()); $response = $dispatcher->dispatch($request->getMethod(), $request->getPathInfo());
$response->send(); $response->send();

View File

@ -59,6 +59,21 @@ Capsule::table('oauth_clients')->insert([
/******************************************************************************/ /******************************************************************************/
print 'Creating client redirect uris table'.PHP_EOL;
Capsule::schema()->create('oauth_client_redirect_uris', function ($table) {
$table->increments('id');
$table->string('client_id');
$table->string('redirect_uri');
});
Capsule::table('oauth_client_redirect_uris')->insert([
'client_id' => 'testclient',
'redirect_uri' => 'http://example.com/redirect'
]);
/******************************************************************************/
print 'Creating scopes table'.PHP_EOL; print 'Creating scopes table'.PHP_EOL;
Capsule::schema()->create('oauth_scopes', function ($table) { Capsule::schema()->create('oauth_scopes', function ($table) {

View File

@ -142,7 +142,8 @@ class AuthCodeGrant extends AbstractGrant
// Create a new auth code // Create a new auth code
$authCode = new AuthCodeEntity($this->server); $authCode = new AuthCodeEntity($this->server);
$authCode->setToken(SecureKey::generate()); $authCode->setToken(SecureKey::generate());
$authCode->setRedirectUri($authParams['redirect_uri']); $authCode->setRedirectUri();
$authCode->setExpireTime(time() + $this->authTokenTTL);
foreach ($authParams['scopes'] as $scope) { foreach ($authParams['scopes'] as $scope) {
$authCode->associateScope($scope); $authCode->associateScope($scope);
@ -156,10 +157,9 @@ class AuthCodeGrant extends AbstractGrant
/** /**
* Complete the auth code grant * Complete the auth code grant
* @param null|array $inputParams
* @return array * @return array
*/ */
public function completeFlow($inputParams = null) public function completeFlow()
{ {
// Get the required params // Get the required params
$clientId = $this->server->getRequest()->request->get('client_id', null); $clientId = $this->server->getRequest()->request->get('client_id', null);

View File

@ -72,10 +72,9 @@ class PasswordGrant extends AbstractGrant
/** /**
* Complete the password grant * Complete the password grant
* @param null|array $inputParams
* @return array * @return array
*/ */
public function completeFlow($inputParams = null) public function completeFlow()
{ {
// Get the required params // Get the required params
$clientId = $this->server->getRequest()->request->get('client_id', null); $clientId = $this->server->getRequest()->request->get('client_id', null);