oauth2-server/examples/public/password.php

73 lines
2.7 KiB
PHP
Raw Normal View History

2015-04-06 01:41:10 +05:30
<?php
2016-04-17 17:36:17 +05:30
use League\OAuth2\Server\AuthorizationServer;
2015-11-16 18:28:50 +05:30
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Grant\PasswordGrant;
2015-04-06 01:41:10 +05:30
use OAuth2ServerExamples\Repositories\AccessTokenRepository;
use OAuth2ServerExamples\Repositories\ClientRepository;
2016-01-13 06:17:41 +05:30
use OAuth2ServerExamples\Repositories\RefreshTokenRepository;
2015-04-06 01:41:10 +05:30
use OAuth2ServerExamples\Repositories\ScopeRepository;
use OAuth2ServerExamples\Repositories\UserRepository;
2016-02-12 23:36:31 +05:30
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
2015-11-16 18:28:50 +05:30
use Slim\App;
2015-04-06 01:41:10 +05:30
2016-02-22 13:30:50 +05:30
include __DIR__ . '/../vendor/autoload.php';
2015-04-06 01:41:10 +05:30
2016-01-17 19:53:42 +05:30
$app = new App([
2016-04-18 16:53:21 +05:30
// Add the authorization server to the DI container
2016-04-17 17:24:49 +05:30
AuthorizationServer::class => function () {
2016-01-17 19:53:42 +05:30
// Setup the authorization server
2016-04-17 17:24:49 +05:30
$server = new AuthorizationServer(
2016-04-18 16:53:21 +05:30
new ClientRepository(), // instance of ClientRepositoryInterface
new AccessTokenRepository(), // instance of AccessTokenRepositoryInterface
new ScopeRepository(), // instance of ScopeRepositoryInterface
2016-09-13 19:47:09 +05:30
'file://' . __DIR__ . '/../private.key', // path to private key
'file://' . __DIR__ . '/../public.key' // path to public key
2016-04-18 16:53:21 +05:30
);
$grant = new PasswordGrant(
new UserRepository(), // instance of UserRepositoryInterface
new RefreshTokenRepository() // instance of RefreshTokenRepositoryInterface
2016-01-17 19:53:42 +05:30
);
2016-04-18 16:53:21 +05:30
$grant->setRefreshTokenTTL(new \DateInterval('P1M')); // refresh tokens will expire after 1 month
2016-01-17 19:53:42 +05:30
2016-01-17 23:10:26 +05:30
// Enable the password grant on the server with a token TTL of 1 hour
2016-01-17 19:53:42 +05:30
$server->enableGrantType(
2016-04-18 16:53:21 +05:30
$grant,
2016-04-19 02:38:27 +05:30
new \DateInterval('PT1H') // access tokens will expire after 1 hour
2016-01-17 19:53:42 +05:30
);
return $server;
2016-02-20 04:39:39 +05:30
},
2016-01-17 19:53:42 +05:30
]);
2015-04-06 01:41:10 +05:30
2016-04-18 16:53:21 +05:30
$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 23:36:31 +05:30
2016-04-18 16:53:21 +05:30
// Catch unexpected exceptions
$body = $response->getBody();
$body->write($exception->getMessage());
2016-02-12 23:36:31 +05:30
2016-09-13 19:47:09 +05:30
return $response->withStatus(500)->withBody($body);
2016-04-18 16:53:21 +05:30
}
2015-04-06 01:41:10 +05:30
}
2016-04-18 16:53:21 +05:30
);
2015-04-06 01:41:10 +05:30
2015-11-16 18:28:50 +05:30
$app->run();