Completed
Pull Request — master (#925)
by
unknown
01:42
created

IntrospectionResponse::getExtraParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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