1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace App\Services\Auth; |
4
|
|
|
|
5
|
|
|
use App\Services\Auth\Contracts\TokenRefresher; |
6
|
|
|
use App\Services\Auth\Contracts\TokenStorage; |
7
|
|
|
use Lcobucci\JWT\Token; |
8
|
|
|
use Lcobucci\JWT\Parser; |
9
|
|
|
use Jumbojett\OpenIDConnectClient; |
10
|
|
|
use Jumbojett\OpenIDConnectClientException; |
11
|
|
|
use App\Exceptions\Auth\TokenRequestException; |
12
|
|
|
use App\Exceptions\Auth\TokenStorageException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Adapted from the OpenIDConnect Laravel package at |
16
|
|
|
* https://github.com/furdarius/oidconnect-laravel |
17
|
|
|
*/ |
|
|
|
|
18
|
|
|
class JumboJettTokenRefresher implements TokenRefresher { |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Used for saving and fetching refresh tokens. |
22
|
|
|
* @var TokenStorage |
|
|
|
|
23
|
|
|
*/ |
24
|
|
|
protected $tokenStorage; |
25
|
|
|
|
26
|
|
|
/** |
|
|
|
|
27
|
|
|
* |
28
|
|
|
* @var Parser |
29
|
|
|
*/ |
30
|
|
|
protected $parser; |
31
|
|
|
|
32
|
|
|
/** |
|
|
|
|
33
|
|
|
* |
34
|
|
|
* @var OpenIDConnectClient |
35
|
|
|
*/ |
36
|
|
|
protected $connectClient; |
37
|
|
|
|
38
|
|
|
public function __construct(TokenStorage $tokenStorage, Parser $parser, |
|
|
|
|
39
|
|
|
string $authUrl, string $clientId, string $clientSecret) { |
|
|
|
|
40
|
|
|
$this->tokenStorage = $tokenStorage; |
41
|
|
|
$this->parser = $parser; |
42
|
|
|
$this->connectClient = new OpenIDConnectClient($authUrl, $clientId, $clientSecret); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
|
|
|
|
46
|
|
|
* |
47
|
|
|
* @param string $sub |
|
|
|
|
48
|
|
|
* @param string $iss |
|
|
|
|
49
|
|
|
* @return Token $idToken |
|
|
|
|
50
|
|
|
*/ |
51
|
|
|
public function refreshIDToken(string $iss, string $sub): \Lcobucci\JWT\Token { |
52
|
|
|
$refreshToken = $this->tokenStorage->fetchRefresh($iss, $sub); |
53
|
|
|
if (!$refreshToken) { |
54
|
|
|
throw new TokenStorageException("Failed to fetch refresh token"); |
55
|
|
|
} |
56
|
|
|
try { |
57
|
|
|
$response = $this->connectClient->refreshToken($refreshToken); |
58
|
|
|
if (isset($response->error)) { |
59
|
|
|
//Delete refresh token if it failed |
60
|
|
|
$this->tokenStorage->forgetRefresh($iss, $sub); |
61
|
|
|
throw new TokenRequestException($response->error); |
62
|
|
|
} |
63
|
|
|
} catch (OpenIDConnectClientException $exception) { |
64
|
|
|
//Delete refresh token if it failed |
65
|
|
|
$this->tokenStorage->forgetRefresh($iss, $sub); |
66
|
|
|
throw new TokenRequestException($exception->getMessage()); |
67
|
|
|
} catch (\Exception $exception) { |
68
|
|
|
//Delete refresh token if it failed |
69
|
|
|
$this->tokenStorage->forgetRefresh($iss, $sub); |
70
|
|
|
throw new TokenRequestException($exception->getMessage()); |
71
|
|
|
} |
72
|
|
|
if (!$this->tokenStorage->saveRefresh($iss, $sub, $response->refresh_token)) { |
73
|
|
|
throw new TokenStorageException("Failed to store refresh token"); |
74
|
|
|
} |
75
|
|
|
//Save new access token we received as well |
76
|
|
|
$this->tokenStorage->saveAccess($iss, $sub, $response->access_token); |
77
|
|
|
return $this->parser->parse($response->id_token); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|