Passed
Pull Request — master (#1121)
by
unknown
02:05
created

BearerTokenResponse::getRefreshTokenPayload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
ccs 8
cts 8
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * OAuth 2.0 Bearer Token Response.
4
 *
5
 * @author      Alex Bilbie <[email protected]>
6
 * @copyright   Copyright (c) Alex Bilbie
7
 * @license     http://mit-license.org/
8
 *
9
 * @link        https://github.com/thephpleague/oauth2-server
10
 */
11
12
namespace League\OAuth2\Server\ResponseTypes;
13
14
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
15
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
16
use LogicException;
17
use Psr\Http\Message\ResponseInterface;
18
19
class BearerTokenResponse extends AbstractResponseType
20
{
21
    /**
22
     * Construct the refresh token payload.
23
     *
24
     * @return false|string
25
     */
26 5
    protected function getRefreshTokenPayload()
27
    {
28 5
        return \json_encode([
29 5
            'client_id'        => $this->accessToken->getClient()->getIdentifier(),
30 5
            'refresh_token_id' => $this->refreshToken->getIdentifier(),
31 5
            'access_token_id'  => $this->accessToken->getIdentifier(),
32 5
            'scopes'           => $this->accessToken->getScopes(),
33 5
            'user_id'          => $this->accessToken->getUserIdentifier(),
34 5
            'expire_time'      => $this->refreshToken->getExpiryDateTime()->getTimestamp(),
35
        ]);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 5
    public function generateHttpResponse(ResponseInterface $response)
42
    {
43 5
        $expireDateTime = $this->accessToken->getExpiryDateTime()->getTimestamp();
44
45
        $responseParams = [
46 5
            'token_type'   => 'Bearer',
47 5
            'expires_in'   => $expireDateTime - \time(),
48 5
            'access_token' => (string) $this->accessToken,
49
        ];
50
51 5
        if ($this->refreshToken instanceof RefreshTokenEntityInterface) {
0 ignored issues
show
introduced by
$this->refreshToken is always a sub-type of League\OAuth2\Server\Ent...eshTokenEntityInterface.
Loading history...
52 5
            $refreshTokenPayload = $this->getRefreshTokenPayload();
53
54 5
            if ($refreshTokenPayload === false) {
55
                throw new LogicException('Error encountered JSON encoding the refresh token payload');
56
            }
57
58 5
            $responseParams['refresh_token'] = $this->encrypt($refreshTokenPayload);
59
        }
60
61 5
        $responseParams = \json_encode(\array_merge($this->getExtraParams($this->accessToken), $responseParams));
62
63 5
        if ($responseParams === false) {
64
            throw new LogicException('Error encountered JSON encoding response parameters');
65
        }
66
67
        $response = $response
68 5
            ->withStatus(200)
69 5
            ->withHeader('pragma', 'no-cache')
70 5
            ->withHeader('cache-control', 'no-store')
71 5
            ->withHeader('content-type', 'application/json; charset=UTF-8');
72
73 5
        $response->getBody()->write($responseParams);
74
75 5
        return $response;
76
    }
77
78
    /**
79
     * Add custom fields to your Bearer Token response here, then override
80
     * AuthorizationServer::getResponseType() to pull in your version of
81
     * this class rather than the default.
82
     *
83
     * @param AccessTokenEntityInterface $accessToken
84
     *
85
     * @return array
86
     */
87 4
    protected function getExtraParams(AccessTokenEntityInterface $accessToken)
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

87
    protected function getExtraParams(/** @scrutinizer ignore-unused */ AccessTokenEntityInterface $accessToken)

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...
88
    {
89 4
        return [];
90
    }
91
}
92