73 lines
2.7 KiB
PHP
Raw Normal View History

2015-04-05 21:11:10 +01:00
<?php
2016-04-17 08:06:17 -04:00
use League\OAuth2\Server\AuthorizationServer;
2015-11-16 12:58:50 +00:00
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Grant\PasswordGrant;
2015-04-05 21:11:10 +01:00
use OAuth2ServerExamples\Repositories\AccessTokenRepository;
use OAuth2ServerExamples\Repositories\ClientRepository;
2016-01-13 00:47:41 +00:00
use OAuth2ServerExamples\Repositories\RefreshTokenRepository;
2015-04-05 21:11:10 +01:00
use OAuth2ServerExamples\Repositories\ScopeRepository;
use OAuth2ServerExamples\Repositories\UserRepository;
2016-02-12 19:06:31 +01:00
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
2015-11-16 12:58:50 +00:00
use Slim\App;
2015-04-05 21:11:10 +01:00
2016-02-22 03:00:50 -05:00
include __DIR__ . '/../vendor/autoload.php';
2015-04-05 21:11:10 +01:00
2016-01-17 14:23:42 +00:00
$app = new App([
2016-04-18 12:23:21 +01:00
// Add the authorization server to the DI container
2016-04-17 12:54:49 +01:00
AuthorizationServer::class => function () {
2016-01-17 14:23:42 +00:00
// Setup the authorization server
2016-04-17 12:54:49 +01:00
$server = new AuthorizationServer(
2016-04-18 12:23:21 +01:00
new ClientRepository(), // instance of ClientRepositoryInterface
new AccessTokenRepository(), // instance of AccessTokenRepositoryInterface
new ScopeRepository(), // instance of ScopeRepositoryInterface
2016-09-13 14:17:09 +00:00
'file://' . __DIR__ . '/../private.key', // path to private key
2017-07-01 18:46:48 +01:00
'lxZFUEsBCJ2Yb14IF2ygAHI5N4+ZAUXXaSeeJm6+twsUmIen' // encryption key
2016-04-18 12:23:21 +01:00
);
$grant = new PasswordGrant(
new UserRepository(), // instance of UserRepositoryInterface
new RefreshTokenRepository() // instance of RefreshTokenRepositoryInterface
2016-01-17 14:23:42 +00:00
);
2016-04-18 12:23:21 +01:00
$grant->setRefreshTokenTTL(new \DateInterval('P1M')); // refresh tokens will expire after 1 month
2016-01-17 14:23:42 +00:00
2016-01-17 18:40:26 +01:00
// Enable the password grant on the server with a token TTL of 1 hour
2016-01-17 14:23:42 +00:00
$server->enableGrantType(
2016-04-18 12:23:21 +01:00
$grant,
2016-04-18 18:08:27 -03:00
new \DateInterval('PT1H') // access tokens will expire after 1 hour
2016-01-17 14:23:42 +00:00
);
return $server;
2016-02-19 18:09:39 -05:00
},
2016-01-17 14:23:42 +00:00
]);
2015-04-05 21:11:10 +01:00
2016-04-18 12:23:21 +01:00
$app->post(
'/access_token',
function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
/* @var \League\OAuth2\Server\AuthorizationServer $server */
$server = $app->getContainer()->get(AuthorizationServer::class);
try {
// Try to respond to the access token request
return $server->respondToAccessTokenRequest($request, $response);
} catch (OAuthServerException $exception) {
// All instances of OAuthServerException can be converted to a PSR-7 response
return $exception->generateHttpResponse($response);
} catch (\Exception $exception) {
2016-02-12 19:06:31 +01:00
2016-04-18 12:23:21 +01:00
// Catch unexpected exceptions
$body = $response->getBody();
$body->write($exception->getMessage());
2016-02-12 19:06:31 +01:00
2016-09-13 14:17:09 +00:00
return $response->withStatus(500)->withBody($body);
2016-04-18 12:23:21 +01:00
}
2015-04-05 21:11:10 +01:00
}
2016-04-18 12:23:21 +01:00
);
2015-04-05 21:11:10 +01:00
2015-11-16 12:58:50 +00:00
$app->run();