Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Authenticator extends AbstractGuardAuthenticator
- {
- private $encoderFactory;
- private $userRepository;
- private $tokenStorage;
- private $router;
- public function __construct(EncoderFactoryInterface $encoderFactory, UserRepositoryInterface $userRepository, TokenStorageInterface $tokenStorage, Router $router)
- {
- $this->encoderFactory = $encoderFactory;
- $this->userRepository = $userRepository;
- $this->tokenStorage = $tokenStorage;
- $this->router = $router;
- }
- /**
- * Called on every request. Return whatever credentials you want,
- * or null to stop authentication.
- */
- public function getCredentials(Request $request)
- {
- $encoder = $this->encoderFactory->getEncoder(new User());
- $displayname = $request->request->get('_username');
- $password = $request->request->get('_password');
- $users = $this->userRepository->findByDisplayname($displayname);
- if ($users !== []) {
- foreach ($users as $user) {
- if ($encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt())) {
- return ['username' => $user->getUsername(), 'password' => $user->getPassword()];
- }
- }
- } else {
- if ($this->tokenStorage->getToken() !== null) {
- $user = $this->tokenStorage->getToken()->getUser();
- return ['username' => $user->getUsername(), 'password' => $user->getPassword()];
- }
- }
- return null;
- }
- public function getUser($credentials, UserProviderInterface $userProvider)
- {
- if ($credentials !== null) {
- return $userProvider->loadUserByUsername($credentials["username"]);
- }
- return null;
- }
- public function checkCredentials($credentials, UserInterface $user)
- {
- if ($user !== null) {
- return true;
- } else {
- return false;
- }
- }
- public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
- {
- return null;
- }
- public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
- {
- $exclusions = ['/login'];
- if (!in_array($request->getPathInfo(), $exclusions)) {
- $request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);
- throw $exception;
- }
- }
- /**
- * Called when authentication is needed, but it's not sent
- */
- public function start(Request $request, AuthenticationException $authException = null)
- {
- $data = array(
- // you might translate this message
- 'message' => 'Authentication Required'
- );
- return new JsonResponse($data, Response::HTTP_UNAUTHORIZED);
- }
- public function supportsRememberMe()
- {
- return false;
- }
- }
- class CustomAuthListener extends UsernamePasswordFormAuthenticationListener
- {
- private $csrfTokenManager;
- private $tokenStorage;
- public function __construct(TokenStorageInterface $tokenStorage, AuthenticationManagerInterface $authenticationManager, SessionAuthenticationStrategyInterface $sessionStrategy, HttpUtils $httpUtils, $providerKey, AuthenticationSuccessHandlerInterface $successHandler, AuthenticationFailureHandlerInterface $failureHandler, array $options = array(), LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null, CsrfTokenManagerInterface $csrfTokenManager = null)
- {
- parent::__construct($tokenStorage, $authenticationManager, $sessionStrategy, $httpUtils, $providerKey, $successHandler, $failureHandler, array_merge(array(
- 'username_parameter' => '_username',
- 'password_parameter' => '_password',
- 'csrf_parameter' => '_csrf_token',
- 'csrf_token_id' => 'authenticate',
- 'post_only' => true,
- ), $options), $logger, $dispatcher);
- $this->csrfTokenManager = $csrfTokenManager;
- $this->tokenStorage = $tokenStorage;
- }
- /**
- * {@inheritdoc}
- */
- protected function attemptAuthentication(Request $request)
- {
- if ($user = $this->tokenStorage->getToken() !== null) {
- $user = $this->tokenStorage->getToken()->getUser();
- $username = $user->getUsername();
- if ($this->options['post_only']) {
- $password = ParameterBagUtils::getParameterBagValue($request->request, $this->options['password_parameter']);
- } else {
- $password = ParameterBagUtils::getRequestParameterValue($request, $this->options['password_parameter']);
- }
- if (strlen($username) > Security::MAX_USERNAME_LENGTH) {
- throw new BadCredentialsException('Invalid username.');
- }
- $request->getSession()->set(Security::LAST_USERNAME, $username);
- return $this->authenticationManager->authenticate(new UsernamePasswordToken($username, $password, $this->providerKey));
- } else {
- return null;
- }
- }
- }
- <service id="security.authentication.listener.form" class="YourPathToCustomAuthListener" parent="security.authentication.listener.abstract" abstract="true" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement