Passed
Pull Request — master (#1473)
by
unknown
35:44
created

TokenServer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 28
c 3
b 0
f 1
dl 0
loc 75
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A respondToTokenRevocationRequest() 0 5 1
A setTokenIntrospectionHandler() 0 3 1
A getTokenIntrospectionHandler() 0 12 1
A getTokenRevocationHandler() 0 12 1
A __construct() 0 13 2
A respondToTokenIntrospectionRequest() 0 5 1
A setTokenRevocationHandler() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\OAuth2\Server;
6
7
use Defuse\Crypto\Key;
8
use League\OAuth2\Server\EventEmitting\EmitterAwareInterface;
9
use League\OAuth2\Server\EventEmitting\EmitterAwarePolyfill;
10
use League\OAuth2\Server\Handlers\TokenHandlerInterface;
11
use League\OAuth2\Server\Handlers\TokenIntrospectionHandler;
12
use League\OAuth2\Server\Handlers\TokenRevocationHandler;
13
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
14
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
15
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
16
use Psr\Http\Message\ResponseInterface;
17
use Psr\Http\Message\ServerRequestInterface;
18
use SensitiveParameter;
0 ignored issues
show
Bug introduced by
The type SensitiveParameter 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...
19
20
class TokenServer implements EmitterAwareInterface
21
{
22
    use EmitterAwarePolyfill;
23
24
    private CryptKeyInterface $publicKey;
25
26
    protected ?TokenHandlerInterface $tokenRevocationHandler = null;
27
28
    protected ?TokenHandlerInterface $tokenIntrospectionHandler = null;
29
30
    public function __construct(
31
        private ClientRepositoryInterface $clientRepository,
32
        private AccessTokenRepositoryInterface $accessTokenRepository,
33
        private RefreshTokenRepositoryInterface $refreshTokenRepository,
34
        CryptKeyInterface|string $publicKey,
35
        #[SensitiveParameter]
36
        private Key|string $encryptionKey
37
    ) {
38
        if ($publicKey instanceof CryptKeyInterface === false) {
39
            $publicKey = new CryptKey($publicKey);
40
        }
41
42
        $this->publicKey = $publicKey;
43
    }
44
45
    public function setTokenRevocationHandler(TokenHandlerInterface $handler): void
46
    {
47
        $this->tokenRevocationHandler = $handler;
48
    }
49
50
    public function setTokenIntrospectionHandler(TokenHandlerInterface $handler): void
51
    {
52
        $this->tokenIntrospectionHandler = $handler;
53
    }
54
55
    protected function getTokenRevocationHandler(): TokenHandlerInterface
56
    {
57
        $this->tokenRevocationHandler ??= new TokenRevocationHandler();
58
59
        $this->tokenRevocationHandler->setClientRepository($this->clientRepository);
0 ignored issues
show
Bug introduced by
The method setClientRepository() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
        $this->tokenRevocationHandler->/** @scrutinizer ignore-call */ 
60
                                       setClientRepository($this->clientRepository);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
        $this->tokenRevocationHandler->setAccessTokenRepository($this->accessTokenRepository);
61
        $this->tokenRevocationHandler->setRefreshTokenRepository($this->refreshTokenRepository);
62
        $this->tokenRevocationHandler->setPublicKey($this->publicKey);
63
        $this->tokenRevocationHandler->setEmitter($this->getEmitter());
64
        $this->tokenRevocationHandler->setEncryptionKey($this->encryptionKey);
65
66
        return $this->tokenRevocationHandler;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->tokenRevocationHandler could return the type null which is incompatible with the type-hinted return League\OAuth2\Server\Han...s\TokenHandlerInterface. Consider adding an additional type-check to rule them out.
Loading history...
67
    }
68
69
    protected function getTokenIntrospectionHandler(): TokenHandlerInterface
70
    {
71
        $this->tokenIntrospectionHandler ??= new TokenIntrospectionHandler();
72
73
        $this->tokenIntrospectionHandler->setClientRepository($this->clientRepository);
0 ignored issues
show
Bug introduced by
The method setClientRepository() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        $this->tokenIntrospectionHandler->/** @scrutinizer ignore-call */ 
74
                                          setClientRepository($this->clientRepository);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
        $this->tokenIntrospectionHandler->setAccessTokenRepository($this->accessTokenRepository);
75
        $this->tokenIntrospectionHandler->setRefreshTokenRepository($this->refreshTokenRepository);
76
        $this->tokenIntrospectionHandler->setPublicKey($this->publicKey);
77
        $this->tokenIntrospectionHandler->setEmitter($this->getEmitter());
78
        $this->tokenIntrospectionHandler->setEncryptionKey($this->encryptionKey);
79
80
        return $this->tokenIntrospectionHandler;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->tokenIntrospectionHandler could return the type null which is incompatible with the type-hinted return League\OAuth2\Server\Han...s\TokenHandlerInterface. Consider adding an additional type-check to rule them out.
Loading history...
81
    }
82
83
    public function respondToTokenRevocationRequest(
84
        ServerRequestInterface $request,
85
        ResponseInterface $response
86
    ): ResponseInterface {
87
        return $this->getTokenRevocationHandler()->respondToRequest($request, $response);
88
    }
89
90
    public function respondToTokenIntrospectionRequest(
91
        ServerRequestInterface $request,
92
        ResponseInterface $response
93
    ): ResponseInterface {
94
        return $this->getTokenIntrospectionHandler()->respondToRequest($request, $response);
95
    }
96
}
97