| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace PiedWeb\CMSBundle\Security; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Doctrine\ORM\EntityManagerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use PiedWeb\CMSBundle\Entity\UserInterface as User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\HttpFoundation\RedirectResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Security\Core\Security; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Security\Core\User\UserInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Security\Core\User\UserProviderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Security\Csrf\CsrfToken; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\Security\Guard\PasswordAuthenticatedInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Component\Security\Http\Util\TargetPathTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | class UserAuthenticator extends AbstractFormLoginAuthenticator implements PasswordAuthenticatedInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     use TargetPathTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $entityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private $urlGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $csrfTokenManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $passwordEncoder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         EntityManagerInterface $entityManager, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         UrlGeneratorInterface $urlGenerator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         CsrfTokenManagerInterface $csrfTokenManager, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         UserPasswordEncoderInterface $passwordEncoder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->entityManager = $entityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->urlGenerator = $urlGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->csrfTokenManager = $csrfTokenManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->passwordEncoder = $passwordEncoder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function supports(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         return 'piedweb_cms_login' === $request->attributes->get('_route') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             && $request->isMethod('POST'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public function getCredentials(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $credentials = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             'email' => $request->request->get('email'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             'password' => $request->request->get('password'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             'csrf_token' => $request->request->get('_csrf_token'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $request->getSession()->set( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             Security::LAST_USERNAME, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $credentials['email'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         return $credentials; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function getUser($credentials, UserProviderInterface $userProvider) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $token = new CsrfToken('authenticate', $credentials['csrf_token']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if (! $this->csrfTokenManager->isTokenValid($token)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             throw new InvalidCsrfTokenException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $credentials['email']]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         if (! $user) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             // fail authentication with a custom error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             throw new CustomUserMessageAuthenticationException('security.login.email_not_found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return $user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     public function checkCredentials($credentials, UserInterface $user) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         return $this->passwordEncoder->isPasswordValid($user, $credentials['password']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * Used to upgrade (rehash) the user's password automatically over time. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function getPassword($credentials): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         return $credentials['password']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |     public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |             return new RedirectResponse($targetPath); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         // todo redirect to previous route | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         return new RedirectResponse($this->urlGenerator->generate('piedweb_cms_admin_dashboard')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     protected function getLoginUrl() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return $this->urlGenerator->generate('piedweb_cms_login'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 109 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |  |