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 Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Authentication\AuthenticationSuccessHandler; |
16
|
|
|
use Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Authentication\AuthenticationFailureHandler; |
17
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
18
|
|
|
use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenManagerInterface; |
19
|
|
|
use Gesdinet\JWTRefreshTokenBundle\Security\Authenticator\RefreshTokenAuthenticator; |
20
|
|
|
use Gesdinet\JWTRefreshTokenBundle\Security\Provider\RefreshTokenProvider; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class RefreshToken. |
24
|
|
|
*/ |
25
|
|
|
class RefreshToken |
26
|
|
|
{ |
27
|
|
|
private $authenticator; |
28
|
|
|
private $provider; |
29
|
|
|
private $successHandler; |
30
|
|
|
private $failureHandler; |
31
|
|
|
private $refreshTokenManager; |
32
|
|
|
|
33
|
3 |
|
public function __construct(RefreshTokenAuthenticator $authenticator, RefreshTokenProvider $provider, AuthenticationSuccessHandler $successHandler, AuthenticationFailureHandler $failureHandler, RefreshTokenManagerInterface $refreshTokenManager, $ttl, $providerKey) |
34
|
|
|
{ |
35
|
3 |
|
$this->authenticator = $authenticator; |
36
|
3 |
|
$this->provider = $provider; |
37
|
3 |
|
$this->successHandler = $successHandler; |
38
|
3 |
|
$this->failureHandler = $failureHandler; |
39
|
3 |
|
$this->refreshTokenManager = $refreshTokenManager; |
40
|
3 |
|
$this->ttl = $ttl; |
|
|
|
|
41
|
3 |
|
$this->providerKey = $providerKey; |
|
|
|
|
42
|
3 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Refresh token. |
46
|
|
|
* |
47
|
|
|
* @param Request $request |
48
|
|
|
* |
49
|
|
|
* @return mixed |
50
|
|
|
* |
51
|
|
|
* @throws AuthenticationException |
52
|
|
|
*/ |
53
|
2 |
|
public function refresh(Request $request) |
54
|
|
|
{ |
55
|
|
|
try { |
56
|
2 |
|
$preAuthenticatedToken = $this->authenticator->authenticateToken( |
57
|
2 |
|
$this->authenticator->createToken($request, $this->providerKey), |
|
|
|
|
58
|
2 |
|
$this->provider, |
59
|
2 |
|
$this->providerKey |
|
|
|
|
60
|
2 |
|
); |
61
|
2 |
|
} catch (AuthenticationException $e) { |
62
|
|
|
return $this->failureHandler->onAuthenticationFailure($request, $e); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
$refreshToken = $this->refreshTokenManager->get($preAuthenticatedToken->getCredentials()); |
66
|
|
|
|
67
|
2 |
|
if (null === $refreshToken || !$refreshToken->isValid()) { |
68
|
1 |
|
return $this->failureHandler->onAuthenticationFailure($request, |
69
|
1 |
|
new AuthenticationException( |
70
|
1 |
|
sprintf('Refresh token "%s" is invalid.', $refreshToken) |
71
|
1 |
|
) |
72
|
1 |
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
$datetime = new \DateTime(); |
76
|
1 |
|
$datetime->modify('+'.$this->ttl.' seconds'); |
77
|
1 |
|
$refreshToken->setValid($datetime); |
78
|
|
|
|
79
|
1 |
|
$this->refreshTokenManager->save($refreshToken); |
80
|
|
|
|
81
|
1 |
|
return $this->successHandler->onAuthenticationSuccess($request, $preAuthenticatedToken); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: