コード例 #1
0
 /**
  * {@inheritdoc}
  */
 protected function attemptAuthentication(Request $request)
 {
     $openIdRequest = $request->duplicate();
     if (false == empty($this->options['required_attributes'])) {
         $openIdRequest->attributes->set('required_attributes', $this->options['required_attributes']);
     }
     if (false == empty($this->options['optional_attributes'])) {
         $openIdRequest->attributes->set('optional_attributes', $this->options['optional_attributes']);
     }
     $result = $this->getRelyingParty()->manage($openIdRequest);
     if ($result instanceof RedirectResponse) {
         if ($targetUrl = $request->get($this->options['target_path_parameter'], null, true)) {
             $request->getSession()->set('_security.' . $this->providerKey . '.target_path', $targetUrl);
         }
         return $result;
     }
     if ($result instanceof IdentityProviderResponse) {
         $token = new OpenIdToken($this->providerKey, $result->getIdentity());
         $token->setAttributes($result->getAttributes());
         try {
             return $this->authenticationManager->authenticate($token);
         } catch (AuthenticationException $e) {
             $e->setToken($token);
             throw $e;
         }
     }
     throw new \RuntimeException(sprintf('The relying party %s::manage() must either return a RedirectResponse or instance of IdentityProviderResponse.', get_class($this->getRelyingParty())));
 }
コード例 #2
0
 /**
  * @test
  */
 public function shouldUnserializeAttributes()
 {
     $expectedAttributes = array('foo' => 'foo', 'bar' => 'bar');
     $token = new OpenIdToken('provider_key', 'identity');
     $token->setAttributes($expectedAttributes);
     $unserializedToken = unserialize(serialize($token));
     $this->assertEquals($expectedAttributes, $unserializedToken->getAttributes());
 }
コード例 #3
0
 /**
  * @test
  */
 public function shouldReturnIdentityProviderResponseOnManage()
 {
     $expectedIdentity = 'theIdentity';
     $expectedAttributes = array('foo' => 'fooVal', 'bar' => 'barVal');
     $token = new OpenIdToken('aProviderKey', $expectedIdentity);
     $token->setAttributes($expectedAttributes);
     $error = new AuthenticationException('an error');
     $error->setToken($token);
     $session = $this->createSessionStub($returnGet = $error);
     $request = $this->createRequestStub($returnGet = 1, $returnSession = $session);
     $relyingParty = new RecoveredFailureRelyingParty();
     //guard
     $this->assertTrue($relyingParty->supports($request));
     $actualIdentityProviderResponse = $relyingParty->manage($request);
     $this->assertInstanceOf('Fp\\OpenIdBundle\\RelyingParty\\IdentityProviderResponse', $actualIdentityProviderResponse);
     $this->assertEquals($expectedIdentity, $actualIdentityProviderResponse->getIdentity());
     $this->assertEquals($expectedAttributes, $actualIdentityProviderResponse->getAttributes());
 }
コード例 #4
0
 /**
  * @param string $identity
  * @param array $attributes
  * @param array $roles
  * @param mixed $user
  *
  * @return \Fp\OpenIdBundle\Security\Core\Authentication\Token\OpenIdToken
  */
 protected function createAuthenticatedToken($identity, array $attributes, array $roles, $user)
 {
     if ($user instanceof UserInterface) {
         $this->userChecker->checkPostAuth($user);
     }
     $newToken = new OpenIdToken($this->providerKey, $identity, $roles);
     $newToken->setUser($user);
     $newToken->setAttributes($attributes);
     $newToken->setAuthenticated(true);
     return $newToken;
 }
コード例 #5
0
 /**
  * @test
  */
 public function shouldCreateAuthenticatedTokenUsingUserManagerCreateFromIdentityMethod()
 {
     $expectedProviderKey = 'main';
     $expectedIdentity = 'the_identity';
     $expectedAttributes = array('foo' => 'foo', 'bar' => 'bar_val');
     $expectedUserMock = $this->createUserMock();
     $expectedUserMock->expects($this->any())->method('getRoles')->will($this->returnValue(array('foo', 'bar')));
     $userManagerMock = $this->createUserManagerMock();
     $userManagerMock->expects($this->once())->method('loadUserByUsername')->with($expectedIdentity)->will($this->throwException(new UsernameNotFoundException('Cannot find user by openid identity')));
     $userManagerMock->expects($this->once())->method('createUserFromIdentity')->with($expectedIdentity)->will($this->returnValue($expectedUserMock));
     $authProvider = new OpenIdAuthenticationProvider($expectedProviderKey, $userManagerMock, $this->createUserCheckerMock(), $createIfNotExist = true);
     $token = new OpenIdToken($expectedProviderKey, $expectedIdentity);
     $token->setUser('');
     $token->setAttributes($expectedAttributes);
     $authenticatedToken = $authProvider->authenticate($token);
     $this->assertInstanceOf('Fp\\OpenIdBundle\\Security\\Core\\Authentication\\Token\\OpenIdToken', $authenticatedToken);
     $this->assertNotSame($token, $authenticatedToken);
     $this->assertTrue($authenticatedToken->isAuthenticated());
     $this->assertEquals($expectedIdentity, $authenticatedToken->getIdentity());
     $this->assertEquals($expectedProviderKey, $authenticatedToken->getProviderKey());
     $this->assertEquals($expectedUserMock, $authenticatedToken->getUser());
     $this->assertEquals($expectedAttributes, $authenticatedToken->getAttributes());
     $roles = $authenticatedToken->getRoles();
     $this->assertInternalType('array', $roles);
     $this->assertCount(2, $roles);
     $this->assertEquals('foo', $roles[0]->getRole());
     $this->assertEquals('bar', $roles[1]->getRole());
 }