forked from bshaffer/oauth2-server-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRefreshToken.php
More file actions
85 lines (70 loc) · 2.67 KB
/
Copy pathRefreshToken.php
File metadata and controls
85 lines (70 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
*
*/
class OAuth2_GrantType_RefreshToken implements OAuth2_GrantTypeInterface, OAuth2_Response_ProviderInterface
{
private $storage;
private $response;
private $config;
private $oldRefreshToken;
public function __construct(OAuth2_Storage_RefreshTokenInterface $storage, $config = array())
{
$this->config = array_merge(array(
'always_issue_new_refresh_token' => false
), $config);
$this->storage = $storage;
}
public function getQuerystringIdentifier()
{
return 'refresh_token';
}
public function validateRequest($request)
{
if (!$request->request("refresh_token")) {
$this->response = new OAuth2_Response_Error(400, 'invalid_request', 'Missing parameter: "refresh_token" is required');
return false;
}
return true;
}
public function getTokenDataFromRequest($request)
{
if (!$stored = $this->storage->getRefreshToken($request->request("refresh_token"))) {
$this->response = new OAuth2_Response_Error(400, 'invalid_grant', 'Invalid refresh token');
return false;
}
return $stored;
}
public function validateTokenData($tokenData, array $clientData)
{
if ($tokenData === null || $clientData['client_id'] != $tokenData["client_id"]) {
$this->response = new OAuth2_Response_Error(400, 'invalid_grant', 'Invalid refresh token');
return false;
}
if ($tokenData["expires"] < time()) {
$this->response = new OAuth2_Response_Error(400, 'invalid_grant', 'Refresh token has expired');
return false;
}
// store the refresh token locally so we can delete it when a new refresh token is generated
$this->oldRefreshToken = $tokenData["refresh_token"];
return true;
}
public function createAccessToken(OAuth2_ResponseType_AccessTokenInterface $accessToken, array $clientData, array $tokenData)
{
/*
* It is optional to force a new refresh token when a refresh token is used.
* However, if a new refresh token is issued, the old one MUST be expired
* @see http://tools.ietf.org/html/rfc6749#section-6
*/
$issueNewRefreshToken = $this->config['always_issue_new_refresh_token'];
$token = $accessToken->createAccessToken($clientData['client_id'], $tokenData['user_id'], $tokenData['scope'], $issueNewRefreshToken);
if ($issueNewRefreshToken) {
$this->storage->unsetRefreshToken($this->oldRefreshToken);
}
return $token;
}
public function getResponse()
{
return $this->response;
}
}