public function login($user, array $attributes = null, $providerKey = null) { if (!$providerKey) { $providerKey = $this->_providerKey; } if ($user instanceof UserInterface) { $token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles()); } else { $token = new AnonymousToken($providerKey, $user ?: 'anon.'); } if ($attributes) { $token->setAttributes($attributes); } $this->loginToken($token); }
/** * {@inheritDoc} */ public function authenticate(TokenInterface $token) { if (!$this->supports($token)) { return null; } /* @var OAuthToken $token */ $resourceOwner = $this->resourceOwnerMap->getResourceOwnerByName($token->getResourceOwnerName()); if ($token->getUser()) { $user = $this->userProvider->refreshUser($token->getUser()); } else { try { $userResponse = $resourceOwner->getUserInformation($token->getRawToken()); } catch (HttpTransportException $e) { $token = new AnonymousToken($token->getRawToken(), 'anon.'); $token->setAuthenticated(true); return $token; } catch (RequestException $e) { $token = new AnonymousToken($token->getRawToken(), 'anon.'); $token->setAuthenticated(true); return $token; } try { $user = $this->userProvider->loadUserByOAuthUserResponse($userResponse); } catch (OAuthAwareExceptionInterface $e) { $e->setToken($token); $e->setResourceOwnerName($token->getResourceOwnerName()); throw $e; } } if (!$user instanceof UserInterface) { throw new AuthenticationServiceException('loadUserByOAuthUserResponse() must return a UserInterface.'); } try { $this->userChecker->checkPreAuth($user); $this->userChecker->checkPostAuth($user); } catch (BadCredentialsException $e) { throw $e; } $token = new OAuthToken($token->getRawToken(), $user->getRoles()); $token->setResourceOwnerName($resourceOwner->getName()); $token->setUser($user); $token->setAuthenticated(true); return $token; }
public function testGetUser() { $token = new AnonymousToken('foo', 'bar'); $this->assertEquals('bar', $token->getUser()); }
/** * Constructor. * * @codeCoverageIgnore * * @param string $key The key shared with the authentication provider * @param string $user The user * @param RoleInterface[] $roles An array of roles */ public function __construct($key, $user, array $roles = array()) { parent::__construct($key, $user, $roles); $this->setAuthenticated(true); }
/** * Constructor. */ public function __construct() { parent::__construct(session_id(), 'anonymous'); }