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
|
1 |
|
} 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 string |
41
|
|
|
*/ |
42
|
|
|
protected $property; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $property |
46
|
|
|
* |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
|
|
public function setProperty($property) |
50
|
|
|
{ |
51
|
|
|
$this->property = $property; |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function createToken(Request $request, $providerKey) |
57
|
|
|
{ |
58
|
|
|
$refreshTokenString = RequestRefreshToken::getRefreshToken($request, $this->property); |
59
|
|
|
|
60
|
|
|
return new PreAuthenticatedToken( |
61
|
|
|
'', |
62
|
|
|
$refreshTokenString, |
63
|
|
|
$providerKey |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey) |
68
|
|
|
{ |
69
|
|
|
if (!$userProvider instanceof RefreshTokenProvider) { |
70
|
|
|
throw new \InvalidArgumentException( |
71
|
|
|
sprintf( |
72
|
|
|
'The user provider must be an instance of RefreshTokenProvider (%s was given).', |
73
|
|
|
get_class($userProvider) |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$refreshToken = $token->getCredentials(); |
79
|
|
|
$username = $userProvider->getUsernameForRefreshToken($refreshToken); |
80
|
1 |
|
|
81
|
|
|
if (!$username) { |
82
|
1 |
|
throw new AuthenticationException( |
83
|
|
|
sprintf('Refresh token "%s" does not exist.', $refreshToken) |
84
|
|
|
); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
1 |
|
$user = $userProvider->loadUserByUsername($username); |
88
|
|
|
|
89
|
|
|
return new PreAuthenticatedToken( |
90
|
|
|
$user, |
91
|
|
|
$refreshToken, |
92
|
|
|
$providerKey, |
93
|
|
|
$user->getRoles() |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function supportsToken(TokenInterface $token, $providerKey) |
98
|
|
|
{ |
99
|
|
|
return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) |
103
|
|
|
{ |
104
|
|
|
return new Response('Refresh token authentication failed.', 403); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
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.