Updated examples

This commit is contained in:
Alex Bilbie 2014-08-04 18:54:01 +01:00
parent cd60c2961f
commit 684a8a269e
4 changed files with 64 additions and 15 deletions

View File

@ -6,6 +6,8 @@ use League\OAuth2\Server\Storage\RefreshTokenInterface;
use League\OAuth2\Server\Storage\Adapter; use League\OAuth2\Server\Storage\Adapter;
use League\OAuth2\Server\Entity\RefreshTokenEntity; use League\OAuth2\Server\Entity\RefreshTokenEntity;
use Illuminate\Database\Capsule\Manager as Capsule;
class RefreshTokenStorage extends Adapter implements RefreshTokenInterface class RefreshTokenStorage extends Adapter implements RefreshTokenInterface
{ {
/** /**
@ -21,7 +23,12 @@ class RefreshTokenStorage extends Adapter implements RefreshTokenInterface
*/ */
public function create($token, $expireTime, $accessToken) public function create($token, $expireTime, $accessToken)
{ {
die(var_dump(__METHOD__, func_get_args())); Capsule::table('oauth_refresh_tokens')
->insert([
'refresh_token' => $token,
'access_token' => $accessToken,
'expire_time' => $expireTime
]);
} }
/** /**

View File

@ -1,24 +1,22 @@
<?php <?php
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 \Orno\Http\Exception\MethodNotAllowedException; use \Orno\Http\Exception\NotFoundException;
use \League\OAuth2\Server\ResourceServer;
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;
use \League\Event\Emitter;
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 authorization server
$server = new \League\OAuth2\Server\AuthorizationServer; $server = new \League\OAuth2\Server\AuthorizationServer;
$server->setSessionStorage(new Storage\SessionStorage); $server->setSessionStorage(new Storage\SessionStorage);
$server->setAccessTokenStorage(new Storage\AccessTokenStorage); $server->setAccessTokenStorage(new Storage\AccessTokenStorage);
@ -30,8 +28,12 @@ $server->setAuthCodeStorage(new Storage\AuthCodeStorage);
$authCodeGrant = new \League\OAuth2\Server\Grant\AuthCodeGrant(); $authCodeGrant = new \League\OAuth2\Server\Grant\AuthCodeGrant();
$server->addGrantType($authCodeGrant); $server->addGrantType($authCodeGrant);
$refrehTokenGrant = new \League\OAuth2\Server\Grant\RefreshTokenGrant();
$server->addGrantType($refrehTokenGrant);
// Routing setup
$request = (new Request)->createFromGlobals(); $request = (new Request)->createFromGlobals();
$server->setRequest($request); $router = new \Orno\Route\RouteCollection;
$router->get('/authorize', function (Request $request) use ($server) { $router->get('/authorize', function (Request $request) use ($server) {
@ -96,5 +98,42 @@ $router->post('/access_token', function (Request $request) use ($server) {
}); });
$dispatcher = $router->getDispatcher(); $dispatcher = $router->getDispatcher();
$response = $dispatcher->dispatch($request->getMethod(), $request->getPathInfo());
$response->send(); try {
// A successful response
$response = $dispatcher->dispatch(
$request->getMethod(),
$request->getPathInfo()
);
} catch (\Orno\Http\Exception $e) {
// A failed response
$response = $e->getJsonResponse();
$response->setContent(json_encode(['status_code' => $e->getStatusCode(), 'message' => $e->getMessage()]));
} catch (\League\OAuth2\Server\Exception\OAuthException $e) {
$response = new Response(json_encode([
'error' => $e->errorType,
'message' => $e->getMessage()
]), $e->httpStatusCode);
foreach ($e->getHttpHeaders() as $header) {
$response->headers($header);
}
} catch (\Exception $e) {
$response = new Orno\Http\Response;
$response->setStatusCode(500);
$response->setContent(json_encode(['status_code' => 500, 'message' => $e->getMessage()]));
} finally {
// Return the response
$response->headers->set('Content-type', 'application/json');
$response->send();
}

View File

@ -43,8 +43,8 @@ $passwordGrant->setVerifyCredentialsCallback(function ($username, $password) {
}); });
$server->addGrantType($passwordGrant); $server->addGrantType($passwordGrant);
// $refrehTokenGrant = new \League\OAuth2\Server\Grant\RefreshTokenGrant(); $refrehTokenGrant = new \League\OAuth2\Server\Grant\RefreshTokenGrant();
// $server->addGrantType($refrehTokenGrant); $server->addGrantType($refrehTokenGrant);
// Routing setup // Routing setup
$request = (new Request)->createFromGlobals(); $request = (new Request)->createFromGlobals();

View File

@ -80,6 +80,9 @@ response:
- -
key: access_token key: access_token
valueRegex: /([a-zA-Z0-9]*)/ valueRegex: /([a-zA-Z0-9]*)/
-
key: refresh_token
valueRegex: /([a-zA-Z0-9]*)/
- -
key: token_type key: token_type
value: Bearer value: Bearer