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