Completed
Pull Request — master (#910)
by Andrew
01:49
created

BearerTokenResponse::generateHttpResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 41
ccs 23
cts 23
cp 1
rs 9.264
cc 2
nc 2
nop 1
crap 2
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 Psr\Http\Message\ResponseInterface;
17
18
class BearerTokenResponse extends AbstractResponseType
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23 5
    public function generateHttpResponse(ResponseInterface $response)
24
    {
25 5
        $expireDateTime = $this->accessToken->getExpiryDateTime()->getTimestamp();
26
27 5
        $jwtAccessToken = $this->accessToken->convertToJWT($this->privateKey);
28
29
        $responseParams = [
30 5
            'token_type'   => 'Bearer',
31 5
            'expires_in'   => $expireDateTime - (new \DateTime())->getTimestamp(),
32 5
            'access_token' => (string) $jwtAccessToken,
33
        ];
34
35 5
        if ($this->refreshToken instanceof RefreshTokenEntityInterface) {
36 5
            $refreshToken = $this->encrypt(
37 5
                json_encode(
38
                    [
39 5
                        'client_id'        => $this->accessToken->getClient()->getIdentifier(),
40 5
                        'refresh_token_id' => $this->refreshToken->getIdentifier(),
41 5
                        'access_token_id'  => $this->accessToken->getIdentifier(),
42 5
                        'scopes'           => $this->accessToken->getScopes(),
43 5
                        'user_id'          => $this->accessToken->getUserIdentifier(),
44 5
                        'expire_time'      => $this->refreshToken->getExpiryDateTime()->getTimestamp(),
45
                    ]
46
                )
47
            );
48
49 5
            $responseParams['refresh_token'] = $refreshToken;
50
        }
51
52 5
        $responseParams = array_merge($this->getExtraParams($this->accessToken), $responseParams);
53
54
        $response = $response
55 5
            ->withStatus(200)
56 5
            ->withHeader('pragma', 'no-cache')
57 5
            ->withHeader('cache-control', 'no-store')
58 5
            ->withHeader('content-type', 'application/json; charset=UTF-8');
59
60 5
        $response->getBody()->write(json_encode($responseParams));
61
62 5
        return $response;
63
    }
64
65
    /**
66
     * Add custom fields to your Bearer Token response here, then override
67
     * AuthorizationServer::getResponseType() to pull in your version of
68
     * this class rather than the default.
69
     *
70
     * @param AccessTokenEntityInterface $accessToken
71
     *
72
     * @return array
73
     */
74 4
    protected function getExtraParams(AccessTokenEntityInterface $accessToken)
0 ignored issues
show
Unused Code introduced by
The parameter $accessToken is not used and could be removed.

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

Loading history...
75
    {
76 4
        return [];
77
    }
78
}
79