Issues (58)

examples/public/auth_code.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\AuthCodeGrant;
19
use OAuth2ServerExamples\Entities\UserEntity;
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;
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...
28
29
$app = new App([
30
    'settings' => [
31
        'displayErrorDetails' => true,
32
    ],
33
    AuthorizationServer::class => function () {
34
        // Init our repositories
35
        $clientRepository = new ClientRepository();
36
        $scopeRepository = new ScopeRepository();
37
        $accessTokenRepository = new AccessTokenRepository();
38
        $authCodeRepository = new AuthCodeRepository();
39
        $refreshTokenRepository = new RefreshTokenRepository();
40
41
        $privateKeyPath = 'file://' . __DIR__ . '/../private.key';
42
43
        // Setup the authorization server
44
        $server = new AuthorizationServer(
45
            $clientRepository,
46
            $accessTokenRepository,
47
            $scopeRepository,
48
            $privateKeyPath,
49
            'lxZFUEsBCJ2Yb14IF2ygAHI5N4+ZAUXXaSeeJm6+twsUmIen'
50
        );
51
52
        // Enable the authentication code grant on the server with a token TTL of 1 hour
53
        $server->enableGrantType(
54
            new AuthCodeGrant(
55
                $authCodeRepository,
56
                $refreshTokenRepository,
57
                new DateInterval('PT10M')
58
            ),
59
            new DateInterval('PT1H')
60
        );
61
62
        return $server;
63
    },
64
]);
65
66
$app->get('/authorize', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
67
    /* @var \League\OAuth2\Server\AuthorizationServer $server */
68
    $server = $app->getContainer()->get(AuthorizationServer::class);
69
70
    try {
71
        // Validate the HTTP request and return an AuthorizationRequest object.
72
        // The auth request object can be serialized into a user's session
73
        $authRequest = $server->validateAuthorizationRequest($request);
74
75
        // Once the user has logged in set the user on the AuthorizationRequest
76
        $authRequest->setUser(new UserEntity());
77
78
        // Once the user has approved or denied the client update the status
79
        // (true = approved, false = denied)
80
        $authRequest->setAuthorizationApproved(true);
81
82
        // Return the HTTP redirect response
83
        return $server->completeAuthorizationRequest($authRequest, $response);
84
    } catch (OAuthServerException $exception) {
85
        return $exception->generateHttpResponse($response);
86
    } catch (Exception $exception) {
87
        $body = new Stream('php://temp', 'r+');
88
        $body->write($exception->getMessage());
89
90
        return $response->withStatus(500)->withBody($body);
91
    }
92
});
93
94
$app->post('/access_token', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
95
    /* @var \League\OAuth2\Server\AuthorizationServer $server */
96
    $server = $app->getContainer()->get(AuthorizationServer::class);
97
98
    try {
99
        return $server->respondToAccessTokenRequest($request, $response);
100
    } catch (OAuthServerException $exception) {
101
        return $exception->generateHttpResponse($response);
102
    } catch (Exception $exception) {
103
        $body = new Stream('php://temp', 'r+');
104
        $body->write($exception->getMessage());
105
106
        return $response->withStatus(500)->withBody($body);
107
    }
108
});
109
110
$app->run();
111