Passed
Pull Request — master (#1473)
by
unknown
31:14
created

AbstractTokenHandler   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 33
c 2
b 0
f 1
dl 0
loc 93
rs 10
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A validateRefreshToken() 0 12 2
A setPublicKey() 0 3 1
A setBearerTokenValidator() 0 3 1
A getBearerTokenValidator() 0 11 3
A validateToken() 0 18 2
A validateAccessToken() 0 16 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\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{type: non-empty-string, data: array<non-empty-string, mixed>}|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{type: non-empty-st...ty-string, mixed>}|null at position 4 could not be parsed: Unknown type name 'non-empty-string' at position 4 in array{type: non-empty-string, data: array<non-empty-string, mixed>}|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
        }
65
66
        return $this->validateAccessToken($request, $token, $client)
67
            ?? $this->validateRefreshToken($request, $token, $client);
68
    }
69
70
    /**
71
     * @return array{type: non-empty-string, data: array<non-empty-string, mixed>}|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{type: non-empty-st...ty-string, mixed>}|null at position 4 could not be parsed: Unknown type name 'non-empty-string' at position 4 in array{type: non-empty-string, data: array<non-empty-string, mixed>}|null.
Loading history...
72
     */
73
    private function validateRefreshToken(
74
        ServerRequestInterface $request,
75
        string $refreshToken,
76
        ClientEntityInterface $client
77
    ): ?array {
78
        try {
79
            return [
80
                'type' => 'refresh_token',
81
                'data' => $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{type: non-empty-string, data: array<non-empty-string, mixed>}|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{type: non-empty-st...ty-string, mixed>}|null at position 4 could not be parsed: Unknown type name 'non-empty-string' at position 4 in array{type: non-empty-string, data: array<non-empty-string, mixed>}|null.
Loading history...
92
     */
93
    private function validateAccessToken(
94
        ServerRequestInterface $request,
95
        string $accessToken,
96
        ClientEntityInterface $client
97
    ): ?array {
98
        try {
99
            return [
100
                'type' => 'access_token',
101
                'data' => $this->getBearerTokenValidator()->validateBearerToken(
102
                    $request,
103
                    $accessToken,
104
                    $client->getIdentifier()
105
                ),
106
            ];
107
        } catch (Throwable) {
108
            return null;
109
        }
110
    }
111
}
112