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

IntrospectionResponse::validTokenResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.7666
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
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 3
    public function setToken(Token $token)
21
    {
22 3
        $this->token = $token;
23 3
    }
24
25
    /**
26
     * Return wether the token has been set
27
     *
28
     * @return bool
29
     */
30 8
    private function hasToken()
31
    {
32 8
        return $this->token !== null;
33
    }
34
35
    /**
36
     * @return array
37
     */
38 3
    private function validTokenResponse()
39
    {
40
        $responseParams = [
41 3
            'active' => true,
42 3
            'token_type' => 'access_token',
43 3
            'scope' => $this->token->getClaim('scopes', ''),
44 3
            'client_id' => $this->token->getClaim('aud'),
45 3
            'exp' => $this->token->getClaim('exp'),
46 3
            'iat' => $this->token->getClaim('iat'),
47 3
            'sub' => $this->token->getClaim('sub'),
48 3
            'jti' => $this->token->getClaim('jti'),
49
        ];
50
51 3
        return array_merge($this->getExtraParams(), $responseParams);
52
    }
53
54
    /**
55
     * @return array
56
     */
57 5
    private function invalidTokenResponse()
58
    {
59
        return [
60 5
            'active' => false,
61
        ];
62
    }
63
64
    /**
65
     * Extract the introspection params from the token
66
     *
67
     * @return array
68
     */
69 8
    public function getIntrospectionParams()
70
    {
71 8
        return $this->hasToken() ?
72 3
            $this->validTokenResponse() :
73 8
            $this->invalidTokenResponse();
74
    }
75
76
    /**
77
     * @param ResponseInterface $response
78
     *
79
     * @return ResponseInterface
80
     */
81 2
    public function generateHttpResponse(ResponseInterface $response)
82
    {
83 2
        $responseParams = $this->getIntrospectionParams();
84
85
        $response = $response
86 2
                ->withStatus(200)
87 2
                ->withHeader('pragma', 'no-cache')
88 2
                ->withHeader('cache-control', 'no-store')
89 2
                ->withHeader('content-type', 'application/json; charset=UTF-8');
90
91 2
        $response->getBody()->write(json_encode($responseParams));
92
93 2
        return $response;
94
    }
95
96
    /**
97
     * Add custom fields to your Introspection response here, then set your introspection
98
     * reponse in AuthorizationServer::setIntrospectionResponseType() to pull in your version of
99
     * this class rather than the default.
100
     *
101
     * @return array
102
     */
103 2
    protected function getExtraParams()
104
    {
105 2
        return [];
106
    }
107
}
108