Passed
Pull Request — master (#1473)
by
unknown
55:04 queued 20:02
created

AbstractTokenHandler   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 32
c 1
b 0
f 0
dl 0
loc 89
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A validateToken() 0 18 2
A setJwtValidator() 0 3 1
A validateRefreshToken() 0 12 2
A setPublicKey() 0 3 1
A getJwtValidator() 0 11 3
A validateAccessToken() 0 12 2
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
15
abstract class AbstractTokenHandler extends AbstractHandler implements TokenHandlerInterface
16
{
17
    private CryptKeyInterface $publicKey;
18
19
    private ?JwtValidatorInterface $jwtValidator = null;
20
21
    public function setPublicKey(CryptKeyInterface $publicKey): void
22
    {
23
        $this->publicKey = $publicKey;
24
    }
25
26
    public function setJwtValidator(JwtValidatorInterface $jwtValidator): void
27
    {
28
        $this->jwtValidator = $jwtValidator;
29
    }
30
31
    protected function getJwtValidator(): JwtValidatorInterface
32
    {
33
        if ($this->jwtValidator instanceof JwtValidatorInterface === false) {
34
            $this->jwtValidator = new BearerTokenValidator($this->accessTokenRepository);
35
        }
36
37
        if ($this->jwtValidator instanceof BearerTokenValidator === true) {
38
            $this->jwtValidator->setPublicKey($this->publicKey);
0 ignored issues
show
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

38
            $this->jwtValidator->/** @scrutinizer ignore-call */ 
39
                                 setPublicKey($this->publicKey);
Loading history...
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

38
            $this->jwtValidator->/** @scrutinizer ignore-call */ 
39
                                 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...
39
        }
40
41
        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...
42
    }
43
44
    /**
45
     * @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...
46
     *
47
     * @throws OAuthServerException
48
     */
49
    protected function validateToken(
50
        ServerRequestInterface $request,
51
        ClientEntityInterface $client
52
    ): array {
53
        $token = $this->getRequestParameter('token', $request)
54
            ?? throw OAuthServerException::invalidRequest('token');
55
56
        $tokenTypeHint = $this->getRequestParameter('token_type_hint', $request, 'access_token');
57
58
        if ($tokenTypeHint === 'refresh_token') {
59
            return $this->validateRefreshToken($request, $token, $client)
60
                ?? $this->validateAccessToken($request, $token, $client)
61
                ?? [null, null];
62
        }
63
64
        return $this->validateAccessToken($request, $token, $client)
65
            ?? $this->validateRefreshToken($request, $token, $client)
66
            ?? [null, null];
67
    }
68
69
    /**
70
     * @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...
71
     */
72
    protected function validateRefreshToken(
73
        ServerRequestInterface $request,
74
        string $refreshToken,
75
        ClientEntityInterface $client
76
    ): ?array {
77
        try {
78
            return [
79
                'refresh_token',
80
                $this->validateEncryptedRefreshToken($request, $refreshToken, $client->getIdentifier()),
81
            ];
82
        } catch (OAuthServerException) {
83
            return null;
84
        }
85
    }
86
87
    /**
88
     * @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...
89
     *
90
     * @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...
91
     */
92
    protected function validateAccessToken(
93
        ServerRequestInterface $request,
94
        string $accessToken,
95
        ClientEntityInterface $client
96
    ): ?array {
97
        try {
98
            return [
99
                'access_token',
100
                $this->getJwtValidator()->validateJwt($request, $accessToken, $client->getIdentifier()),
101
            ];
102
        } catch (OAuthServerException) {
103
            return null;
104
        }
105
    }
106
}
107