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