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

getValidIntrospectionParams()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 12
cts 12
cp 1
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
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
        }
59
        else {
60
            $responseParams = [
61
                'active' => false,
62
            ];
63
        }
64
65
        $response = $response
66
                ->withStatus(200)
67
                ->withHeader('pragma', 'no-cache')
68
                ->withHeader('cache-control', 'no-store')
69
                ->withHeader('content-type', 'application/json; charset=UTF-8');
70
71
        $response->getBody()->write(json_encode($responseParams));
72
73
        return $response;
74
    }
75
76
    /**
77
     * Add custom fields to your Introspection response here, then set your introspection
78
     * reponse in AuthorizationServer::setIntrospectionResponseType() to pull in your version of
79
     * this class rather than the default.
80
     *
81
     * @return array
82
     */
83
    protected function getExtraParams()
84
    {
85
        return [];
86
    }
87
}
88