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

IntrospectionResponse   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 17.86%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getExtraParams() 0 4 1
A setToken() 0 4 1
A hasToken() 0 4 1
A getValidIntrospectionParams() 0 19 2
A generateHttpResponse() 0 18 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
    public function setToken(Token $token)
21
    {
22
        $this->token = $token;
23
    }
24
25 1
    private function hasToken()
26
    {
27 1
        return $this->token !== null;
28
    }
29
    /**
30
     * Extract the introspection params from the token
31
     */
32 1
    public function getValidIntrospectionParams()
33
    {
34 1
        if (!$this->hasToken()) {
35
            return [
36 1
                'active' => false
37
            ];
38
        }
39
40
        return [
41
            'active' => true,
42
            'token_type' => 'access_token',
43
            'scope' => $this->token->getClaim('scopes', ''),
44
            'client_id' => $this->token->getClaim('aud'),
45
            'exp' => $this->token->getClaim('exp'),
46
            'iat' => $this->token->getClaim('iat'),
47
            'sub' => $this->token->getClaim('sub'),
48
            '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->getValidIntrospectionParams();
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