Completed
Pull Request — master (#925)
by
unknown
33:06
created

IntrospectionResponse::invalidTokenResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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