1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace League\OAuth2\Server\ResponseTypes; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
|
9
|
|
|
class IntrospectionResponse implements IntrospectionResponseTypeInterface |
10
|
|
|
{ |
11
|
|
|
private bool $active = false; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var non-empty-string|null |
|
|
|
|
15
|
|
|
*/ |
16
|
|
|
private ?string $tokenType = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array<non-empty-string, mixed> |
|
|
|
|
20
|
|
|
*/ |
21
|
|
|
private ?array $token = null; |
22
|
|
|
|
23
|
|
|
public function setActive(bool $active): void |
24
|
|
|
{ |
25
|
|
|
$this->active = $active; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function setTokenType(string $tokenType): void |
32
|
|
|
{ |
33
|
|
|
$this->tokenType = $tokenType; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function setToken(array $token): void |
40
|
|
|
{ |
41
|
|
|
$this->token = $token; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function generateHttpResponse(ResponseInterface $response): ResponseInterface |
45
|
|
|
{ |
46
|
|
|
$params = [ |
47
|
|
|
'active' => $this->active, |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
if ($this->active === true && $this->tokenType !== null && $this->token !== null) { |
51
|
|
|
if ($this->tokenType === 'access_token') { |
52
|
|
|
$params = array_merge($params, array_filter([ |
53
|
|
|
'scope' => $this->token['scope'] ?? implode(' ', $this->token['scopes'] ?? []), |
54
|
|
|
'client_id' => $this->token['client_id'] ?? $this->token['aud'][0] ?? null, |
55
|
|
|
'username' => $this->token['username'] ?? null, |
56
|
|
|
'token_type' => 'Bearer', |
57
|
|
|
'exp' => $this->token['exp'] ?? null, |
58
|
|
|
'iat' => $this->token['iat'] ?? null, |
59
|
|
|
'nbf' => $this->token['nbf'] ?? null, |
60
|
|
|
'sub' => $this->token['sub'] ?? null, |
61
|
|
|
'aud' => $this->token['aud'] ?? null, |
62
|
|
|
'iss' => $this->token['iss'] ?? null, |
63
|
|
|
'jti' => $this->token['jti'] ?? null, |
64
|
|
|
])); |
65
|
|
|
} elseif ($this->tokenType === 'refresh_token') { |
66
|
|
|
$params = array_merge($params, array_filter([ |
67
|
|
|
'scope' => implode(' ', $this->token['scopes'] ?? []), |
68
|
|
|
'client_id' => $this->token['client_id'] ?? null, |
69
|
|
|
'exp' => $this->token['expire_time'] ?? null, |
70
|
|
|
'sub' => $this->token['user_id'] ?? null, |
71
|
|
|
'jti' => $this->token['refresh_token_id'] ?? null, |
72
|
|
|
])); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$params = array_merge($params, $this->getExtraParams($this->tokenType, $this->token)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$params = json_encode($params, flags: JSON_THROW_ON_ERROR); |
79
|
|
|
|
80
|
|
|
$response = $response |
81
|
|
|
->withStatus(200) |
82
|
|
|
->withHeader('pragma', 'no-cache') |
83
|
|
|
->withHeader('cache-control', 'no-store') |
84
|
|
|
->withHeader('content-type', 'application/json; charset=UTF-8'); |
85
|
|
|
|
86
|
|
|
$response->getBody()->write($params); |
87
|
|
|
|
88
|
|
|
return $response; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param non-empty-string $tokenType |
|
|
|
|
93
|
|
|
* @param array<non-empty-string, mixed> $token |
94
|
|
|
* |
95
|
|
|
* @return array<non-empty-string, mixed> |
|
|
|
|
96
|
|
|
*/ |
97
|
|
|
protected function getExtraParams(string $tokenType, array $token): array |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
return []; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|