/**
  * @param UserInterface $user
  */
 protected function authenticateUser(UserInterface $user)
 {
     $this->token = $this->createToken($user);
     $tokenStorage = $this->container->has('security.token_storage') ? $this->container->get('security.token_storage') : $this->container->get('security.context');
     $tokenStorage->setToken($this->token);
     $this->assertTrue($this->token->isAuthenticated());
 }
Пример #2
0
 public function authenticate(TokenInterface $token)
 {
     $user = $this->userProvider->loadUserByUsername($token->getUsername());
     if ($user && ($token->isAuthenticated() || $this->crowd->isauthenticationvalid($token->getUsername(), $token->getCredentials()))) {
         $authenticatedToken = new CrowdToken($user->getRoles());
         $authenticatedToken->setUser($user);
         return $authenticatedToken;
     }
     throw new AuthenticationException('The Crowd authentication failed.');
 }
Пример #3
0
 /**
  * @param TokenInterface $token
  *
  * @return array
  */
 public static function getAuthenticationResponse(TokenInterface $token)
 {
     $response = array('success' => false);
     if ($token->isAuthenticated() && $token->getUser() instanceof User) {
         /* @var User $user */
         $user = $token->getUser();
         $response = array('success' => true, 'profile' => self::userToArray($user));
     }
     return $response;
 }
 /**
  * @inheritdoc
  */
 public function logout(Request $request, Response $response, TokenInterface $token)
 {
     if ($token instanceof ReviveAuthenticationToken) {
         if ($token->isAuthenticated()) {
             $sessionId = $token->getSessionId();
             try {
                 $this->userSessionRepository->invalidateSession($sessionId);
             } catch (RepositoryInfrastructureException $ignored) {
             } catch (\InvalidArgumentException $ignored) {
             }
         }
     }
 }
Пример #5
0
 /**
  * {@inheritdoc}
  */
 public function vote(TokenInterface $token, $object, array $attributes)
 {
     $result = VoterInterface::ACCESS_ABSTAIN;
     foreach ($attributes as $attribute) {
         if (!$this->supportsAttribute($attribute)) {
             continue;
         }
         $result = VoterInterface::ACCESS_DENIED;
         if ((self::IS_AUTHENTICATED === $attribute or self::AUTH === $attribute) && $token->isAuthenticated()) {
             return VoterInterface::ACCESS_GRANTED;
         }
     }
     return $result;
 }
Пример #6
0
 /**
  * Function used for user authentication based on token object
  *
  * @param  \Symfony\Component\Security\Core\Authentication\Token\TokenInterface        $token
  * @param  \Symfony\Component\Security\Core\User\UserProviderInterface                 $userProvider
  * @param  type                                                                        $providerKey
  * @return \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken
  * @throws BadCredentialsException
  */
 public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
 {
     $passwordValid = false;
     // Loda user object
     try {
         $user = $userProvider->loadUserByUsername($token->getUsername());
     } catch (UsernameNotFoundException $e) {
         throw new BadCredentialsException('Invalid username or password', 0, $e);
     }
     // Check if ldap extension is enabled and user's ldap flag is set.
     if (null !== $this->ldapManager && $user->isLdapEnabled()) {
         try {
             $this->ldapManager->bind($token->getUsername(), $token->getCredentials());
             $passwordValid = (bool) $this->ldapManager->getBoundUser();
             if (null !== $this->logger && !$token->isAuthenticated()) {
                 $this->logger->info("[LdapAuthenticator] Ldap authentication successful.", array('user' => $this->ldapManager->getBoundUser()));
             }
         } catch (\Zend\Ldap\Exception\LdapException $e) {
             throw new BadCredentialsException('Invalid username or password', 0, $e);
         }
     } else {
         $currentUser = $token->getUser();
         if ($currentUser instanceof UserInterface) {
             if ($currentUser->getPassword() !== $user->getPassword()) {
                 throw new BadCredentialsException('The credentials were changed from another session.');
             } else {
                 $passwordValid = true;
             }
         } else {
             if ("" === ($presentedPassword = $token->getCredentials())) {
                 throw new BadCredentialsException('Invalid username or password.');
             }
             if (!($passwordValid = $this->encoderFactory->getEncoder($user)->isPasswordValid($user->getPassword(), $presentedPassword, $user->getSalt()))) {
                 throw new BadCredentialsException('Invalid username or password.');
             }
         }
         if (null !== $this->logger && !$token->isAuthenticated()) {
             $this->logger->info("[LdapAuthenticator] Local authentication successful.", array('user' => $user->getUsername()));
         }
     }
     // Set the authenticated token
     if ($passwordValid) {
         return new UsernamePasswordToken($user, $user->getPassword(), $providerKey, $user->getRoles());
     }
     throw new BadCredentialsException('Invalid username or password');
 }
Пример #7
0
 /**
  * @param                $service
  * @param TokenInterface $token
  */
 public function setToken($service, TokenInterface $token)
 {
     $this->token = $token;
     $this->authenticatingService = $service;
     $this->isAuthenticated = $token->isAuthenticated();
     $this->stopPropagation();
 }
Пример #8
0
 private function showMenu()
 {
     return $this->token && $this->token->isAuthenticated() and $this->acl->isGranted("ROLE_USER");
 }