/** * Iterate over two-factor providers and ask for two-factor authentication. * Each provider can return a response. The first response will be returned. * * @param AuthenticationContextInterface $context * * @return Response|null */ public function requestAuthenticationCode(AuthenticationContextInterface $context) { $token = $context->getToken(); // Iterate over two-factor providers and ask for completion /** @var TwoFactorProviderInterface $provider */ foreach ($this->providers as $providerName => $provider) { if ($this->flagManager->isNotAuthenticated($providerName, $token)) { $response = $provider->requestAuthenticationCode($context); // Set authentication completed if ($context->isAuthenticated()) { $this->eventDispatcher->dispatch(TwoFactorAuthenticationEvents::SUCCESS, new TwoFactorAuthenticationEvent()); $this->flagManager->setComplete($providerName, $token); } else { if ($context->getRequest()->get($this->authRequestParameter) !== null) { $this->eventDispatcher->dispatch(TwoFactorAuthenticationEvents::FAILURE, new TwoFactorAuthenticationEvent()); } } // Return response if ($response instanceof Response) { return $response; } } } return null; }
/** * @param TokenInterface $token * @param mixed $object * @param array $attributes * * @return mixed result */ public function vote(TokenInterface $token, $object, array $attributes) { foreach ($this->providers as $providerName) { if ($this->sessionFlagManager->isNotAuthenticated($providerName, $token)) { return VoterInterface::ACCESS_DENIED; } } return VoterInterface::ACCESS_ABSTAIN; }
/** * vote * @param TokenInterface $token * @param mixed $object * @param array $attributes * @return mixed result **/ public function vote(TokenInterface $token, $object, array $attributes) { foreach ($this->providerCollection->getProviders() as $providerName => $provider) { $res = $this->sessionFlagManager->isNotAuthenticated($providerName, $token); if (true === $res) { return VoterInterface::ACCESS_DENIED; } } return VoterInterface::ACCESS_ABSTAIN; }
/** * @test * @dataProvider dataProvider_isNotAuthenticated */ public function isNotAuthenticated_hasFlagSet_returnCorrectBoolean($getReturnValue, $expectedReturnValue) { $token = $this->getMock("Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface"); //Mock the SessionFlagGenerator $this->flagGenerator->expects($this->once())->method("getSessionFlag")->with("providerName", $token)->will($this->returnValue("session_flag")); //Mock the Session $this->session->expects($this->once())->method("has")->with("session_flag")->will($this->returnValue(true)); $this->session->expects($this->once())->method("get")->with("session_flag")->will($this->returnValue($getReturnValue)); $returnValue = $this->sessionFlagManager->isNotAuthenticated("providerName", $token); $this->assertEquals($expectedReturnValue, $returnValue); }
/** * Iterate over two-factor providers and ask for two-factor authentcation. * Each provider can return a response. The first response will be returned. * * @param \Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContext $context * @return \Symfony\Component\HttpFoundation\Response|null */ public function requestAuthenticationCode(AuthenticationContext $context) { $token = $context->getToken(); // Iterate over two-factor providers and ask for completion foreach ($this->providers as $providerName => $provider) { if ($this->flagManager->isNotAuthenticated($providerName, $token)) { $response = $provider->requestAuthenticationCode($context); // Set authentication completed if ($context->isAuthenticated()) { $this->flagManager->setComplete($providerName, $token); } // Return response if ($response instanceof Response) { return $response; } } } return null; }
/** * @test * @dataProvider dataProvider_isNotAuthenticated */ public function isNotAuthenticated_hasFlagSet_returnCorrectBoolean($getReturnValue, $expectedReturnValue) { $token = $this->createMock('Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface'); //Mock the SessionFlagGenerator $this->flagGenerator->expects($this->once())->method('getSessionFlag')->with('providerName', $token)->willReturn('session_flag'); //Mock the Session $this->session->expects($this->once())->method('isStarted')->willReturn($this->returnValue(true)); $this->session->expects($this->once())->method('has')->with('session_flag')->willReturn(true); $this->session->expects($this->once())->method('get')->with('session_flag')->willReturn($getReturnValue); $returnValue = $this->sessionFlagManager->isNotAuthenticated('providerName', $token); $this->assertEquals($expectedReturnValue, $returnValue); }