44
55namespace League \OAuth2 \Server \ResponseTypes ;
66
7+ use DateTimeInterface ;
78use Psr \Http \Message \ResponseInterface ;
89
910class IntrospectionResponse implements IntrospectionResponseTypeInterface
1011{
11- private bool $ active = false ;
12+ protected bool $ active = false ;
1213
1314 /**
1415 * @var non-empty-string|null
1516 */
16- private ?string $ tokenType = null ;
17+ protected ?string $ tokenType = null ;
1718
1819 /**
1920 * @var array<non-empty-string, mixed>
2021 */
21- private ?array $ token = null ;
22+ protected ?array $ token = null ;
2223
2324 public function setActive (bool $ active ): void
2425 {
@@ -48,31 +49,11 @@ public function generateHttpResponse(ResponseInterface $response): ResponseInter
4849 ];
4950
5051 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 ));
52+ $ params = array_merge (
53+ $ params ,
54+ $ this ->parseParams ($ this ->tokenType , $ this ->token ),
55+ $ this ->getExtraParams ($ this ->tokenType , $ this ->token )
56+ );
7657 }
7758
7859 $ params = json_encode ($ params , flags: JSON_THROW_ON_ERROR );
@@ -88,6 +69,49 @@ public function generateHttpResponse(ResponseInterface $response): ResponseInter
8869 return $ response ;
8970 }
9071
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+ ]);
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+ ]);
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+
91115 /**
92116 * @param non-empty-string $tokenType
93117 * @param array<non-empty-string, mixed> $token
0 commit comments