Issues (37)

examples/public/device_code.php (2 issues)

Labels
Severity
1
<?php
2
3
/**
4
 * @author    Andrew Millington <[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 League\OAuth2\Server\AuthorizationServer;
16
use League\OAuth2\Server\Exception\OAuthServerException;
17
use League\OAuth2\Server\Grant\DeviceCodeGrant;
18
use OAuth2ServerExamples\Repositories\AccessTokenRepository;
19
use OAuth2ServerExamples\Repositories\ClientRepository;
20
use OAuth2ServerExamples\Repositories\DeviceCodeRepository;
21
use OAuth2ServerExamples\Repositories\RefreshTokenRepository;
22
use OAuth2ServerExamples\Repositories\ScopeRepository;
23
use Psr\Http\Message\ResponseInterface;
24
use Psr\Http\Message\ServerRequestInterface;
25
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...
26
use Zend\Diactoros\Stream;
0 ignored issues
show
The type Zend\Diactoros\Stream 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...
27
28
$app = new App([
29
    'settings' => [
30
        'displayErrorDetails' => true,
31
    ],
32
    AuthorizationServer::class => function () {
33
        // Init our repositories
34
        $clientRepository = new ClientRepository();
35
        $scopeRepository = new ScopeRepository();
36
        $accessTokenRepository = new AccessTokenRepository();
37
        $refreshTokenRepository = new RefreshTokenRepository();
38
        $deviceCodeRepository = new DeviceCodeRepository();
39
40
        $privateKeyPath = 'file://' . __DIR__ . '/../private.key';
41
42
        // Set up the authorization server
43
        $server = new AuthorizationServer(
44
            $clientRepository,
45
            $accessTokenRepository,
46
            $scopeRepository,
47
            $privateKeyPath,
48
            'lxZFUEsBCJ2Yb14IF2ygAHI5N4+ZAUXXaSeeJm6+twsUmIen'
49
        );
50
51
        // Enable the device code grant on the server with a token TTL of 1 hour
52
        $server->enableGrantType(
53
            new DeviceCodeGrant(
54
                $deviceCodeRepository,
55
                $refreshTokenRepository,
56
                new DateInterval('PT10M'),
57
                'http://foo/bar'
58
            ),
59
            new DateInterval('PT1H')
60
        );
61
62
        return $server;
63
    },
64
]);
65
66
$app->post('/device_authorization', 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
        $deviceCodeResponse = $server->respondToDeviceAuthorizationRequest($request, $response);
72
73
        return $deviceCodeResponse;
74
75
        // Extract the device code. Usually we would then assign the user ID to
76
        // the device code but for the purposes of this example, we've hard
77
        // coded it in the response above.
78
        // $deviceCode = json_decode((string) $deviceCodeResponse->getBody());
79
80
        // Once the user has logged in and approved the request, set the user on the device code
81
        // $server->completeDeviceAuthorizationRequest($deviceCode->user_code, 1);
82
    } catch (OAuthServerException $exception) {
83
        return $exception->generateHttpResponse($response);
84
    } catch (Exception $exception) {
85
        $body = new Stream('php://temp', 'r+');
86
        $body->write($exception->getMessage());
87
88
        return $response->withStatus(500)->withBody($body);
89
    }
90
});
91
92
$app->post('/access_token', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
93
    /* @var \League\OAuth2\Server\AuthorizationServer $server */
94
    $server = $app->getContainer()->get(AuthorizationServer::class);
95
96
    try {
97
        return $server->respondToAccessTokenRequest($request, $response);
98
    } catch (OAuthServerException $exception) {
99
        return $exception->generateHttpResponse($response);
100
    } catch (Exception $exception) {
101
        $body = new Stream('php://temp', 'r+');
102
        $body->write($exception->getMessage());
103
104
        return $response->withStatus(500)->withBody($body);
105
    }
106
});
107
108
$app->run();
109