* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Security\Core\Authentication\Provider; use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserCheckerInterface; use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\Exception\BadCredentialsException; use Symfony\Component\Security\Core\Exception\AuthenticationServiceException; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Role\SwitchUserRole; /** * UserProviderInterface retrieves users for UsernamePasswordToken tokens. * * @author Fabien Potencier */ abstract class UserAuthenticationProvider implements AuthenticationProviderInterface { private $hideUserNotFoundExceptions; private $userChecker; private $providerKey; /** * @param UserCheckerInterface $userChecker An UserCheckerInterface interface * @param string $providerKey A provider key * @param bool $hideUserNotFoundExceptions Whether to hide user not found exception or not * * @throws \InvalidArgumentException */ public function __construct(UserCheckerInterface $userChecker, $providerKey, $hideUserNotFoundExceptions = true) { if (empty($providerKey)) { throw new \InvalidArgumentException('$providerKey must not be empty.'); } $this->userChecker = $userChecker; $this->providerKey = $providerKey; $this->hideUserNotFoundExceptions = $hideUserNotFoundExceptions; } /** * {@inheritdoc} */ public function authenticate(TokenInterface $token) { if (!$this->supports($token)) { throw new AuthenticationException('The token is not supported by this authentication provider.'); } $username = $token->getUsername(); if ('' === $username || null === $username) { $username = AuthenticationProviderInterface::USERNAME_NONE_PROVIDED; } try { $user = $this->retrieveUser($username, $token); } catch (UsernameNotFoundException $e) { if ($this->hideUserNotFoundExceptions) { throw new BadCredentialsException('Bad credentials.', 0, $e); } $e->setUsername($username); throw $e; } if (!$user instanceof UserInterface) { throw new AuthenticationServiceException('retrieveUser() must return a UserInterface.'); } try { $this->userChecker->checkPreAuth($user); $this->checkAuthentication($user, $token); $this->userChecker->checkPostAuth($user); } catch (BadCredentialsException $e) { if ($this->hideUserNotFoundExceptions) { throw new BadCredentialsException('Bad credentials.', 0, $e); } throw $e; } $authenticatedToken = new UsernamePasswordToken($user, $token->getCredentials(), $this->providerKey, $this->getRoles($user, $token)); $authenticatedToken->setAttributes($token->getAttributes()); return $authenticatedToken; } /** * {@inheritdoc} */ public function supports(TokenInterface $token) { return $token instanceof UsernamePasswordToken && $this->providerKey === $token->getProviderKey(); } /** * Retrieves roles from user and appends SwitchUserRole if original token contained one. * * @return array The user roles */ private function getRoles(UserInterface $user, TokenInterface $token) { $roles = $user->getRoles(); foreach ($token->getRoles() as $role) { if ($role instanceof SwitchUserRole) { $roles[] = $role; break; } } return $roles; } /** * Retrieves the user from an implementation-specific location. * * @param string $username The username to retrieve * @param UsernamePasswordToken $token The Token * * @return UserInterface The user * * @throws AuthenticationException if the credentials could not be validated */ abstract protected function retrieveUser($username, UsernamePasswordToken $token); /** * Does additional checks on the user and token (like validating the * credentials). * * @throws AuthenticationException if the credentials could not be validated */ abstract protected function checkAuthentication(UserInterface $user, UsernamePasswordToken $token); } __halt_compiler();----SIGNATURE:----MfsYwlCUTCvrgA4zQtdZpIDNImqaH23xMQOMj0DAMWOHFwxugRutgM1hWm+FU4uRdwbbMMDds36yC4WCD7I48UUR/yA8dYwOHx0Hpsk7yNVqEPcH/jJ0jfCa4/B1pQ2BRVkcgWy0lwtH0SZgzkOE/pZA+wYasLx3+5Tr3EJ/Jp3DJytIl7OaovrVCGMVSQFyod+joN/T9DgOrqBIOLrRLssUPfY431Mzzjazfk/x1uTKmKuR88zI2/0MwtEuaNeBIJZfzwK+7/QVEgx1uohjuu0rox3pi2cNEnta9/Yb+M4Zog3OcQoR220oNO9DjqrPv/G8bJXQy7ppR1kbLrFGS3nbFdyMWE0DbG4LQoRy9K0boO2woHrHfYpoZ5aOkcVhLOfgwLl1qOGSlzN1gKD/j3BTfSgWrmtSCtHOHF6XUB8LXT4ojZ1A488iOCKm8VpBMHfMpEs9o1WPLQ/htKbODYgVHRpU6TU29LkDva9xrIz18KxQwb3Bj6N92bFD2z5nZZ22EnAZAWNtq2Bc1pxPP7yxc3IK8wI4rE785Xzglf82XO/OaoTOK1mKoq5hUYaGRztPoypKH2ftkAEO53ksAhw3I1eYpwst8hHxUcZ7LFsjcUYf0+WQMtZAzLYcH+f7gSpacSC3VXPuRgFo3S7zbR+hQZXISFn1uzYflfPqgg4=----ATTACHMENT:----NzkxNjY5MjA5MDI4MTM3NSA2OTc2NDY5NDc1MDI0MTk4IDkxNDM2ODEzNDQ0Mjg4NDc=