oauth2-server/examples/public/refresh_token.php

74 lines
2.5 KiB
PHP
Raw Normal View History

2016-01-13 00:28:52 +00:00
<?php
2016-04-17 13:06:05 +01:00
/**
* @author Alex Bilbie <hello@alexbilbie.com>
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
*
* @link https://github.com/thephpleague/oauth2-server
*/
2016-01-13 00:28:52 +00:00
2016-04-17 08:06:17 -04:00
use League\OAuth2\Server\AuthorizationServer;
2016-01-13 00:28:52 +00:00
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Grant\RefreshTokenGrant;
use OAuth2ServerExamples\Repositories\AccessTokenRepository;
use OAuth2ServerExamples\Repositories\ClientRepository;
use OAuth2ServerExamples\Repositories\RefreshTokenRepository;
use OAuth2ServerExamples\Repositories\ScopeRepository;
2016-02-12 19:06:31 +01:00
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
2016-01-13 00:28:52 +00:00
use Slim\App;
2016-02-22 03:00:50 -05:00
include __DIR__ . '/../vendor/autoload.php';
2016-01-13 00:28:52 +00:00
2016-02-12 19:06:31 +01:00
$app = new App([
'settings' => [
'displayErrorDetails' => true,
],
2016-04-17 12:54:49 +01:00
AuthorizationServer::class => function () {
2016-02-12 19:06:31 +01:00
// Init our repositories
$clientRepository = new ClientRepository();
$accessTokenRepository = new AccessTokenRepository();
$scopeRepository = new ScopeRepository();
$refreshTokenRepository = new RefreshTokenRepository();
2016-01-17 14:25:44 +00:00
2016-02-12 19:06:31 +01:00
$privateKeyPath = 'file://' . __DIR__ . '/../private.key';
2016-01-13 00:28:52 +00:00
2016-02-12 19:06:31 +01:00
// Setup the authorization server
2016-04-17 12:54:49 +01:00
$server = new AuthorizationServer(
2016-02-12 19:06:31 +01:00
$clientRepository,
$accessTokenRepository,
$scopeRepository,
$privateKeyPath,
2017-07-01 18:46:48 +01:00
'lxZFUEsBCJ2Yb14IF2ygAHI5N4+ZAUXXaSeeJm6+twsUmIen'
2016-02-12 19:06:31 +01:00
);
2016-01-17 14:25:44 +00:00
2016-04-12 20:23:05 +01:00
// Enable the refresh token grant on the server
$grant = new RefreshTokenGrant($refreshTokenRepository);
$grant->setRefreshTokenTTL(new \DateInterval('P1M')); // The refresh token will expire in 1 month
2016-02-12 19:06:31 +01:00
$server->enableGrantType(
2016-04-12 20:23:05 +01:00
$grant,
new \DateInterval('PT1H') // The new access token will expire after 1 hour
2016-02-12 19:06:31 +01:00
);
2016-01-17 14:25:44 +00:00
2016-02-12 19:06:31 +01:00
return $server;
2016-03-08 21:49:05 +01:00
},
2016-02-12 19:06:31 +01:00
]);
2016-01-17 14:25:44 +00:00
2016-02-12 19:06:31 +01:00
$app->post('/access_token', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
2016-04-17 12:54:49 +01:00
/* @var \League\OAuth2\Server\AuthorizationServer $server */
$server = $app->getContainer()->get(AuthorizationServer::class);
2016-01-13 00:28:52 +00:00
try {
return $server->respondToAccessTokenRequest($request, $response);
2016-02-12 19:06:31 +01:00
} catch (OAuthServerException $exception) {
return $exception->generateHttpResponse($response);
} catch (\Exception $exception) {
2017-07-01 16:30:36 +01:00
$response->getBody()->write($exception->getMessage());
2017-07-01 16:19:23 +00:00
2017-07-01 16:30:36 +01:00
return $response->withStatus(500);
2016-01-13 00:28:52 +00:00
}
});
$app->run();