作者:mickaelsteinber
项目:BackBe
/**
* Authenticate a token according to the user provider.
*
* @param \Symfony\Component\Security\Core\Authentication\Token\TokenInterface $token
*
* @return \Symfony\Component\Security\Core\User\UserProviderInterface
*
* @throws SecurityException Occures on invalid connection
*/
public function authenticate(TokenInterface $token)
{
if (!$this->supports($token)) {
return;
}
$username = $token->getUsername();
if (empty($username)) {
$username = 'NONE_PROVIDED';
}
$user = $this->_userProvider->loadUserByUsername($username);
if (false === is_array($user)) {
$user = array($user);
}
$authenticatedToken = false;
while (false === $authenticatedToken) {
if (null !== ($provider = array_pop($user))) {
$authenticatedToken = $this->authenticateUser($token, $provider);
} else {
break;
}
}
if (false === $authenticatedToken) {
throw new SecurityException('Invalid authentication informations', SecurityException::INVALID_CREDENTIALS);
}
return $authenticatedToken;
}
作者:athla
项目:revive-symfony-security-bundl
/**
* @inheritdoc
*/
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
{
try {
$user = $userProvider->loadUserByUsername($token->getUsername());
} catch (UsernameNotFoundException $e) {
throw new CustomUserMessageAuthenticationException('Invalid username or password');
}
$username = $token->getUsername();
$password = $token->getCredentials();
$sessionCreationResult = null;
try {
$sessionCreationResult = $this->userSessionRepository->createSessionIdByCredentials($username, $password);
} catch (\InvalidArgumentException $e) {
throw new CustomUserMessageAuthenticationException('Invalid username or password');
} catch (RepositoryInfrastructureException $e) {
throw new CustomUserMessageAuthenticationException('Cannot connect to Revive service');
}
$passwordValid = $sessionCreationResult !== null && UserSessionCreationAuthenticationResult::isSuccess($sessionCreationResult->getSessionCreationAuthenticationResult());
if ($passwordValid) {
$sessionId = $sessionCreationResult->getSessionId();
$roles = [];
$roles[] = 'USER';
if (UserSessionCreationAuthorizationSessionCreationResult::isSuccess($sessionCreationResult->getSessionCreationAuthorizationSessionCreation())) {
$roles[] = 'ADMIN';
}
$token = new ReviveAuthenticationToken($user, $sessionId, $providerKey, $roles);
return $token;
}
throw new CustomUserMessageAuthenticationException('Invalid username or password');
}
作者:hkmsh
项目:OpitHr
/**
* 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 string $providerKey
* @return \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken
* @throws BadCredentialsException
*/
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
{
$passwordValid = false;
// Load user object
try {
$user = $userProvider->loadUserByUsername($token->getUsername());
} catch (UsernameNotFoundException $e) {
throw new BadCredentialsException('Invalid username or password', 0, $e);
}
try {
$this->userChecker->checkPreAuth($user);
// Call the correct authentication method
if (null !== $this->ldapManager && $user->isLdapEnabled()) {
$passwordValid = $this->checkAuthenticationLdap($user, $token);
} else {
$passwordValid = $this->checkAuthentication($user, $token);
}
$this->userChecker->checkPostAuth($user);
} catch (BadCredentialsException $e) {
if ($this->hideUserNotFoundExceptions) {
throw new BadCredentialsException('Invalid username or password', 0, $e);
}
throw $e;
}
// Set the authenticated token
if ($passwordValid) {
return new UsernamePasswordToken($user, $user->getPassword(), $providerKey, $user->getRoles());
}
throw new BadCredentialsException('Invalid username or password');
}
作者:BenGorUse
项目:UserBundl
function it_gets_user(UserProviderInterface $userProvider, UserCommandBus $commandBus, LogInUserCommand $credentials, User $user)
{
$commandBus->handle($credentials)->shouldBeCalled();
$credentials->email()->shouldBeCalled()->willReturn('bengor@user.com');
$userProvider->loadUserByUsername('bengor@user.com')->shouldBeCalled()->willReturn($user);
$this->getUser($credentials, $userProvider)->shouldReturn($user);
}
作者:arcany
项目:SSO-user-provider-bundl
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
{
$secret = $token->getCredentials();
$userData = $this->session->getFlashBag()->get('arcanys_sso_auth.user_data');
if ($userData) {
// TODO create mapping config in the future
$username = reset($userData['uid']);
$email = reset($userData['email']);
$firstname = reset($userData['firstname']);
$lastname = reset($userData['lastname']);
$token = reset($userData['token']);
$roles = $userData['rights'];
if (!$roles) {
$roles = ['ROLE_USER'];
}
} else {
$this->saml2->login();
exit;
}
if (!$username) {
throw new AuthenticationException("Failed to authenticate from SSO");
}
$user = $userProvider->loadUserByUsername(['username' => $username, 'email' => $email, 'firstname' => $firstname, 'lastname' => $lastname, 'token' => $token, 'roles' => $roles]);
return new PreAuthenticatedToken($user, $secret, $providerKey, $user->getRoles($roles));
}
作者:mlukma
项目:securile
/**
* Attempt to authenticate the provided token using the provided user provider.
* @param TokenInterface $token
* @param UserProviderInterface $userProvider
* @param string $providerKey
* @return UsernamePasswordToken
* @throws BadCredentialsException
*/
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
{
if (($user = $userProvider->loadUserByUsername($token->getUsername())) && $user->getPassword() == $token->getCredentials()) {
return new UsernamePasswordToken($user, $user->getPassword(), $providerKey, $user->getRoles());
}
throw new BadCredentialsException('The presented password is invalid.');
}
作者:ninodafont
项目:KayueWordpressBundl
/**
* Validates WordPress authentication cookie
*
* @param UserProviderInterface $userProvider
* @param Cookie $cookie
* @return UserInterface UserInterface if valid.
* @throws RuntimeException
* @throws AuthenticationException
*/
public function validateCookie(UserProviderInterface $userProvider, $cookie)
{
$cookieParts = $this->decodeCookie($cookie);
switch (count($cookieParts)) {
case 3:
list($username, $expiration, $hmac) = $cookieParts;
$token = null;
break;
case 4:
list($username, $expiration, $token, $hmac) = $cookieParts;
break;
default:
throw new AuthenticationException('Invalid WordPress cookie.');
}
if ($expiration < time()) {
throw new AuthenticationException('The WordPress cookie has expired.');
}
try {
$user = $userProvider->loadUserByUsername($username);
} catch (Exception $exception) {
if (!$exception instanceof AuthenticationException) {
$exception = new AuthenticationException($exception->getMessage(), $exception->getCode(), $exception);
}
throw $exception;
}
if (!$user instanceof UserInterface) {
throw new RuntimeException(sprintf('The UserProviderInterface implementation must return an instance of UserInterface, but returned "%s".', get_class($user)));
}
if ($token && $hmac !== $this->generateHmacWithToken($username, $expiration, $token, $user->getPassword()) || !$token && $hmac !== $this->generateHmac($username, $expiration, $user->getPassword())) {
throw new AuthenticationException('The WordPress cookie\'s hash is invalid. Your logged in key and salt settings could be wrong.');
}
return $user;
}
作者:PRayn
项目:CasAuthBundl
/**
* Calls the UserProvider providing a valid User
* @param array $credentials
* @param UserProviderInterface $userProvider
* @return bool
*/
public function getUser($credentials, UserProviderInterface $userProvider)
{
if (isset($credentials[$this->username_attribute])) {
return $userProvider->loadUserByUsername($credentials[$this->username_attribute]);
} else {
return null;
}
}
作者:busae
项目:s
/**
* {@inheritdoc}
*/
public function getUser($credentials, UserProviderInterface $userProvider)
{
try {
return $userProvider->loadUserByUsername($credentials['username']);
} catch (UsernameNotFoundException $e) {
throw new CustomUserMessageAuthenticationException($this->translator->trans($this->failMessage));
}
}
作者:BenGorUse
项目:UserBundl
function it_does_not_get_user_because_user_is_inactive(JWTEncoderInterface $jwtEncoder, UserProviderInterface $userProvider, User $user)
{
$jwtEncoder->decode('bearer-token')->shouldBeCalled()->willReturn(['email' => 'bengor@user.com']);
$userProvider->loadUserByUsername('bengor@user.com')->shouldBeCalled()->willReturn($user);
$user->confirmationToken = 'confirmation-token';
$user->invitationToken = null;
$this->shouldThrow(new CustomUserMessageAuthenticationException('The user does not exist'))->duringGetUser('bearer-token', $userProvider);
}
作者:ruslan-polutsyga
项目:dev-bundl
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
{
$user = $userProvider->loadUserByUsername($token->getUsername());
if (!$user) {
throw new AuthenticationException('User not found');
}
return new PreAuthenticatedToken($user, $token->getUsername(), $providerKey, $user->getRoles());
}
作者:jayrule
项目:sf
public function getUser($credentials, UserProviderInterface $userProvider)
{
try {
$user = $userProvider->loadUserByUsername($credentials['username']);
return $user;
} catch (\Exception $ex) {
throw new CustomUserMessageAuthenticationException("User was not found.");
}
}
作者:ngyda
项目:CoreBundl
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
{
try {
$user = $userProvider->loadUserByUsername($token->getUsername());
return $this->authenticate($user, $token, $providerKey);
} catch (UsernameNotFoundException $e) {
return $this->getFromProviders($token, $providerKey);
}
}
作者:gobudgi
项目:gobudgi
function it_throws_authentication_exception_when_user_could_not_be_loaded(ResourceServer $resourceServer, UserProviderInterface $userProvider, EmitterInterface $emitter)
{
$providerKey = 'default';
$userIdentifier = 'user@example.org';
$token = new PreAuthenticatedToken('anon.', 'DDSHs55zpG51Mtxnt6H8vwn5fVJ230dF', $providerKey);
$resourceServer->isValidRequest(true)->shouldBeCalled();
$this->resourceServerWillReturnOwnerId($resourceServer, $emitter, $userIdentifier);
$userProvider->loadUserByUsername($userIdentifier)->willThrow(UsernameNotFoundException::class);
$this->shouldThrow(AuthenticationException::class)->during('authenticateToken', [$token, $userProvider, $providerKey]);
}
作者:formvaul
项目:Antenn
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
{
$user = $userProvider->loadUserByUsername($token->getUsername());
$this->userChecker->checkPreAuth($user);
if (!$this->encoder->isPasswordValid($user, $token->getCredentials())) {
throw new BadCredentialsException('The presented password is invalid.');
}
$this->userChecker->checkPostAuth($user);
return new UsernamePasswordToken($user, $token->getCredentials(), $providerKey, $user->getRoles());
}
作者:formvaul
项目:Antenn
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
{
$webToken = $token->getWebToken();
$user = $userProvider->loadUserByUsername($webToken->getSubject());
$this->userChecker->checkPreAuth($user);
$this->userChecker->checkPostAuth($user);
$token = new WebTokenToken($providerKey, $webToken, $user->getRoles());
$token->setUser($user)->setAuthenticated(true);
return $token;
}
作者:datavoyage
项目:SimplesamlphpBundl
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
{
$username = $token->getUsername();
$user = $userProvider->loadUserByUsername($username);
if ($user instanceof SamlUserInterface) {
$user->setSamlAttributes($token->getAttributes());
}
$authenticatedToken = new SamlToken($user, $user->getRoles());
$authenticatedToken->setAttributes($token->getAttributes());
return $authenticatedToken;
}
作者:enneit
项目:swagger-bundl
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
{
$accessToken = $token->getCredentials();
try {
$user = $userProvider->loadUserByUsername($accessToken);
} catch (UsernameNotFoundException $e) {
throw new UnauthorizedApiException($e->getMessage());
}
$token->setUser($user);
return $token;
}
作者:upchu
项目:symfony
/**
* {@inheritdoc}
*/
public function getUser($credentials, UserProviderInterface $userProvider)
{
if (!$userProvider instanceof InMemoryUserProvider) {
return;
}
try {
return $userProvider->loadUserByUsername($credentials['username']);
} catch (UsernameNotFoundException $e) {
throw new CustomUserMessageAuthenticationException($this->failMessage);
}
}
作者:pixocod
项目:noostach
/**
* Return a UserInterface object based on the credentials returned by getCredentials()
* if null, authentication will fail
* if a User object, checkCredentials() is called
*/
public function getUser($credentials, UserProviderInterface $userProvider)
{
if (isset($credentials['token'])) {
return $this->em->getRepository('AppBundle:User')->findOneBy(array('apiToken' => $credentials['token']));
} else {
if (isset($credentials['username']) && isset($credentials['plainPassword'])) {
return $userProvider->loadUserByUsername($credentials['username']);
}
}
return null;
}