mirror of
https://github.com/elyby/oauth2-server.git
synced 2025-01-03 10:41:51 +05:30
Updated examples
This commit is contained in:
parent
cd60c2961f
commit
684a8a269e
@ -6,6 +6,8 @@ use League\OAuth2\Server\Storage\RefreshTokenInterface;
|
||||
use League\OAuth2\Server\Storage\Adapter;
|
||||
use League\OAuth2\Server\Entity\RefreshTokenEntity;
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
|
||||
class RefreshTokenStorage extends Adapter implements RefreshTokenInterface
|
||||
{
|
||||
/**
|
||||
@ -21,7 +23,12 @@ class RefreshTokenStorage extends Adapter implements RefreshTokenInterface
|
||||
*/
|
||||
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
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,24 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace OAuth2Server\RelationalExample;
|
||||
|
||||
use \Orno\Http\Request;
|
||||
use \Orno\Http\Response;
|
||||
use \Orno\Http\JsonResponse;
|
||||
use \Orno\Http\Exception\MethodNotAllowedException;
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
|
||||
// use \League\OAuth2\Server\Exception;
|
||||
use \Orno\Http\Exception\NotFoundException;
|
||||
use \League\OAuth2\Server\ResourceServer;
|
||||
use \RelationalExample\Storage;
|
||||
use \RelationalExample\Model;
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use \League\Event\Emitter;
|
||||
|
||||
include __DIR__.'/vendor/autoload.php';
|
||||
|
||||
// Routing setup
|
||||
$request = (new Request)->createFromGlobals();
|
||||
$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->setSessionStorage(new Storage\SessionStorage);
|
||||
$server->setAccessTokenStorage(new Storage\AccessTokenStorage);
|
||||
@ -30,8 +28,12 @@ $server->setAuthCodeStorage(new Storage\AuthCodeStorage);
|
||||
$authCodeGrant = new \League\OAuth2\Server\Grant\AuthCodeGrant();
|
||||
$server->addGrantType($authCodeGrant);
|
||||
|
||||
$refrehTokenGrant = new \League\OAuth2\Server\Grant\RefreshTokenGrant();
|
||||
$server->addGrantType($refrehTokenGrant);
|
||||
|
||||
// Routing setup
|
||||
$request = (new Request)->createFromGlobals();
|
||||
$server->setRequest($request);
|
||||
$router = new \Orno\Route\RouteCollection;
|
||||
|
||||
$router->get('/authorize', function (Request $request) use ($server) {
|
||||
|
||||
@ -96,5 +98,42 @@ $router->post('/access_token', function (Request $request) use ($server) {
|
||||
});
|
||||
|
||||
$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();
|
||||
|
||||
}
|
||||
|
@ -43,8 +43,8 @@ $passwordGrant->setVerifyCredentialsCallback(function ($username, $password) {
|
||||
});
|
||||
$server->addGrantType($passwordGrant);
|
||||
|
||||
// $refrehTokenGrant = new \League\OAuth2\Server\Grant\RefreshTokenGrant();
|
||||
// $server->addGrantType($refrehTokenGrant);
|
||||
$refrehTokenGrant = new \League\OAuth2\Server\Grant\RefreshTokenGrant();
|
||||
$server->addGrantType($refrehTokenGrant);
|
||||
|
||||
// Routing setup
|
||||
$request = (new Request)->createFromGlobals();
|
||||
|
@ -80,6 +80,9 @@ response:
|
||||
-
|
||||
key: access_token
|
||||
valueRegex: /([a-zA-Z0-9]*)/
|
||||
-
|
||||
key: refresh_token
|
||||
valueRegex: /([a-zA-Z0-9]*)/
|
||||
-
|
||||
key: token_type
|
||||
value: Bearer
|
Loading…
Reference in New Issue
Block a user