mirror of
https://github.com/elyby/oauth2-server.git
synced 2025-01-03 10:41:51 +05:30
Code tidy client_credentials
This commit is contained in:
parent
6ed9cbf701
commit
25c2e9b31b
@ -1,8 +1,14 @@
|
|||||||
<?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 League\OAuth2\Server\Exception\OAuthServerException;
|
||||||
use League\OAuth2\Server\Grant\ClientCredentialsGrant;
|
|
||||||
use League\OAuth2\Server\Server;
|
|
||||||
use OAuth2ServerExamples\Repositories\AccessTokenRepository;
|
use OAuth2ServerExamples\Repositories\AccessTokenRepository;
|
||||||
use OAuth2ServerExamples\Repositories\ClientRepository;
|
use OAuth2ServerExamples\Repositories\ClientRepository;
|
||||||
use OAuth2ServerExamples\Repositories\ScopeRepository;
|
use OAuth2ServerExamples\Repositories\ScopeRepository;
|
||||||
@ -17,28 +23,30 @@ $app = new App([
|
|||||||
'settings' => [
|
'settings' => [
|
||||||
'displayErrorDetails' => true,
|
'displayErrorDetails' => true,
|
||||||
],
|
],
|
||||||
Server::class => function () {
|
AuthorizationServer::class => function () {
|
||||||
// Init our repositories
|
// Init our repositories
|
||||||
$clientRepository = new ClientRepository();
|
$clientRepository = new ClientRepository(); // instance of ClientRepositoryInterface
|
||||||
$accessTokenRepository = new AccessTokenRepository();
|
$scopeRepository = new ScopeRepository(); // instance of ScopeRepositoryInterface
|
||||||
$scopeRepository = new ScopeRepository();
|
$accessTokenRepository = new AccessTokenRepository(); // instance of AccessTokenRepositoryInterface
|
||||||
|
|
||||||
$privateKeyPath = 'file://' . __DIR__ . '/../private.key';
|
// Path to public and private keys
|
||||||
$publicKeyPath = 'file://' . __DIR__ . '/../public.key';
|
$privateKey = 'file://path/to/private.key';
|
||||||
|
//$privateKey = new CryptKey('file://path/to/private.key', 'passphrase'); // if private key has a pass phrase
|
||||||
|
$publicKey = 'file://path/to/public.key';
|
||||||
|
|
||||||
// Setup the authorization server
|
// Setup the authorization server
|
||||||
$server = new Server(
|
$server = new AuthorizationServer(
|
||||||
$clientRepository,
|
$clientRepository,
|
||||||
$accessTokenRepository,
|
$accessTokenRepository,
|
||||||
$scopeRepository,
|
$scopeRepository,
|
||||||
$privateKeyPath,
|
$privateKey,
|
||||||
$publicKeyPath
|
$publicKey
|
||||||
);
|
);
|
||||||
|
|
||||||
// Enable the client credentials grant on the server with a token TTL of 1 hour
|
// Enable the client credentials grant on the server
|
||||||
$server->enableGrantType(
|
$server->enableGrantType(
|
||||||
new ClientCredentialsGrant(),
|
new \League\OAuth2\Server\Grant\ClientCredentialsGrant(),
|
||||||
new \DateInterval('PT1H')
|
new \DateInterval('PT1H') // access tokens will expire after 1 hour
|
||||||
);
|
);
|
||||||
|
|
||||||
return $server;
|
return $server;
|
||||||
@ -46,18 +54,28 @@ $app = new App([
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$app->post('/access_token', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
|
$app->post('/access_token', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
|
||||||
/* @var \League\OAuth2\Server\Server $server */
|
|
||||||
$server = $app->getContainer()->get(Server::class);
|
/* @var \League\OAuth2\Server\AuthorizationServer $server */
|
||||||
|
$server = $app->getContainer()->get(AuthorizationServer::class);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
// Try to respond to the request
|
||||||
return $server->respondToAccessTokenRequest($request, $response);
|
return $server->respondToAccessTokenRequest($request, $response);
|
||||||
|
|
||||||
} catch (OAuthServerException $exception) {
|
} catch (OAuthServerException $exception) {
|
||||||
|
|
||||||
|
// All instances of OAuthServerException can be formatted into a HTTP response
|
||||||
return $exception->generateHttpResponse($response);
|
return $exception->generateHttpResponse($response);
|
||||||
|
|
||||||
} catch (\Exception $exception) {
|
} catch (\Exception $exception) {
|
||||||
|
|
||||||
|
// Unknown exception
|
||||||
$body = new Stream('php://temp', 'r+');
|
$body = new Stream('php://temp', 'r+');
|
||||||
$body->write($exception->getMessage());
|
$body->write($exception->getMessage());
|
||||||
|
|
||||||
return $response->withStatus(500)->withBody($body);
|
return $response->withStatus(500)->withBody($body);
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user