BearerTokenResponse   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 93.55%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 27
c 1
b 1
f 0
dl 0
loc 58
ccs 29
cts 31
cp 0.9355
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getExtraParams() 0 5 1
A generateHttpResponse() 0 42 4
1
<?php
2
3
/**
4
 * OAuth 2.0 Bearer Token Response.
5
 *
6
 * @author      Alex Bilbie <[email protected]>
7
 * @copyright   Copyright (c) Alex Bilbie
8
 * @license     http://mit-license.org/
9
 *
10
 * @link        https://github.com/thephpleague/oauth2-server
11
 */
12
13
declare(strict_types=1);
14
15
namespace League\OAuth2\Server\ResponseTypes;
16
17
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
18
use LogicException;
19
use Psr\Http\Message\ResponseInterface;
20
use SensitiveParameter;
0 ignored issues
show
Bug introduced by
The type SensitiveParameter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
22
use function array_merge;
23
use function json_encode;
24
use function time;
25
26
class BearerTokenResponse extends AbstractResponseType
27
{
28 6
    public function generateHttpResponse(ResponseInterface $response): ResponseInterface
29
    {
30 6
        $expireDateTime = $this->accessToken->getExpiryDateTime()->getTimestamp();
31
32 6
        $responseParams = [
33 6
            'token_type'   => 'Bearer',
34 6
            'expires_in'   => $expireDateTime - time(),
35 6
            'access_token' => $this->accessToken->toString(),
36 6
        ];
37
38 6
        if (isset($this->refreshToken)) {
39 6
            $refreshTokenPayload = json_encode([
40 6
                    'client_id'        => $this->accessToken->getClient()->getIdentifier(),
41 6
                    'refresh_token_id' => $this->refreshToken->getIdentifier(),
42 6
                    'access_token_id'  => $this->accessToken->getIdentifier(),
43 6
                    'scopes'           => $this->accessToken->getScopes(),
44 6
                    'user_id'          => $this->accessToken->getUserIdentifier(),
45 6
                    'expire_time'      => $this->refreshToken->getExpiryDateTime()->getTimestamp(),
46 6
            ]);
47
48 6
            if ($refreshTokenPayload === false) {
49
                throw new LogicException('Error encountered JSON encoding the refresh token payload');
50
            }
51
52 6
            $responseParams['refresh_token'] = $this->encrypt($refreshTokenPayload);
53
        }
54
55 6
        $responseParams = json_encode(array_merge($this->getExtraParams($this->accessToken), $responseParams));
56
57 6
        if ($responseParams === false) {
58
            throw new LogicException('Error encountered JSON encoding response parameters');
59
        }
60
61 6
        $response = $response
62 6
            ->withStatus(200)
63 6
            ->withHeader('pragma', 'no-cache')
64 6
            ->withHeader('cache-control', 'no-store')
65 6
            ->withHeader('content-type', 'application/json; charset=UTF-8');
66
67 6
        $response->getBody()->write($responseParams);
68
69 6
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response returns the type Psr\Http\Message\MessageInterface which includes types incompatible with the type-hinted return Psr\Http\Message\ResponseInterface.
Loading history...
70
    }
71
72
    /**
73
     * Add custom fields to your Bearer Token response here, then override
74
     * AuthorizationServer::getResponseType() to pull in your version of
75
     * this class rather than the default.
76
     *
77
     * @return array<array-key,mixed>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key,mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key,mixed>.
Loading history...
78
     */
79 5
    protected function getExtraParams(
80
        #[SensitiveParameter]
0 ignored issues
show
Unused Code introduced by
The parameter $accessToken is not used and could be removed. ( Ignorable by Annotation )

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

80
        /** @scrutinizer ignore-unused */ #[SensitiveParameter]

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
        AccessTokenEntityInterface $accessToken
82
    ): array {
83 5
        return [];
84
    }
85
}
86