|
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), $this->provider, $this->providerKey |
|
|
|
|
|
|
58
|
2 |
|
); |
|
59
|
2 |
|
} catch (AuthenticationException $e) { |
|
60
|
2 |
|
return $this->failureHandler->onAuthenticationFailure($request, $e); |
|
61
|
2 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
$refreshToken = $this->refreshTokenManager->get($preAuthenticatedToken->getCredentials()); |
|
64
|
|
|
|
|
65
|
2 |
|
if (null === $refreshToken || !$refreshToken->isValid()) { |
|
66
|
|
|
return $this->failureHandler->onAuthenticationFailure($request, new AuthenticationException( |
|
67
|
2 |
|
sprintf('Refresh token "%s" is invalid.', $refreshToken) |
|
68
|
1 |
|
) |
|
69
|
1 |
|
); |
|
70
|
1 |
|
} |
|
71
|
1 |
|
|
|
72
|
1 |
|
return $this->successHandler->onAuthenticationSuccess($request, $preAuthenticatedToken); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
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: