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