Issues (58)

examples/public/api.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\Middleware\ResourceServerMiddleware;
8
use League\OAuth2\Server\ResourceServer;
9
use OAuth2ServerExamples\Repositories\AccessTokenRepository;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
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...
13
14
$app = new App([
15
    // Add the resource server to the DI container
16
    ResourceServer::class => function () {
17
        $server = new ResourceServer(
18
            new AccessTokenRepository(),            // instance of AccessTokenRepositoryInterface
19
            'file://' . __DIR__ . '/../public.key'  // the authorization server's public key
20
        );
21
22
        return $server;
23
    },
24
]);
25
26
// Add the resource server middleware which will intercept and validate requests
27
$app->add(
28
    new ResourceServerMiddleware(
29
        $app->getContainer()->get(ResourceServer::class)
30
    )
31
);
32
33
// An example endpoint secured with OAuth 2.0
34
$app->get(
35
    '/users',
36
    function (ServerRequestInterface $request, ResponseInterface $response) {
37
        $users = [
38
            [
39
                'id'    => 123,
40
                'name'  => 'Alex',
41
                'email' => '[email protected]',
42
            ],
43
            [
44
                'id'    => 124,
45
                'name'  => 'Frank',
46
                'email' => '[email protected]',
47
            ],
48
            [
49
                'id'    => 125,
50
                'name'  => 'Phil',
51
                'email' => '[email protected]',
52
            ],
53
        ];
54
55
        $totalUsers = count($users);
56
57
        // If the access token doesn't have the `basic` scope hide users' names
58
        if (in_array('basic', $request->getAttribute('oauth_scopes')) === false) {
59
            for ($i = 0; $i < $totalUsers; $i++) {
60
                unset($users[$i]['name']);
61
            }
62
        }
63
64
        // If the access token doesn't have the `email` scope hide users' email addresses
65
        if (in_array('email', $request->getAttribute('oauth_scopes')) === false) {
66
            for ($i = 0; $i < $totalUsers; $i++) {
67
                unset($users[$i]['email']);
68
            }
69
        }
70
71
        $response->getBody()->write(json_encode($users));
72
73
        return $response->withStatus(200);
74
    }
75
);
76
77
$app->run();
78