Completed
Push — master ( 661e1f...1f20a4 )
by Andrew
18s queued 10s
created

src/ResponseTypes/BearerTokenResponse.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
     * {@inheritdoc}
23
     */
24 5
    public function generateHttpResponse(ResponseInterface $response)
25
    {
26 5
        $expireDateTime = $this->accessToken->getExpiryDateTime()->getTimestamp();
27
28
        $responseParams = [
29 5
            'token_type'   => 'Bearer',
30 5
            'expires_in'   => $expireDateTime - \time(),
31 5
            'access_token' => (string) $this->accessToken,
32
        ];
33
34 5
        if ($this->refreshToken instanceof RefreshTokenEntityInterface) {
35 5
            $refreshTokenPayload = json_encode([
36 5
                'client_id'        => $this->accessToken->getClient()->getIdentifier(),
37 5
                'refresh_token_id' => $this->refreshToken->getIdentifier(),
38 5
                'access_token_id'  => $this->accessToken->getIdentifier(),
39 5
                'scopes'           => $this->accessToken->getScopes(),
40 5
                'user_id'          => $this->accessToken->getUserIdentifier(),
41 5
                'expire_time'      => $this->refreshToken->getExpiryDateTime()->getTimestamp(),
42
            ]);
43
44 5
            if ($refreshTokenPayload === false) {
45
                throw new LogicException('Error encountered JSON encoding the refresh token payload');
46
            }
47
48 5
            $responseParams['refresh_token'] = $this->encrypt($refreshTokenPayload);
49
        }
50
51 5
        $responseParams = json_encode(array_merge($this->getExtraParams($this->accessToken), $responseParams));
52
53 5
        if ($responseParams === false) {
54
            throw new LogicException('Error encountered JSON encoding response parameters');
55
        }
56
57
        $response = $response
58 5
            ->withStatus(200)
59 5
            ->withHeader('pragma', 'no-cache')
60 5
            ->withHeader('cache-control', 'no-store')
61 5
            ->withHeader('content-type', 'application/json; charset=UTF-8');
62
63 5
        $response->getBody()->write($responseParams);
64
65 5
        return $response;
66
    }
67
68
    /**
69
     * Add custom fields to your Bearer Token response here, then override
70
     * AuthorizationServer::getResponseType() to pull in your version of
71
     * this class rather than the default.
72
     *
73
     * @param AccessTokenEntityInterface $accessToken
74
     *
75
     * @return array
76
     */
77 4
    protected function getExtraParams(AccessTokenEntityInterface $accessToken)
0 ignored issues
show
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...
78
    {
79 4
        return [];
80
    }
81
}
82