thephpleague /
oauth2-server
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * @author Alex Bilbie <[email protected]> |
||
| 5 | * @copyright Copyright (c) Alex Bilbie |
||
| 6 | * @license http://mit-license.org/ |
||
| 7 | * |
||
| 8 | * @link https://github.com/thephpleague/oauth2-server |
||
| 9 | */ |
||
| 10 | |||
| 11 | declare(strict_types=1); |
||
| 12 | |||
| 13 | include __DIR__ . '/../vendor/autoload.php'; |
||
| 14 | |||
| 15 | use Laminas\Diactoros\Stream; |
||
| 16 | use League\OAuth2\Server\AuthorizationServer; |
||
| 17 | use League\OAuth2\Server\Grant\AuthCodeGrant; |
||
| 18 | use League\OAuth2\Server\Grant\RefreshTokenGrant; |
||
| 19 | use League\OAuth2\Server\Middleware\AuthorizationServerMiddleware; |
||
| 20 | use League\OAuth2\Server\Middleware\ResourceServerMiddleware; |
||
| 21 | use League\OAuth2\Server\ResourceServer; |
||
| 22 | use OAuth2ServerExamples\Repositories\AccessTokenRepository; |
||
| 23 | use OAuth2ServerExamples\Repositories\AuthCodeRepository; |
||
| 24 | use OAuth2ServerExamples\Repositories\ClientRepository; |
||
| 25 | use OAuth2ServerExamples\Repositories\RefreshTokenRepository; |
||
| 26 | use OAuth2ServerExamples\Repositories\ScopeRepository; |
||
| 27 | use Psr\Http\Message\ResponseInterface; |
||
| 28 | use Psr\Http\Message\ServerRequestInterface; |
||
| 29 | use Slim\App; |
||
|
0 ignored issues
–
show
|
|||
| 30 | |||
| 31 | $app = new App([ |
||
| 32 | 'settings' => [ |
||
| 33 | 'displayErrorDetails' => true, |
||
| 34 | ], |
||
| 35 | AuthorizationServer::class => function () { |
||
| 36 | // Init our repositories |
||
| 37 | $clientRepository = new ClientRepository(); |
||
| 38 | $accessTokenRepository = new AccessTokenRepository(); |
||
| 39 | $scopeRepository = new ScopeRepository(); |
||
| 40 | $authCodeRepository = new AuthCodeRepository(); |
||
| 41 | $refreshTokenRepository = new RefreshTokenRepository(); |
||
| 42 | |||
| 43 | $privateKeyPath = 'file://' . __DIR__ . '/../private.key'; |
||
| 44 | |||
| 45 | // Setup the authorization server |
||
| 46 | $server = new AuthorizationServer( |
||
| 47 | $clientRepository, |
||
| 48 | $accessTokenRepository, |
||
| 49 | $scopeRepository, |
||
| 50 | $privateKeyPath, |
||
| 51 | 'lxZFUEsBCJ2Yb14IF2ygAHI5N4+ZAUXXaSeeJm6+twsUmIen' |
||
| 52 | ); |
||
| 53 | |||
| 54 | // Enable the authentication code grant on the server with a token TTL of 1 hour |
||
| 55 | $server->enableGrantType( |
||
| 56 | new AuthCodeGrant( |
||
| 57 | $authCodeRepository, |
||
| 58 | $refreshTokenRepository, |
||
| 59 | new DateInterval('PT10M') |
||
| 60 | ), |
||
| 61 | new DateInterval('PT1H') |
||
| 62 | ); |
||
| 63 | |||
| 64 | // Enable the refresh token grant on the server with a token TTL of 1 month |
||
| 65 | $server->enableGrantType( |
||
| 66 | new RefreshTokenGrant($refreshTokenRepository), |
||
| 67 | new DateInterval('P1M') |
||
| 68 | ); |
||
| 69 | |||
| 70 | return $server; |
||
| 71 | }, |
||
| 72 | ResourceServer::class => function () { |
||
| 73 | $publicKeyPath = 'file://' . __DIR__ . '/../public.key'; |
||
| 74 | |||
| 75 | $server = new ResourceServer( |
||
| 76 | new AccessTokenRepository(), |
||
| 77 | $publicKeyPath |
||
| 78 | ); |
||
| 79 | |||
| 80 | return $server; |
||
| 81 | }, |
||
| 82 | ]); |
||
| 83 | |||
| 84 | // Access token issuer |
||
| 85 | $app->post('/access_token', function (): void { |
||
| 86 | })->add(new AuthorizationServerMiddleware($app->getContainer()->get(AuthorizationServer::class))); |
||
| 87 | |||
| 88 | // Secured API |
||
| 89 | $app->group('/api', function (): void { |
||
| 90 | $app->get('/user', function (ServerRequestInterface $request, ResponseInterface $response) { |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 91 | $params = []; |
||
| 92 | |||
| 93 | if (in_array('basic', $request->getAttribute('oauth_scopes', []))) { |
||
| 94 | $params = [ |
||
| 95 | 'id' => 1, |
||
| 96 | 'name' => 'Alex', |
||
| 97 | 'city' => 'London', |
||
| 98 | ]; |
||
| 99 | } |
||
| 100 | |||
| 101 | if (in_array('email', $request->getAttribute('oauth_scopes', []))) { |
||
| 102 | $params['email'] = '[email protected]'; |
||
| 103 | } |
||
| 104 | |||
| 105 | $body = new Stream('php://temp', 'r+'); |
||
| 106 | $body->write(json_encode($params)); |
||
| 107 | |||
| 108 | return $response->withBody($body); |
||
| 109 | }); |
||
| 110 | })->add(new ResourceServerMiddleware($app->getContainer()->get(ResourceServer::class))); |
||
| 111 | |||
| 112 | $app->run(); |
||
| 113 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths