Completed
Pull Request — master (#925)
by
unknown
02:58
created

IntrospectionResponse::generateHttpResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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