1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace League\OAuth2\Server; |
6
|
|
|
|
7
|
|
|
use League\OAuth2\Server\AuthorizationValidators\AuthorizationValidatorInterface; |
8
|
|
|
use League\OAuth2\Server\AuthorizationValidators\BearerTokenValidator as AuthorizationBearerTokenValidator; |
9
|
|
|
use League\OAuth2\Server\IntrospectionValidators\BearerTokenValidator as IntrospectionBearerTokenValidator; |
10
|
|
|
use League\OAuth2\Server\IntrospectionValidators\IntrospectionValidatorInterface; |
11
|
|
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; |
12
|
|
|
use League\OAuth2\Server\ResponseTypes\Introspection\AbstractResponseType; |
13
|
|
|
use League\OAuth2\Server\ResponseTypes\Introspection\BearerTokenResponse; |
14
|
|
|
use League\OAuth2\Server\ResponseTypes\Introspection\ResponseTypeInterface; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
17
|
|
|
|
18
|
|
|
class IntrospectionServer |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var AccessTokenRepositoryInterface |
22
|
|
|
*/ |
23
|
|
|
protected $accessTokenRepository; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var CryptKey |
27
|
|
|
*/ |
28
|
|
|
protected $publicKey; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var AbstractResponseType |
32
|
|
|
*/ |
33
|
|
|
protected $responseType; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var AuthorizationValidatorInterface|null |
37
|
|
|
*/ |
38
|
|
|
protected $authorizationValidator; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var IntrospectionValidatorInterface|null |
42
|
|
|
*/ |
43
|
|
|
protected $introspectionValidator; |
44
|
|
|
|
45
|
|
|
public function __construct( |
46
|
|
|
AccessTokenRepositoryInterface $accessTokenRepository, |
47
|
|
|
$publicKey, |
48
|
|
|
IntrospectionValidatorInterface $introspectionValidator = null, |
49
|
|
|
AuthorizationValidatorInterface $authorizationValidator = null, |
50
|
|
|
ResponseTypeInterface $responseType = null |
51
|
|
|
) { |
52
|
|
|
$this->accessTokenRepository = $accessTokenRepository; |
53
|
|
|
|
54
|
|
|
if ($publicKey instanceof CryptKey === false) { |
55
|
|
|
$publicKey = new CryptKey($publicKey); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->publicKey = $publicKey; |
59
|
|
|
$this->introspectionValidator = $introspectionValidator; |
60
|
|
|
$this->authorizationValidator = $authorizationValidator; |
61
|
|
|
|
62
|
|
|
if ($responseType === null) { |
63
|
|
|
$this->responseType = new BearerTokenResponse(); |
64
|
|
|
} else { |
65
|
|
|
$this->responseType = clone $responseType; |
|
|
|
|
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the introspection validator |
71
|
|
|
* |
72
|
|
|
* @return IntrospectionValidatorInterface |
73
|
|
|
*/ |
74
|
|
|
protected function getIntrospectionValidator(): IntrospectionValidatorInterface |
75
|
|
|
{ |
76
|
|
|
if ($this->introspectionValidator instanceof IntrospectionValidatorInterface === false) { |
77
|
|
|
$this->introspectionValidator = new IntrospectionBearerTokenValidator($this->accessTokenRepository); |
78
|
|
|
|
79
|
|
|
$this->introspectionValidator->setPublicKey($this->publicKey); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->introspectionValidator; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the authorization validator |
87
|
|
|
* |
88
|
|
|
* @return AuthorizationValidatorInterface |
89
|
|
|
*/ |
90
|
|
|
protected function getAuthorizationValidator(): AuthorizationValidatorInterface |
91
|
|
|
{ |
92
|
|
|
if ($this->authorizationValidator instanceof AuthorizationValidatorInterface === false) { |
93
|
|
|
$this->authorizationValidator = new AuthorizationBearerTokenValidator($this->accessTokenRepository); |
94
|
|
|
|
95
|
|
|
$this->authorizationValidator->setPublicKey($this->publicKey); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->authorizationValidator; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Return an introspection response. |
103
|
|
|
* |
104
|
|
|
* @param ServerRequestInterface $request |
105
|
|
|
* @param ResponseInterface $response |
106
|
|
|
* |
107
|
|
|
* @return ResponseInterface |
108
|
|
|
* |
109
|
|
|
* @throws Exception\OAuthServerException |
110
|
|
|
*/ |
111
|
|
|
public function respondToIntrospectionRequest(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface |
112
|
|
|
{ |
113
|
|
|
$this->getAuthorizationValidator()->validateAuthorization($request); |
114
|
|
|
|
115
|
|
|
$this->responseType->setRequest($request); |
116
|
|
|
$this->responseType->setValidity( |
117
|
|
|
$this->getIntrospectionValidator()->validateIntrospection($request) |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
return $this->responseType->generateHttpResponse($response); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.