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