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

AbstractTokenHandler::validateToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
eloc 10
c 2
b 0
f 1
nc 2
nop 2
dl 0
loc 20
rs 9.9332
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\BearerTokenValidatorInterface;
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 ?BearerTokenValidatorInterface $bearerTokenValidator = null;
21
22
    public function setPublicKey(CryptKeyInterface $publicKey): void
23
    {
24
        $this->publicKey = $publicKey;
25
    }
26
27
    public function setBearerTokenValidator(BearerTokenValidatorInterface $bearerTokenValidator): void
28
    {
29
        $this->bearerTokenValidator = $bearerTokenValidator;
30
    }
31
32
    protected function getBearerTokenValidator(): BearerTokenValidatorInterface
33
    {
34
        if ($this->bearerTokenValidator instanceof BearerTokenValidatorInterface === false) {
35
            $this->bearerTokenValidator = new BearerTokenValidator($this->accessTokenRepository);
36
        }
37
38
        if ($this->bearerTokenValidator instanceof BearerTokenValidator === true) {
39
            $this->bearerTokenValidator->setPublicKey($this->publicKey);
0 ignored issues
show
Bug introduced by
The method setPublicKey() does not exist on League\OAuth2\Server\Aut...TokenValidatorInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to League\OAuth2\Server\Aut...TokenValidatorInterface. ( 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->bearerTokenValidator->/** @scrutinizer ignore-call */ 
40
                                         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

39
            $this->bearerTokenValidator->/** @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...
40
        }
41
42
        return $this->bearerTokenValidator;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->bearerTokenValidator could return the type null which is incompatible with the type-hinted return League\OAuth2\Server\Aut...TokenValidatorInterface. 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 the token cannot be located using the provided token type hint, we extend
60
        // the search across all supported token types according to the RFC spec.
61
        if ($tokenTypeHint === 'refresh_token') {
62
            return $this->validateRefreshToken($request, $token, $client)
63
                ?? $this->validateAccessToken($request, $token, $client)
64
                ?? [null, null];
65
        }
66
67
        return $this->validateAccessToken($request, $token, $client)
68
            ?? $this->validateRefreshToken($request, $token, $client)
69
            ?? [null, null];
70
    }
71
72
    /**
73
     * @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...
74
     */
75
    private function validateRefreshToken(
76
        ServerRequestInterface $request,
77
        string $refreshToken,
78
        ClientEntityInterface $client
79
    ): ?array {
80
        try {
81
            return [
82
                'refresh_token',
83
                $this->validateEncryptedRefreshToken($request, $refreshToken, $client->getIdentifier()),
84
            ];
85
        } catch (Throwable) {
86
            return null;
87
        }
88
    }
89
90
    /**
91
     * @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...
92
     *
93
     * @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...
94
     */
95
    private function validateAccessToken(
96
        ServerRequestInterface $request,
97
        string $accessToken,
98
        ClientEntityInterface $client
99
    ): ?array {
100
        try {
101
            return [
102
                'access_token',
103
                $this->getBearerTokenValidator()->validateBearerToken($request, $accessToken, $client->getIdentifier()),
104
            ];
105
        } catch (Throwable) {
106
            return null;
107
        }
108
    }
109
}
110