1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\OAuth2\Server\ResponseTypes; |
4
|
|
|
|
5
|
|
|
use Lcobucci\JWT\Token; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
|
8
|
|
|
class IntrospectionResponse extends AbstractResponseType |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var Token |
12
|
|
|
*/ |
13
|
|
|
protected $token; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Set the token against the response |
17
|
|
|
* |
18
|
|
|
* @param Token $token |
19
|
|
|
*/ |
20
|
3 |
|
public function setToken(Token $token) |
21
|
|
|
{ |
22
|
3 |
|
$this->token = $token; |
23
|
3 |
|
} |
24
|
|
|
|
25
|
8 |
|
private function hasToken() |
26
|
|
|
{ |
27
|
8 |
|
return $this->token !== null; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
3 |
|
private function validTokenResponse() |
34
|
|
|
{ |
35
|
|
|
$responseParams = [ |
36
|
3 |
|
'active' => true, |
37
|
3 |
|
'token_type' => 'access_token', |
38
|
3 |
|
'scope' => $this->token->getClaim('scopes', ''), |
39
|
3 |
|
'client_id' => $this->token->getClaim('aud'), |
40
|
3 |
|
'exp' => $this->token->getClaim('exp'), |
41
|
3 |
|
'iat' => $this->token->getClaim('iat'), |
42
|
3 |
|
'sub' => $this->token->getClaim('sub'), |
43
|
3 |
|
'jti' => $this->token->getClaim('jti'), |
44
|
|
|
]; |
45
|
|
|
|
46
|
3 |
|
return array_merge($this->getExtraParams(), $responseParams); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
5 |
|
private function invalidTokenResponse() |
53
|
|
|
{ |
54
|
|
|
return [ |
55
|
5 |
|
'active' => false, |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Extract the introspection params from the token |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
8 |
|
public function getIntrospectionParams() |
65
|
|
|
{ |
66
|
8 |
|
return $this->hasToken() ? |
67
|
3 |
|
$this->validTokenResponse() : |
68
|
8 |
|
$this->invalidTokenResponse(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param ResponseInterface $response |
73
|
|
|
* |
74
|
|
|
* @return ResponseInterface |
75
|
|
|
*/ |
76
|
2 |
|
public function generateHttpResponse(ResponseInterface $response) |
77
|
|
|
{ |
78
|
2 |
|
$responseParams = $this->getIntrospectionParams(); |
79
|
|
|
|
80
|
|
|
$response = $response |
81
|
2 |
|
->withStatus(200) |
82
|
2 |
|
->withHeader('pragma', 'no-cache') |
83
|
2 |
|
->withHeader('cache-control', 'no-store') |
84
|
2 |
|
->withHeader('content-type', 'application/json; charset=UTF-8'); |
85
|
|
|
|
86
|
2 |
|
$response->getBody()->write(json_encode($responseParams)); |
87
|
|
|
|
88
|
2 |
|
return $response; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Add custom fields to your Introspection response here, then set your introspection |
93
|
|
|
* reponse in AuthorizationServer::setIntrospectionResponseType() to pull in your version of |
94
|
|
|
* this class rather than the default. |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
2 |
|
protected function getExtraParams() |
99
|
|
|
{ |
100
|
2 |
|
return []; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|