1 | <?php |
||
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 | public function setToken(Token $token) |
||
21 | { |
||
22 | $this->token = $token; |
||
23 | } |
||
24 | |||
25 | 1 | private function hasToken() |
|
26 | { |
||
27 | 1 | return $this->token !== null; |
|
28 | } |
||
29 | /** |
||
30 | * Extract the introspection params from the token |
||
31 | */ |
||
32 | 1 | public function getValidIntrospectionParams() |
|
33 | { |
||
34 | 1 | if (!$this->hasToken()) { |
|
35 | return [ |
||
36 | 1 | 'active' => false |
|
37 | ]; |
||
38 | } |
||
39 | |||
40 | return [ |
||
41 | 'active' => true, |
||
42 | 'token_type' => 'access_token', |
||
43 | 'scope' => $this->token->getClaim('scopes', ''), |
||
44 | 'client_id' => $this->token->getClaim('aud'), |
||
45 | 'exp' => $this->token->getClaim('exp'), |
||
46 | 'iat' => $this->token->getClaim('iat'), |
||
47 | 'sub' => $this->token->getClaim('sub'), |
||
48 | 'jti' => $this->token->getClaim('jti'), |
||
49 | ]; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param ResponseInterface $response |
||
54 | * |
||
55 | * @return ResponseInterface |
||
56 | */ |
||
57 | public function generateHttpResponse(ResponseInterface $response) |
||
58 | { |
||
59 | $responseParams = $this->getValidIntrospectionParams(); |
||
60 | |||
61 | if ($this->hasToken()) { |
||
62 | $responseParams = array_merge($this->getExtraParams(), $responseParams); |
||
63 | } |
||
64 | |||
65 | $response = $response |
||
66 | ->withStatus(200) |
||
67 | ->withHeader('pragma', 'no-cache') |
||
68 | ->withHeader('cache-control', 'no-store') |
||
69 | ->withHeader('content-type', 'application/json; charset=UTF-8'); |
||
70 | |||
71 | $response->getBody()->write(json_encode($responseParams)); |
||
72 | |||
73 | return $response; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Add custom fields to your Introspection response here, then set your introspection |
||
78 | * reponse in AuthorizationServer::setIntrospectionResponseType() to pull in your version of |
||
79 | * this class rather than the default. |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | protected function getExtraParams() |
||
87 | } |
||
88 |