Passed
Pull Request — master (#1255)
by
unknown
56:50 queued 21:45
created

BearerTokenResponse::validIntrospectionResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 16
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\OAuth2\Server\ResponseTypes\Introspection;
6
7
use Lcobucci\JWT\Configuration;
8
use Lcobucci\JWT\Signer\Key\InMemory;
9
use Lcobucci\JWT\Signer\Rsa\Sha256;
10
use Lcobucci\JWT\Token;
11
use Lcobucci\JWT\UnencryptedToken;
12
13
class BearerTokenResponse extends AbstractResponseType
14
{
15
    /**
16
     * @var Configuration|null
17
     */
18
    private $jwtConfiguration;
19
20
    public function __construct()
21
    {
22
        $this->initJwtConfiguration();
23
    }
24
25
    /**
26
     * Initialise the JWT configuration.
27
     */
28
    private function initJwtConfiguration(): void
29
    {
30
        $this->jwtConfiguration = Configuration::forSymmetricSigner(new Sha256(), InMemory::empty());
31
    }
32
33
    /**
34
     * Add the token data to the response.
35
     *
36
     * @return array
37
     */
38
    protected function validIntrospectionResponse(): array
39
    {
40
        $token = $this->getTokenFromRequest();
41
42
        $responseParams = [
43
            'active' => true,
44
            'token_type' => 'access_token',
45
            'scope' => $this->getClaimFromToken($token, 'scopes', ''),
46
            'client_id' => $this->getClaimFromToken($token, 'aud'),
47
            'exp' => $this->getClaimFromToken($token, 'exp'),
48
            'iat' => $this->getClaimFromToken($token, 'iat'),
49
            'sub' => $this->getClaimFromToken($token, 'sub'),
50
            'jti' => $this->getClaimFromToken($token, 'jti'),
51
        ];
52
53
        return \array_merge($this->getExtraParams(), $responseParams);
54
    }
55
56
    /**
57
     * Gets the token from the request body.
58
     *
59
     * @return UnencryptedToken|Token
60
     */
61
    protected function getTokenFromRequest()
62
    {
63
        $jwt = $this->request->getParsedBody()['token'] ?? '';
64
65
        return $this->jwtConfiguration->parser()
0 ignored issues
show
Bug introduced by
The method parser() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
        return $this->jwtConfiguration->/** @scrutinizer ignore-call */ parser()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
            ->parse($jwt);
67
    }
68
69
    /**
70
     * Gets a single claim from the JWT token.
71
     *
72
     * @param UnencryptedToken|Token\Plain $token
73
     * @param string                       $claim
74
     * @param mixed|null                   $default
75
     *
76
     * @return mixed
77
     */
78
    protected function getClaimFromToken($token, string $claim, $default = null)
79
    {
80
        return $token->claims()->get($claim, $default);
81
    }
82
}
83