Issues (58)

examples/public/client_credentials.php (1 issue)

Labels
Severity
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\Exception\OAuthServerException;
18
use League\OAuth2\Server\Grant\ClientCredentialsGrant;
19
use OAuth2ServerExamples\Repositories\AccessTokenRepository;
20
use OAuth2ServerExamples\Repositories\ClientRepository;
21
use OAuth2ServerExamples\Repositories\ScopeRepository;
22
use Psr\Http\Message\ResponseInterface;
23
use Psr\Http\Message\ServerRequestInterface;
24
use Slim\App;
0 ignored issues
show
The type Slim\App was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
26
$app = new App([
27
    'settings' => [
28
        'displayErrorDetails' => true,
29
    ],
30
    AuthorizationServer::class => function () {
31
        // Init our repositories
32
        $clientRepository = new ClientRepository(); // instance of ClientRepositoryInterface
33
        $scopeRepository = new ScopeRepository(); // instance of ScopeRepositoryInterface
34
        $accessTokenRepository = new AccessTokenRepository(); // instance of AccessTokenRepositoryInterface
35
36
        // Path to public and private keys
37
        $privateKey = 'file://' . __DIR__ . '/../private.key';
38
        //$privateKey = new CryptKey('file://path/to/private.key', 'passphrase'); // if private key has a pass phrase
39
40
        // Setup the authorization server
41
        $server = new AuthorizationServer(
42
            $clientRepository,
43
            $accessTokenRepository,
44
            $scopeRepository,
45
            $privateKey,
46
            'lxZFUEsBCJ2Yb14IF2ygAHI5N4+ZAUXXaSeeJm6+twsUmIen'
47
        );
48
49
        // Enable the client credentials grant on the server
50
        $server->enableGrantType(
51
            new ClientCredentialsGrant(),
52
            new DateInterval('PT1H') // access tokens will expire after 1 hour
53
        );
54
55
        return $server;
56
    },
57
]);
58
59
$app->post('/access_token', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
60
    /* @var \League\OAuth2\Server\AuthorizationServer $server */
61
    $server = $app->getContainer()->get(AuthorizationServer::class);
62
63
    try {
64
        // Try to respond to the request
65
        return $server->respondToAccessTokenRequest($request, $response);
66
    } catch (OAuthServerException $exception) {
67
        // All instances of OAuthServerException can be formatted into a HTTP response
68
        return $exception->generateHttpResponse($response);
69
    } catch (Exception $exception) {
70
        // Unknown exception
71
        $body = new Stream('php://temp', 'r+');
72
        $body->write($exception->getMessage());
73
74
        return $response->withStatus(500)->withBody($body);
75
    }
76
});
77
78
$app->run();
79