Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorProviderRegistry::requestAuthenticationCode PHP Method

requestAuthenticationCode() public method

Each provider can return a response. The first response will be returned.
public requestAuthenticationCode ( Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextInterface $context ) : Response | null
$context Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextInterface
return Symfony\Component\HttpFoundation\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;
    }

Usage Example

 /**
  * @test
  */
 public function requestAuthenticationCode_requestAuthenticationCode_returnResponse()
 {
     $token = $this->getToken();
     $context = $this->getAuthenticationContext($token, true);
     //Stub the SessionFlagManager
     $this->stubIsNotAuthenticated(true);
     //Stub the provider
     $this->provider->expects($this->any())->method('requestAuthenticationCode')->willReturn(new Response('<form></form>'));
     $returnValue = $this->registry->requestAuthenticationCode($context);
     $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Response', $returnValue);
     $this->assertEquals('<form></form>', $returnValue->getContent());
 }