Author: Fabien Potencier (fabien@symfony.com)
Inheritance: extends AbstractToken
Esempio n. 1
0
 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;
 }
Esempio n. 3
0
 public function testGetUser()
 {
     $token = new AnonymousToken('foo', 'bar');
     $this->assertEquals('bar', $token->getUser());
 }
Esempio n. 4
0
 /**
  * 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);
 }
Esempio n. 5
0
 /**
  * Constructor.
  */
 public function __construct()
 {
     parent::__construct(session_id(), 'anonymous');
 }