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

IntrospectionResponse   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 57.14%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 80
ccs 16
cts 28
cp 0.5714
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setToken() 0 4 1
A getExtraParams() 0 4 1
A hasToken() 0 4 1
A generateHttpResponse() 0 18 2
A getIntrospectionParams() 0 19 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 $token
19
     */
20 1
    public function setToken(Token $token)
21
    {
22 1
        $this->token = $token;
23 1
    }
24
25 5
    private function hasToken()
26
    {
27 5
        return $this->token !== null;
28
    }
29
    /**
30
     * Extract the introspection params from the token
31
     */
32 5
    public function getIntrospectionParams()
33
    {
34 5
        if (!$this->hasToken()) {
35
            return [
36 4
                'active' => false,
37
            ];
38
        }
39
40
        return [
41 1
            'active' => true,
42 1
            'token_type' => 'access_token',
43 1
            'scope' => $this->token->getClaim('scopes', ''),
44 1
            'client_id' => $this->token->getClaim('aud'),
45 1
            'exp' => $this->token->getClaim('exp'),
46 1
            'iat' => $this->token->getClaim('iat'),
47 1
            'sub' => $this->token->getClaim('sub'),
48 1
            'jti' => $this->token->getClaim('jti'),
49
        ];
50
    }
51
52
    /**
53
     * @param ResponseInterface $response
54
     *
55
     * @return ResponseInterface
56
     */
57
    public function generateHttpResponse(ResponseInterface $response)
58
    {
59
        $responseParams = $this->getIntrospectionParams();
60
61
        if ($this->hasToken()) {
62
            $responseParams = array_merge($this->getExtraParams(), $responseParams);
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