Passed
Pull Request — master (#1473)
by
unknown
34:47
created

TokenServer::setTokenRevocationHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
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
19
class TokenServer implements EmitterAwareInterface
20
{
21
    use EmitterAwarePolyfill;
22
23
    private CryptKeyInterface $publicKey;
24
25
    protected ?TokenHandlerInterface $tokenRevocationHandler = null;
26
27
    protected ?TokenHandlerInterface $tokenIntrospectionHandler = null;
28
29
    public function __construct(
30
        private ClientRepositoryInterface $clientRepository,
31
        private AccessTokenRepositoryInterface $accessTokenRepository,
32
        private RefreshTokenRepositoryInterface $refreshTokenRepository,
33
        CryptKeyInterface|string $publicKey,
34
        private Key|string $encryptionKey
35
    ) {
36
        if ($publicKey instanceof CryptKeyInterface === false) {
37
            $publicKey = new CryptKey($publicKey);
38
        }
39
40
        $this->publicKey = $publicKey;
41
    }
42
43
    public function setTokenRevocationHandler(TokenHandlerInterface $handler): void
44
    {
45
        $this->tokenRevocationHandler = $handler;
46
    }
47
48
    public function setTokenIntrospectionHandler(TokenHandlerInterface $handler): void
49
    {
50
        $this->tokenIntrospectionHandler = $handler;
51
    }
52
53
    protected function getTokenRevocationHandler(): TokenHandlerInterface
54
    {
55
        $this->tokenRevocationHandler ??= new TokenRevocationHandler();
56
57
        $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

57
        $this->tokenRevocationHandler->/** @scrutinizer ignore-call */ 
58
                                       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...
58
        $this->tokenRevocationHandler->setAccessTokenRepository($this->accessTokenRepository);
59
        $this->tokenRevocationHandler->setRefreshTokenRepository($this->refreshTokenRepository);
60
        $this->tokenRevocationHandler->setPublicKey($this->publicKey);
61
        $this->tokenRevocationHandler->setEmitter($this->getEmitter());
62
        $this->tokenRevocationHandler->setEncryptionKey($this->encryptionKey);
63
64
        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...
65
    }
66
67
    protected function getTokenIntrospectionHandler(): TokenHandlerInterface
68
    {
69
        $this->tokenIntrospectionHandler ??= new TokenIntrospectionHandler();
70
71
        $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

71
        $this->tokenIntrospectionHandler->/** @scrutinizer ignore-call */ 
72
                                          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...
72
        $this->tokenIntrospectionHandler->setAccessTokenRepository($this->accessTokenRepository);
73
        $this->tokenIntrospectionHandler->setRefreshTokenRepository($this->refreshTokenRepository);
74
        $this->tokenIntrospectionHandler->setPublicKey($this->publicKey);
75
        $this->tokenIntrospectionHandler->setEmitter($this->getEmitter());
76
        $this->tokenIntrospectionHandler->setEncryptionKey($this->encryptionKey);
77
78
        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...
79
    }
80
81
    public function respondToTokenRevocationRequest(
82
        ServerRequestInterface $request,
83
        ResponseInterface $response
84
    ): ResponseInterface {
85
        return $this->getTokenRevocationHandler()->respondToRequest($request, $response);
86
    }
87
88
    public function respondToTokenIntrospectionRequest(
89
        ServerRequestInterface $request,
90
        ResponseInterface $response
91
    ): ResponseInterface {
92
        return $this->getTokenIntrospectionHandler()->respondToRequest($request, $response);
93
    }
94
}
95