Passed
Push — master ( c84556...38a67b )
by Andrew
02:08
created

examples/public/middleware_use.php (1 issue)

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
use Laminas\Diactoros\Stream;
14
use League\OAuth2\Server\AuthorizationServer;
15
use League\OAuth2\Server\Grant\AuthCodeGrant;
16
use League\OAuth2\Server\Grant\RefreshTokenGrant;
17
use League\OAuth2\Server\Middleware\AuthorizationServerMiddleware;
18
use League\OAuth2\Server\Middleware\ResourceServerMiddleware;
19
use League\OAuth2\Server\ResourceServer;
20
use OAuth2ServerExamples\Repositories\AccessTokenRepository;
21
use OAuth2ServerExamples\Repositories\AuthCodeRepository;
22
use OAuth2ServerExamples\Repositories\ClientRepository;
23
use OAuth2ServerExamples\Repositories\RefreshTokenRepository;
24
use OAuth2ServerExamples\Repositories\ScopeRepository;
25
use Psr\Http\Message\ResponseInterface;
26
use Psr\Http\Message\ServerRequestInterface;
27
use Slim\App;
28
29
include __DIR__ . '/../vendor/autoload.php';
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
    $this->get('/user', function (ServerRequestInterface $request, ResponseInterface $response) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
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