|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the GesdinetJWTRefreshTokenBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Gesdinet <http://www.gesdinet.com/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Gesdinet\JWTRefreshTokenBundle\Service; |
|
13
|
|
|
|
|
14
|
|
|
use Gesdinet\JWTRefreshTokenBundle\Event\RefreshEvent; |
|
15
|
|
|
use Gesdinet\JWTRefreshTokenBundle\Security\Authenticator\RefreshTokenAuthenticator; |
|
16
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
17
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface; |
|
18
|
|
|
use InvalidArgumentException; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
20
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
|
21
|
|
|
use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenManagerInterface; |
|
22
|
|
|
use Gesdinet\JWTRefreshTokenBundle\Security\Provider\RefreshTokenProvider; |
|
23
|
|
|
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; |
|
24
|
|
|
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Class RefreshToken. |
|
28
|
|
|
*/ |
|
29
|
|
|
class RefreshToken |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var RefreshTokenAuthenticator |
|
33
|
|
|
*/ |
|
34
|
|
|
private $authenticator; |
|
35
|
4 |
|
|
|
36
|
|
|
/** |
|
37
|
4 |
|
* @var RefreshTokenProvider |
|
38
|
4 |
|
*/ |
|
39
|
4 |
|
private $provider; |
|
40
|
4 |
|
|
|
41
|
4 |
|
/** |
|
42
|
4 |
|
* @var AuthenticationSuccessHandlerInterface |
|
43
|
4 |
|
*/ |
|
44
|
4 |
|
private $successHandler; |
|
45
|
4 |
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var AuthenticationFailureHandlerInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
private $failureHandler; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var RefreshTokenManagerInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
private $refreshTokenManager; |
|
55
|
|
|
|
|
56
|
3 |
|
/** |
|
57
|
|
|
* @var int |
|
58
|
|
|
*/ |
|
59
|
3 |
|
private $ttl; |
|
60
|
3 |
|
|
|
61
|
3 |
|
/** |
|
62
|
3 |
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
private $providerKey; |
|
65
|
|
|
|
|
66
|
3 |
|
/** |
|
67
|
|
|
* @var bool |
|
68
|
3 |
|
*/ |
|
69
|
1 |
|
private $ttlUpdate; |
|
70
|
1 |
|
|
|
71
|
1 |
|
/** |
|
72
|
1 |
|
* @var EventDispatcherInterface |
|
73
|
|
|
*/ |
|
74
|
|
|
private $eventDispatcher; |
|
75
|
2 |
|
|
|
76
|
1 |
|
/** |
|
77
|
1 |
|
* RefreshToken constructor. |
|
78
|
1 |
|
* |
|
79
|
|
|
* @param RefreshTokenAuthenticator $authenticator |
|
80
|
1 |
|
* @param RefreshTokenProvider $provider |
|
81
|
1 |
|
* @param AuthenticationSuccessHandlerInterface $successHandler |
|
82
|
|
|
* @param AuthenticationFailureHandlerInterface $failureHandler |
|
83
|
2 |
|
* @param RefreshTokenManagerInterface $refreshTokenManager |
|
84
|
|
|
* @param int $ttl |
|
85
|
|
|
* @param string $providerKey |
|
86
|
|
|
* @param bool $ttlUpdate |
|
87
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
88
|
|
|
*/ |
|
89
|
|
|
public function __construct( |
|
90
|
|
|
RefreshTokenAuthenticator $authenticator, |
|
91
|
|
|
RefreshTokenProvider $provider, |
|
92
|
|
|
AuthenticationSuccessHandlerInterface $successHandler, |
|
93
|
|
|
AuthenticationFailureHandlerInterface $failureHandler, |
|
94
|
|
|
RefreshTokenManagerInterface $refreshTokenManager, |
|
95
|
|
|
$ttl, |
|
96
|
|
|
$providerKey, |
|
97
|
|
|
$ttlUpdate, |
|
98
|
|
|
EventDispatcherInterface $eventDispatcher |
|
99
|
|
|
) { |
|
100
|
|
|
$this->authenticator = $authenticator; |
|
101
|
|
|
$this->provider = $provider; |
|
102
|
|
|
$this->successHandler = $successHandler; |
|
103
|
|
|
$this->failureHandler = $failureHandler; |
|
104
|
|
|
$this->refreshTokenManager = $refreshTokenManager; |
|
105
|
|
|
$this->ttl = $ttl; |
|
106
|
|
|
$this->providerKey = $providerKey; |
|
107
|
|
|
$this->ttlUpdate = $ttlUpdate; |
|
108
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Refresh token. |
|
113
|
|
|
* |
|
114
|
|
|
* @param Request $request |
|
115
|
|
|
* |
|
116
|
|
|
* @return mixed |
|
117
|
|
|
* |
|
118
|
|
|
* @throws InvalidArgumentException |
|
119
|
|
|
* @throws AuthenticationException |
|
120
|
|
|
*/ |
|
121
|
|
|
public function refresh(Request $request) |
|
122
|
|
|
{ |
|
123
|
|
|
try { |
|
124
|
|
|
$user = $this->authenticator->getUser( |
|
125
|
|
|
$this->authenticator->getCredentials($request), |
|
126
|
|
|
$this->provider |
|
127
|
|
|
); |
|
128
|
|
|
|
|
129
|
|
|
$postAuthenticationToken = $this->authenticator->createAuthenticatedToken($user, $this->providerKey); |
|
|
|
|
|
|
130
|
|
|
} catch (AuthenticationException $e) { |
|
131
|
|
|
return $this->failureHandler->onAuthenticationFailure($request, $e); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$refreshToken = $this->refreshTokenManager->get($this->authenticator->getCredentials($request)); |
|
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
if (null === $refreshToken || !$refreshToken->isValid()) { |
|
137
|
|
|
return $this->failureHandler->onAuthenticationFailure($request, new AuthenticationException( |
|
138
|
|
|
sprintf('Refresh token "%s" is invalid.', $refreshToken) |
|
139
|
|
|
) |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if ($this->ttlUpdate) { |
|
144
|
|
|
$expirationDate = new \DateTime(); |
|
145
|
|
|
$expirationDate->modify(sprintf('+%d seconds', $this->ttl)); |
|
146
|
|
|
$refreshToken->setValid($expirationDate); |
|
147
|
|
|
|
|
148
|
|
|
$this->refreshTokenManager->save($refreshToken); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if ($this->eventDispatcher instanceof ContractsEventDispatcherInterface) { |
|
|
|
|
|
|
152
|
|
|
$this->eventDispatcher->dispatch(new RefreshEvent($refreshToken, $postAuthenticationToken), 'gesdinet.refresh_token'); |
|
153
|
|
|
} else { |
|
154
|
|
|
$this->eventDispatcher->dispatch('gesdinet.refresh_token', new RefreshEvent($refreshToken, $postAuthenticationToken)); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $this->successHandler->onAuthenticationSuccess($request, $postAuthenticationToken); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: