mirror of
https://github.com/elyby/oauth2-server.git
synced 2024-12-22 21:19:46 +05:30
80 lines
2.7 KiB
PHP
80 lines
2.7 KiB
PHP
<?php
|
|
/**
|
|
* @author Alex Bilbie <hello@alexbilbie.com>
|
|
* @copyright Copyright (c) Alex Bilbie
|
|
* @license http://mit-license.org/
|
|
*
|
|
* @link https://github.com/thephpleague/oauth2-server
|
|
*/
|
|
|
|
use League\OAuth2\Server\AuthorizationServer;
|
|
use League\OAuth2\Server\Exception\OAuthServerException;
|
|
use OAuth2ServerExamples\Repositories\AccessTokenRepository;
|
|
use OAuth2ServerExamples\Repositories\ClientRepository;
|
|
use OAuth2ServerExamples\Repositories\ScopeRepository;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use Slim\App;
|
|
use Zend\Diactoros\Stream;
|
|
|
|
include __DIR__ . '/../vendor/autoload.php';
|
|
|
|
$app = new App([
|
|
'settings' => [
|
|
'displayErrorDetails' => true,
|
|
],
|
|
AuthorizationServer::class => function () {
|
|
// Init our repositories
|
|
$clientRepository = new ClientRepository(); // instance of ClientRepositoryInterface
|
|
$scopeRepository = new ScopeRepository(); // instance of ScopeRepositoryInterface
|
|
$accessTokenRepository = new AccessTokenRepository(); // instance of AccessTokenRepositoryInterface
|
|
|
|
// Path to public and private keys
|
|
$privateKey = 'file://' . __DIR__ . '/../private.key';
|
|
//$privateKey = new CryptKey('file://path/to/private.key', 'passphrase'); // if private key has a pass phrase
|
|
$publicKey = 'file://' . __DIR__ . '/../public.key';
|
|
|
|
// Setup the authorization server
|
|
$server = new AuthorizationServer(
|
|
$clientRepository,
|
|
$accessTokenRepository,
|
|
$scopeRepository,
|
|
$privateKey,
|
|
$publicKey
|
|
);
|
|
|
|
// Enable the client credentials grant on the server
|
|
$server->enableGrantType(
|
|
new \League\OAuth2\Server\Grant\ClientCredentialsGrant(),
|
|
new \DateInterval('PT1H') // access tokens will expire after 1 hour
|
|
);
|
|
|
|
return $server;
|
|
},
|
|
]);
|
|
|
|
$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 request
|
|
return $server->respondToAccessTokenRequest($request, $response);
|
|
} catch (OAuthServerException $exception) {
|
|
|
|
// All instances of OAuthServerException can be formatted into a HTTP response
|
|
return $exception->generateHttpResponse($response);
|
|
} catch (\Exception $exception) {
|
|
|
|
// Unknown exception
|
|
$body = new Stream('php://temp', 'r+');
|
|
$body->write($exception->getMessage());
|
|
|
|
return $response->withStatus(500)->withBody($body);
|
|
}
|
|
});
|
|
|
|
$app->run();
|