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 Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
15
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
16
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
19
|
|
|
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; |
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
21
|
|
|
use Gesdinet\JWTRefreshTokenBundle\Security\Provider\RefreshTokenProvider; |
22
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
23
|
|
|
use Gesdinet\JWTRefreshTokenBundle\Event\GetTokenRequestEvent; |
24
|
1 |
|
use Gesdinet\JWTRefreshTokenBundle\Events; |
25
|
|
|
|
26
|
|
|
if (interface_exists('Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface')) { |
27
|
|
|
abstract class RefreshTokenAuthenticatorBase implements \Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface |
28
|
1 |
|
{ |
29
|
|
|
} |
30
|
|
|
} else { |
31
|
|
|
abstract class RefreshTokenAuthenticatorBase implements \Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Class RefreshTokenAuthenticator. |
38
|
|
|
*/ |
39
|
|
|
class RefreshTokenAuthenticator extends RefreshTokenAuthenticatorBase implements AuthenticationFailureHandlerInterface |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
public function __construct(EventDispatcherInterface $dispatcher){ |
42
|
|
|
$this->dispatcher = $dispatcher; |
|
|
|
|
43
|
|
|
} |
44
|
|
|
public function createToken(Request $request, $providerKey) |
45
|
|
|
{ |
46
|
|
|
$event = new GetTokenRequestEvent($request); |
47
|
|
|
$this->dispatcher->dispatch(Events::GET_TOKEN_REQUEST, $event); |
48
|
|
|
$refreshTokenString = $event->getToken(); |
49
|
|
|
|
50
|
|
|
return new PreAuthenticatedToken( |
51
|
|
|
'', |
52
|
|
|
$refreshTokenString, |
53
|
|
|
$providerKey |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey) |
58
|
|
|
{ |
59
|
|
|
if (!$userProvider instanceof RefreshTokenProvider) { |
60
|
|
|
throw new \InvalidArgumentException( |
61
|
|
|
sprintf( |
62
|
|
|
'The user provider must be an instance of RefreshTokenProvider (%s was given).', |
63
|
|
|
get_class($userProvider) |
64
|
|
|
) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$refreshToken = $token->getCredentials(); |
69
|
|
|
$username = $userProvider->getUsernameForRefreshToken($refreshToken); |
70
|
|
|
|
71
|
|
|
if (!$username) { |
72
|
|
|
throw new AuthenticationException( |
73
|
|
|
sprintf('Refresh token "%s" does not exist.', $refreshToken) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$user = $userProvider->loadUserByUsername($username); |
78
|
|
|
|
79
|
|
|
return new PreAuthenticatedToken( |
80
|
1 |
|
$user, |
81
|
|
|
$refreshToken, |
82
|
1 |
|
$providerKey, |
83
|
|
|
$user->getRoles() |
84
|
|
|
); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
1 |
|
public function supportsToken(TokenInterface $token, $providerKey) |
88
|
|
|
{ |
89
|
|
|
return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) |
93
|
|
|
{ |
94
|
|
|
return new Response('Refresh token authentication failed.', 403); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
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.