/**
  * Performs authentication.
  * @param Request $request A Request instance
  * @throws \Exception
  * @throws \Symfony\Component\Security\Core\Exception\AuthenticationException
  * @throws \RuntimeException
  * @return TokenInterface|Response|null The authenticated token, null if full authentication is not possible, or a Response
  */
 protected function attemptAuthentication(Request $request)
 {
     $myRequest = $request->duplicate();
     $this->copyOptionsToRequestAttributes($myRequest);
     if (!$this->getRelyingParty()->supports($myRequest)) {
         return null;
     }
     $result = $this->getRelyingParty()->manage($myRequest);
     if ($result instanceof Response) {
         return $result;
     }
     if ($result instanceof SamlSpInfo) {
         $token = new SamlSpToken($this->providerKey);
         $token->setSamlSpInfo($result);
         try {
             return $this->authenticationManager->authenticate($token);
         } catch (AuthenticationException $e) {
             $e->setToken($token);
             throw $e;
         }
     }
     return null;
 }
 /**
  * @test
  */
 public function shouldWrapAnyThrownExceptionsAsAuthenticatedServiceException()
 {
     $samlSpInfoHelper = new SamlSpInfoHelper();
     $providerKey = 'main';
     $expectedSamlSpInfo = $samlSpInfoHelper->getSamlSpInfo();
     $expectedPreviousException = new \Exception($expectedMessage = 'Something goes wrong', $expectedCode = 21);
     $userProviderMock = $this->createUserManagerMock();
     $userProviderMock->expects($this->once())->method('loadUserBySamlInfo')->will($this->throwException($expectedPreviousException));
     $authProvider = new SamlSpAuthenticationProvider($providerKey, $userProviderMock, $this->createUserCheckerMock());
     $token = new SamlSpToken($providerKey);
     $token->setUser('');
     $token->setSamlSpInfo($expectedSamlSpInfo);
     try {
         $authProvider->authenticate($token);
     } catch (AuthenticationServiceException $e) {
         $this->assertSame($expectedPreviousException, $e->getPrevious(), $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');
 }
 /**
  * @test
  */
 public function shouldCopySamlAttributesToAttributes()
 {
     $samlSpInfoHelper = new SamlSpInfoHelper();
     $token = new SamlSpToken('key');
     $expectedSamlSpInfo = $samlSpInfoHelper->getSamlSpInfo();
     $token->setSamlSpInfo($expectedSamlSpInfo);
     $this->assertTrue($token->hasAttribute('a'));
     $this->assertEquals(1, $token->getAttribute('a'));
     $this->assertTrue($token->hasAttribute('b'));
     $this->assertEquals(array(2, 3), $token->getAttribute('b'));
 }
 /**
  * @param \AerialShip\SamlSPBundle\Security\Core\Authentication\Token\SamlSpToken $token
  * @return UserInterface
  */
 private function getDefaultUser(SamlSpToken $token)
 {
     $nameID = $token && $token->getSamlSpInfo()->getNameID() && $token->getSamlSpInfo()->getNameID()->getValue() ? $token->getSamlSpInfo()->getNameID()->getValue() : 'anon.';
     $result = new User($nameID, '', array('ROLE_USER'));
     return $result;
 }