-
Notifications
You must be signed in to change notification settings - Fork 3
/
RefreshTokenController.php
45 lines (37 loc) · 1.44 KB
/
RefreshTokenController.php
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
<?php
namespace NPR\One\Controllers;
use NPR\One\Models\AccessTokenModel;
/**
* This controller should always be used by every consumer, regardless of whether you are using the `authorization_code`
* or `device_code` grant types. Use this to generate a new access token when a previously-generated token has expired.
* The consumer of this codebase is responsible for setting up a router which forwards on the relevant requests
* to the {@see RefreshTokenController::generateNewAccessTokenFromRefreshToken()} public method in this class.
*
* @package NPR\One\Controllers
*/
class RefreshTokenController extends AbstractOAuth2Controller
{
/**
* Attempts to locate a refresh token securely stored in the cookies and, if found,
* generates a new access token.
*
* @api
* @return AccessTokenModel
* @throws \Exception
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function generateNewAccessTokenFromRefreshToken(): AccessTokenModel
{
$this->ensureExternalProvidersExist();
$refreshToken = $this->getSecureStorageProvider()->get('refresh_token');
if (empty($refreshToken))
{
throw new \Exception('Could not locate a refresh token');
}
$accessToken = $this->createAccessToken('refresh_token', [
'refresh_token' => $refreshToken
]);
$this->storeRefreshToken($accessToken);
return $accessToken;
}
}