Issues (58)

examples/public/password.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
include __DIR__ . '/../vendor/autoload.php';
6
7
use League\OAuth2\Server\AuthorizationServer;
8
use League\OAuth2\Server\Exception\OAuthServerException;
9
use League\OAuth2\Server\Grant\PasswordGrant;
10
use OAuth2ServerExamples\Repositories\AccessTokenRepository;
11
use OAuth2ServerExamples\Repositories\ClientRepository;
12
use OAuth2ServerExamples\Repositories\RefreshTokenRepository;
13
use OAuth2ServerExamples\Repositories\ScopeRepository;
14
use OAuth2ServerExamples\Repositories\UserRepository;
15
use Psr\Http\Message\ResponseInterface;
16
use Psr\Http\Message\ServerRequestInterface;
17
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...
18
19
$app = new App([
20
    // Add the authorization server to the DI container
21
    AuthorizationServer::class => function () {
22
        // Setup the authorization server
23
        $server = new AuthorizationServer(
24
            new ClientRepository(),                 // instance of ClientRepositoryInterface
25
            new AccessTokenRepository(),            // instance of AccessTokenRepositoryInterface
26
            new ScopeRepository(),                  // instance of ScopeRepositoryInterface
27
            'file://' . __DIR__ . '/../private.key',    // path to private key
28
            'lxZFUEsBCJ2Yb14IF2ygAHI5N4+ZAUXXaSeeJm6+twsUmIen'      // encryption key
29
        );
30
31
        $grant = new PasswordGrant(
32
            new UserRepository(),           // instance of UserRepositoryInterface
33
            new RefreshTokenRepository()    // instance of RefreshTokenRepositoryInterface
34
        );
35
        $grant->setRefreshTokenTTL(new DateInterval('P1M')); // refresh tokens will expire after 1 month
36
37
        // Enable the password grant on the server with a token TTL of 1 hour
38
        $server->enableGrantType(
39
            $grant,
40
            new DateInterval('PT1H') // access tokens will expire after 1 hour
41
        );
42
43
        return $server;
44
    },
45
]);
46
47
$app->post(
48
    '/access_token',
49
    function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
50
        /* @var \League\OAuth2\Server\AuthorizationServer $server */
51
        $server = $app->getContainer()->get(AuthorizationServer::class);
52
53
        try {
54
            // Try to respond to the access token request
55
            return $server->respondToAccessTokenRequest($request, $response);
56
        } catch (OAuthServerException $exception) {
57
            // All instances of OAuthServerException can be converted to a PSR-7 response
58
            return $exception->generateHttpResponse($response);
59
        } catch (Exception $exception) {
60
            // Catch unexpected exceptions
61
            $body = $response->getBody();
62
            $body->write($exception->getMessage());
63
64
            return $response->withStatus(500)->withBody($body);
65
        }
66
    }
67
);
68
69
$app->run();
70