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 League\OAuth2\Server\Entities\ScopeEntityInterface; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
|
19
|
|
|
class BearerTokenResponse extends AbstractResponseType |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
public function generateHttpResponse(ResponseInterface $response) |
25
|
|
|
{ |
26
|
|
|
$expireDateTime = $this->accessToken->getExpiryDateTime()->getTimestamp(); |
27
|
|
|
|
28
|
|
|
$jwtAccessToken = $this->accessToken->convertToJWT($this->privateKey); |
29
|
|
|
|
30
|
|
|
$responseParams = [ |
31
|
|
|
'token_type' => 'Bearer', |
32
|
|
|
'expires_in' => $expireDateTime - (new \DateTime())->getTimestamp(), |
33
|
|
|
'access_token' => (string) $jwtAccessToken, |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
if ($this->returnScopes === true) { |
37
|
|
|
$responseParams['scope'] = implode(" ", array_map( |
38
|
|
|
function (ScopeEntityInterface $scopeEntity) { |
39
|
|
|
return $scopeEntity->getIdentifier(); |
40
|
|
|
}, $this->accessToken->getScopes() |
41
|
|
|
)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ($this->refreshToken instanceof RefreshTokenEntityInterface) { |
45
|
|
|
$refreshToken = $this->encrypt( |
46
|
|
|
json_encode( |
47
|
|
|
[ |
48
|
|
|
'client_id' => $this->accessToken->getClient()->getIdentifier(), |
49
|
|
|
'refresh_token_id' => $this->refreshToken->getIdentifier(), |
50
|
|
|
'access_token_id' => $this->accessToken->getIdentifier(), |
51
|
|
|
'scopes' => $this->accessToken->getScopes(), |
52
|
|
|
'user_id' => $this->accessToken->getUserIdentifier(), |
53
|
|
|
'expire_time' => $this->refreshToken->getExpiryDateTime()->getTimestamp(), |
54
|
|
|
] |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$responseParams['refresh_token'] = $refreshToken; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$responseParams = array_merge($this->getExtraParams($this->accessToken), $responseParams); |
62
|
|
|
|
63
|
|
|
$response = $response |
64
|
|
|
->withStatus(200) |
65
|
|
|
->withHeader('pragma', 'no-cache') |
66
|
|
|
->withHeader('cache-control', 'no-store') |
67
|
|
|
->withHeader('content-type', 'application/json; charset=UTF-8'); |
68
|
|
|
|
69
|
|
|
$response->getBody()->write(json_encode($responseParams)); |
70
|
|
|
|
71
|
|
|
return $response; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Add custom fields to your Bearer Token response here, then override |
76
|
|
|
* AuthorizationServer::getResponseType() to pull in your version of |
77
|
|
|
* this class rather than the default. |
78
|
|
|
* |
79
|
|
|
* @param AccessTokenEntityInterface $accessToken |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
protected function getExtraParams(AccessTokenEntityInterface $accessToken) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
return []; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.