/** * Attempts to authenticate a TokenInterface object. * * @param OAuthToken $token The TokenInterface instance to authenticate * * @return TokenInterface An authenticated TokenInterface instance, never null * * @throws AuthenticationException if the authentication fails */ public function authenticate(TokenInterface $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 = new OAuthToken($token->getRawToken(), $user->getRoles()); $token->setResourceOwnerName($resourceOwner->getName()); $token->setOrganizationContext($organization); $token->setUser($user); $token->setAuthenticated(true); $this->userChecker->checkPostAuth($user); return $token; }
public function testTokenShouldBeAuthenticated() { $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()); }