Esempio n. 1
5
 /**
  * {@inheritDoc}
  */
 public function authenticate(TokenInterface $token)
 {
     if (!$this->supports($token)) {
         return;
     }
     /* @var OAuthToken $token */
     $resourceOwner = $this->resourceOwnerMap->getResourceOwnerByName($token->getResourceOwnerName());
     $userResponse = $resourceOwner->getUserInformation($token->getRawToken());
     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) {
         if ($this->hideUserNotFoundExceptions) {
             throw new BadCredentialsException('Bad credentials', 0, $e);
         }
         throw $e;
     }
     $token = new OAuthToken($token->getRawToken(), $user->getRoles());
     $token->setResourceOwnerName($resourceOwner->getName());
     $token->setUser($user);
     $token->setAuthenticated(true);
     return $token;
 }
 /**
  * {@inheritDoc}
  */
 public function authenticate(TokenInterface $token)
 {
     $resourceOwner = $this->resourceOwnerMap->getResourceOwnerByName($token->getResourceOwnerName());
     $userResponse = $resourceOwner->getUserInformation($token->getAccessToken());
     try {
         $user = $this->userProvider->loadUserByOAuthUserResponse($userResponse);
     } catch (OAuthAwareExceptionInterface $e) {
         $e->setAccessToken($token->getAccessToken());
         $e->setResourceOwnerName($token->getResourceOwnerName());
         throw $e;
     }
     $token = new OAuthToken($token->getAccessToken(), $user->getRoles());
     $token->setResourceOwnerName($resourceOwner->getName());
     $token->setUser($user);
     $token->setAuthenticated(true);
     $this->userChecker->checkPostAuth($user);
     return $token;
 }
Esempio n. 3
0
 public function testTokenShouldBeAuthenticated()
 {
     $this->oauthProvider->setTokenFactory($this->tokenFactory);
     $token = new OAuthToken('token');
     $token->setResourceOwnerName('google');
     $organization = new Organization();
     $organization->setEnabled(true);
     $token->setOrganizationContext($organization);
     $userResponse = $this->getMock('HWI\\Bundle\\OAuthBundle\\OAuth\\Response\\UserResponseInterface');
     $resourceOwner = $this->getMock('HWI\\Bundle\\OAuthBundle\\OAuth\\ResourceOwnerInterface');
     $resourceOwner->expects($this->any())->method('getName')->will($this->returnValue('google'));
     $resourceOwner->expects($this->any())->method('getUserInformation')->will($this->returnValue($userResponse));
     $this->resourceOwnerMap->expects($this->any())->method('getResourceOwnerByName')->will($this->returnValue($resourceOwner));
     $user = new User();
     $user->addOrganization($organization);
     $this->userProvider->expects($this->any())->method('loadUserByOAuthUserResponse')->with($userResponse)->will($this->returnValue($user));
     $resultToken = $this->oauthProvider->authenticate($token);
     $this->assertInstanceOf('Oro\\Bundle\\SSOBundle\\Security\\OAuthToken', $resultToken);
     $this->assertSame($user, $resultToken->getUser());
     $this->assertEquals('google', $resultToken->getResourceOwnerName());
     $this->assertTrue($resultToken->isAuthenticated());
 }
Esempio n. 4
0
 /**
  * {@inheritDoc}
  *
  * @throws OAuthAwareExceptionInterface
  */
 public function authenticate(TokenInterface $token)
 {
     if (null === $this->tokenFactory) {
         throw new AuthenticationException('Token Factory is not set in OAuthProvider.');
     }
     /* @var OAuthToken $token */
     $resourceOwner = $this->resourceOwnerMap->getResourceOwnerByName($token->getResourceOwnerName());
     try {
         $userResponse = $resourceOwner->getUserInformation($token->getRawToken());
         $user = $this->userProvider->loadUserByOAuthUserResponse($userResponse);
     } catch (OAuthAwareExceptionInterface $e) {
         $e->setToken($token);
         $e->setResourceOwnerName($token->getResourceOwnerName());
         throw $e;
     }
     $organization = $this->guessOrganization($user, $token);
     $token = $this->tokenFactory->create($token->getRawToken(), $user->getRoles());
     $token->setResourceOwnerName($resourceOwner->getName());
     $token->setOrganizationContext($organization);
     $token->setUser($user);
     $token->setAuthenticated(true);
     $this->userChecker->checkPostAuth($user);
     return $token;
 }