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

AbstractTokenHandler::setJwtValidator()   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\Handlers;
6
7
use League\OAuth2\Server\AbstractHandler;
8
use League\OAuth2\Server\AuthorizationValidators\BearerTokenValidator;
9
use League\OAuth2\Server\AuthorizationValidators\JwtValidatorInterface;
10
use League\OAuth2\Server\CryptKeyInterface;
11
use League\OAuth2\Server\Entities\ClientEntityInterface;
12
use League\OAuth2\Server\Exception\OAuthServerException;
13
use Psr\Http\Message\ServerRequestInterface;
14
use Throwable;
15
16
abstract class AbstractTokenHandler extends AbstractHandler implements TokenHandlerInterface
17
{
18
    private CryptKeyInterface $publicKey;
19
20
    private ?JwtValidatorInterface $jwtValidator = null;
21
22
    public function setPublicKey(CryptKeyInterface $publicKey): void
23
    {
24
        $this->publicKey = $publicKey;
25
    }
26
27
    public function setJwtValidator(JwtValidatorInterface $jwtValidator): void
28
    {
29
        $this->jwtValidator = $jwtValidator;
30
    }
31
32
    protected function getJwtValidator(): JwtValidatorInterface
33
    {
34
        if ($this->jwtValidator instanceof JwtValidatorInterface === false) {
35
            $this->jwtValidator = new BearerTokenValidator($this->accessTokenRepository);
36
        }
37
38
        if ($this->jwtValidator instanceof BearerTokenValidator === true) {
39
            $this->jwtValidator->setPublicKey($this->publicKey);
0 ignored issues
show
Bug introduced by
The method setPublicKey() 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

39
            $this->jwtValidator->/** @scrutinizer ignore-call */ 
40
                                 setPublicKey($this->publicKey);

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...
Bug introduced by
The method setPublicKey() does not exist on League\OAuth2\Server\Aut...s\JwtValidatorInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to League\OAuth2\Server\Aut...s\JwtValidatorInterface. ( Ignorable by Annotation )

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

39
            $this->jwtValidator->/** @scrutinizer ignore-call */ 
40
                                 setPublicKey($this->publicKey);
Loading history...
40
        }
41
42
        return $this->jwtValidator;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->jwtValidator could return the type null which is incompatible with the type-hinted return League\OAuth2\Server\Aut...s\JwtValidatorInterface. Consider adding an additional type-check to rule them out.
Loading history...
43
    }
44
45
    /**
46
     * @return array{0:non-empty-string, 1:array<non-empty-string, mixed>}|array{0:null, 1:null}
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{0:non-empty-string...}|array{0:null, 1:null} at position 4 could not be parsed: Unknown type name 'non-empty-string' at position 4 in array{0:non-empty-string, 1:array<non-empty-string, mixed>}|array{0:null, 1:null}.
Loading history...
47
     *
48
     * @throws OAuthServerException
49
     */
50
    protected function validateToken(
51
        ServerRequestInterface $request,
52
        ClientEntityInterface $client
53
    ): array {
54
        $token = $this->getRequestParameter('token', $request)
55
            ?? throw OAuthServerException::invalidRequest('token');
56
57
        $tokenTypeHint = $this->getRequestParameter('token_type_hint', $request, 'access_token');
58
59
        if ($tokenTypeHint === 'refresh_token') {
60
            return $this->validateRefreshToken($request, $token, $client)
61
                ?? $this->validateAccessToken($request, $token, $client)
62
                ?? [null, null];
63
        }
64
65
        return $this->validateAccessToken($request, $token, $client)
66
            ?? $this->validateRefreshToken($request, $token, $client)
67
            ?? [null, null];
68
    }
69
70
    /**
71
     * @return array{0:non-empty-string, 1:array<non-empty-string, mixed>}|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{0:non-empty-string...ty-string, mixed>}|null at position 4 could not be parsed: Unknown type name 'non-empty-string' at position 4 in array{0:non-empty-string, 1:array<non-empty-string, mixed>}|null.
Loading history...
72
     */
73
    protected function validateRefreshToken(
74
        ServerRequestInterface $request,
75
        string $refreshToken,
76
        ClientEntityInterface $client
77
    ): ?array {
78
        try {
79
            return [
80
                'refresh_token',
81
                $this->validateEncryptedRefreshToken($request, $refreshToken, $client->getIdentifier()),
82
            ];
83
        } catch (Throwable) {
84
            return null;
85
        }
86
    }
87
88
    /**
89
     * @param non-empty-string $accessToken
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
90
     *
91
     * @return array{0:non-empty-string, 1:array<non-empty-string, mixed>}|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{0:non-empty-string...ty-string, mixed>}|null at position 4 could not be parsed: Unknown type name 'non-empty-string' at position 4 in array{0:non-empty-string, 1:array<non-empty-string, mixed>}|null.
Loading history...
92
     */
93
    protected function validateAccessToken(
94
        ServerRequestInterface $request,
95
        string $accessToken,
96
        ClientEntityInterface $client
97
    ): ?array {
98
        try {
99
            return [
100
                'access_token',
101
                $this->getJwtValidator()->validateJwt($request, $accessToken, $client->getIdentifier()),
102
            ];
103
        } catch (Throwable) {
104
            return null;
105
        }
106
    }
107
}
108