/** * {@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()))); }
/** * @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()); }
/** * @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()); }
/** * @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; }
/** * @test */ public function shouldWrapAnyThrownExceptionsAsAuthenticatedServiceException() { $providerKey = 'main'; $expectedPreviousException = new \Exception($expectedMessage = 'Something goes wrong', $expectedCode = 23); $userProviderMock = $this->createUserProviderMock(); $userProviderMock->expects($this->once())->method('loadUserByUsername')->will($this->throwException($expectedPreviousException)); $authProvider = new OpenIdAuthenticationProvider($providerKey, $userProviderMock, $this->createUserCheckerMock()); $token = new OpenIdToken($providerKey, 'identity'); $token->setUser(''); try { $authProvider->authenticate($token); } catch (AuthenticationServiceException $e) { $this->assertSame($expectedPreviousException, $e->getPrevious()); $this->assertEquals($expectedMessage, $e->getMessage()); $this->assertEquals($expectedCode, $e->getCode()); $this->assertNull($e->getToken()); return; } $this->fail('Expected exception: AuthenticationServiceException was not thrown'); }