Completed
Push — master ( 4b6ba5...ada8d2 )
by Alex
62:19 queued 27:21
created

BearerTokenResponse   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 60
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B generateHttpResponse() 0 41 2
A getExtraParams() 0 4 1
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
    public function generateHttpResponse(ResponseInterface $response)
24
    {
25
        $expireDateTime = $this->accessToken->getExpiryDateTime()->getTimestamp();
26
27
        $jwtAccessToken = $this->accessToken->convertToJWT($this->privateKey);
28
29
        $responseParams = [
30
            'token_type'   => 'Bearer',
31
            'expires_in'   => $expireDateTime - (new \DateTime())->getTimestamp(),
32
            'access_token' => (string) $jwtAccessToken,
33
        ];
34
35
        if ($this->refreshToken instanceof RefreshTokenEntityInterface) {
36
            $refreshToken = $this->encrypt(
37
                json_encode(
38
                    [
39
                        'client_id'        => $this->accessToken->getClient()->getIdentifier(),
40
                        'refresh_token_id' => $this->refreshToken->getIdentifier(),
41
                        'access_token_id'  => $this->accessToken->getIdentifier(),
42
                        'scopes'           => $this->accessToken->getScopes(),
43
                        'user_id'          => $this->accessToken->getUserIdentifier(),
44
                        'expire_time'      => $this->refreshToken->getExpiryDateTime()->getTimestamp(),
45
                    ]
46
                )
47
            );
48
49
            $responseParams['refresh_token'] = $refreshToken;
50
        }
51
52
        $responseParams = array_merge($this->getExtraParams($this->accessToken), $responseParams);
53
54
        $response = $response
55
            ->withStatus(200)
56
            ->withHeader('pragma', 'no-cache')
57
            ->withHeader('cache-control', 'no-store')
58
            ->withHeader('content-type', 'application/json; charset=UTF-8');
59
60
        $response->getBody()->write(json_encode($responseParams));
61
62
        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
     * @return array
72
     */
73
    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...
74
    {
75
        return [];
76
    }
77
}
78