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\Security\Authenticator; |
13
|
|
|
|
14
|
|
|
use Gesdinet\JWTRefreshTokenBundle\Request\RequestRefreshToken; |
15
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
16
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
17
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
20
|
|
|
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; |
21
|
|
|
use Symfony\Component\HttpFoundation\Response; |
22
|
|
|
use Gesdinet\JWTRefreshTokenBundle\Security\Provider\RefreshTokenProvider; |
23
|
|
|
|
24
|
1 |
|
if (interface_exists('Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface')) { |
25
|
|
|
abstract class RefreshTokenAuthenticatorBase implements \Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface |
26
|
|
|
{ |
27
|
|
|
} |
28
|
|
|
} else { |
29
|
|
|
abstract class RefreshTokenAuthenticatorBase implements \Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Class RefreshTokenAuthenticator. |
36
|
|
|
*/ |
37
|
|
|
class RefreshTokenAuthenticator extends RefreshTokenAuthenticatorBase implements AuthenticationFailureHandlerInterface |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var RequestRefreshToken |
41
|
|
|
*/ |
42
|
|
|
private $requestRefreshToken; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Injects dependencies. |
46
|
|
|
* |
47
|
|
|
* @param RequestRefreshToken $requestRefreshToken |
48
|
|
|
*/ |
49
|
4 |
|
public function __construct(RequestRefreshToken $requestRefreshToken) |
50
|
|
|
{ |
51
|
4 |
|
$this->requestRefreshToken = $requestRefreshToken; |
52
|
4 |
|
} |
53
|
|
|
|
54
|
1 |
|
public function createToken(Request $request, $providerKey) |
55
|
|
|
{ |
56
|
1 |
|
$refreshTokenString = $this->requestRefreshToken->getRefreshToken($request); |
57
|
|
|
|
58
|
1 |
|
return new PreAuthenticatedToken( |
59
|
1 |
|
'', |
60
|
1 |
|
$refreshTokenString, |
61
|
1 |
|
$providerKey |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey) |
66
|
|
|
{ |
67
|
|
|
if (!$userProvider instanceof RefreshTokenProvider) { |
68
|
|
|
throw new \InvalidArgumentException( |
69
|
|
|
sprintf( |
70
|
|
|
'The user provider must be an instance of RefreshTokenProvider (%s was given).', |
71
|
|
|
get_class($userProvider) |
72
|
|
|
) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$refreshToken = $token->getCredentials(); |
77
|
|
|
$username = $userProvider->getUsernameForRefreshToken($refreshToken); |
78
|
|
|
|
79
|
|
|
if (!$username) { |
80
|
|
|
throw new AuthenticationException( |
81
|
|
|
sprintf('Refresh token "%s" does not exist.', $refreshToken) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$user = $userProvider->loadUserByUsername($username); |
86
|
|
|
|
87
|
|
|
return new PreAuthenticatedToken( |
88
|
|
|
$user, |
89
|
|
|
$refreshToken, |
90
|
|
|
$providerKey, |
91
|
|
|
$user->getRoles() |
|
|
|
|
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
public function supportsToken(TokenInterface $token, $providerKey) |
96
|
|
|
{ |
97
|
1 |
|
return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) |
101
|
|
|
{ |
102
|
1 |
|
return new Response('Refresh token authentication failed.', 403); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.