1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\OAuth2\Server\ResponseTypes; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
7
|
|
|
|
8
|
|
|
class IntrospectionResponse extends AbstractResponseType |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var bool |
12
|
|
|
*/ |
13
|
|
|
protected $valid = false; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var ServerRequestInterface |
17
|
|
|
*/ |
18
|
|
|
protected $request; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param bool $bool |
22
|
|
|
*/ |
23
|
3 |
|
public function setValidity(bool $bool) |
24
|
|
|
{ |
25
|
3 |
|
$this->valid = $bool; |
26
|
3 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param ServerRequestInterface $request |
30
|
|
|
*/ |
31
|
1 |
|
public function setRequest(ServerRequestInterface $request) |
32
|
|
|
{ |
33
|
1 |
|
$this->request = $request; |
34
|
1 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
1 |
|
protected function validIntrospectionResponse() |
40
|
|
|
{ |
41
|
|
|
$responseParams = [ |
42
|
1 |
|
'active' => true, |
43
|
|
|
]; |
44
|
|
|
|
45
|
1 |
|
return array_merge($this->getExtraParams(), $responseParams); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
2 |
|
protected function invalidIntrospectionResponse() |
52
|
|
|
{ |
53
|
|
|
return [ |
54
|
2 |
|
'active' => false, |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Extract the introspection response |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
5 |
|
public function getIntrospectionResponseParams() |
64
|
|
|
{ |
65
|
5 |
|
return $this->isValid() ? |
66
|
3 |
|
$this->validIntrospectionResponse() : |
67
|
5 |
|
$this->invalidIntrospectionResponse(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
5 |
|
protected function isValid() |
74
|
|
|
{ |
75
|
5 |
|
return $this->valid === true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param ResponseInterface $response |
80
|
|
|
* |
81
|
|
|
* @return ResponseInterface |
82
|
|
|
*/ |
83
|
3 |
|
public function generateHttpResponse(ResponseInterface $response) |
84
|
|
|
{ |
85
|
3 |
|
$responseParams = $this->getIntrospectionResponseParams(); |
86
|
|
|
|
87
|
|
|
$response = $response |
88
|
3 |
|
->withStatus(200) |
89
|
3 |
|
->withHeader('pragma', 'no-cache') |
90
|
3 |
|
->withHeader('cache-control', 'no-store') |
91
|
3 |
|
->withHeader('content-type', 'application/json; charset=UTF-8'); |
92
|
|
|
|
93
|
3 |
|
$response->getBody()->write(json_encode($responseParams)); |
94
|
|
|
|
95
|
3 |
|
return $response; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Add custom fields to your Introspection response here, then set your introspection |
100
|
|
|
* reponse in AuthorizationServer::setIntrospectionResponseType() to pull in your version of |
101
|
|
|
* this class rather than the default. |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
2 |
|
protected function getExtraParams() |
106
|
|
|
{ |
107
|
2 |
|
return []; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|