Passed
Pull Request — master (#1121)
by
unknown
06:56
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
     * {@inheritdoc}
39
     */
40 5
    public function generateHttpResponse(ResponseInterface $response)
41
    {
42 5
        $expireDateTime = $this->accessToken->getExpiryDateTime()->getTimestamp();
43
44
        $responseParams = [
45 5
            'token_type'   => 'Bearer',
46 5
            'expires_in'   => $expireDateTime - \time(),
47 5
            'access_token' => (string) $this->accessToken,
48
        ];
49
50 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...
51 5
            $refreshTokenPayload = $this->getRefreshTokenPayload();
52
53 5
            if ($refreshTokenPayload === false) {
54
                throw new LogicException('Error encountered JSON encoding the refresh token payload');
55
            }
56
57 5
            $responseParams['refresh_token'] = $this->encrypt($refreshTokenPayload);
58
        }
59
60 5
        $responseParams = \json_encode(\array_merge($this->getExtraParams($this->accessToken), $responseParams));
61
62 5
        if ($responseParams === false) {
63
            throw new LogicException('Error encountered JSON encoding response parameters');
64
        }
65
66
        $response = $response
67 5
            ->withStatus(200)
68 5
            ->withHeader('pragma', 'no-cache')
69 5
            ->withHeader('cache-control', 'no-store')
70 5
            ->withHeader('content-type', 'application/json; charset=UTF-8');
71
72 5
        $response->getBody()->write($responseParams);
73
74 5
        return $response;
75
    }
76
77
    /**
78
     * Add custom fields to your Bearer Token response here, then override
79
     * AuthorizationServer::getResponseType() to pull in your version of
80
     * this class rather than the default.
81
     *
82
     * @param AccessTokenEntityInterface $accessToken
83
     *
84
     * @return array
85
     */
86 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

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