Skip to content
This repository was archived by the owner on Dec 2, 2021. It is now read-only.

Commit 253ed9e

Browse files
gnat42scheb
authored andcommitted
Allow multiple firewalls
Perhaps I'm not doing this correctly however, we need 2fa with multiple firewalls. There seems to be an issue with being able to support that. The provider is configured with the template to render, however the path is configured by the firewall. So when my email provider renders the 2fa it has no concept of where the check should be submitted to. If the template was configured on the firewall that'd be different. So what I've done is passed the check_path variable to the template so the form submission can submit to the correct location
1 parent ecbb116 commit 253ed9e

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

Controller/FormController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ protected function getTemplateVars(Request $request, TwoFactorTokenInterface $to
9898
$pendingTwoFactorProviders = $token->getTwoFactorProviders();
9999
$displayTrustedOption = $this->trustedFeatureEnabled && (!$config->isMultiFactor() || 1 === count($pendingTwoFactorProviders));
100100
$authenticationException = $this->getLastAuthenticationException($request->getSession());
101+
$checkPath = $config->getCheckPath();
102+
$isRoute = strpos($checkPath, '/') === false;
101103

102104
return [
103105
'twoFactorProvider' => $token->getCurrentTwoFactorProvider(),
@@ -110,6 +112,8 @@ protected function getTemplateVars(Request $request, TwoFactorTokenInterface $to
110112
'isCsrfProtectionEnabled' => $config->isCsrfProtectionEnabled(),
111113
'csrfParameterName' => $config->getCsrfParameterName(),
112114
'csrfTokenId' => $config->getCsrfTokenId(),
115+
'checkPathRoute' => $isRoute ? $checkPath:null,
116+
'checkPathUrl' => $isRoute ? null:$checkPath,
113117
'logoutPath' => $this->logoutUrlGenerator->getLogoutPath(),
114118
];
115119
}

Resources/views/Authentication/form.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ especially when you're using different route names than the ones used here.
1818
{# Display current two-factor provider #}
1919
<p class="label"><label for="_auth_code">{{ "auth_code"|trans({}, 'SchebTwoFactorBundle') }} {{ twoFactorProvider }}:</label></p>
2020

21-
<form class="form" action="{{ path("2fa_login_check") }}" method="post">
21+
<form class="form" action="{{ checkPathUrl ? checkPathUrl: path(checkPathRoute) }}" method="post">
2222
<p class="widget">
2323
<input
2424
id="_auth_code"

Tests/Controller/FormControllerTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,55 @@ public function form_csrfTokenGeneratorInstanceOfCsrfTokenManagerInterface_isCsr
405405
*/
406406
public function form_renderForm_renderTemplateWithTemplateVars(): void
407407
{
408+
$this->firewallConfig
409+
->expects($this->any())
410+
->method('getCheckPath')
411+
->willReturn('/2fa_check');
412+
413+
$this->stubTokenStorageHasTwoFactorToken();
414+
415+
$this->assertTemplateVars(function (array $templateVars) {
416+
$this->assertArrayHasKey('twoFactorProvider', $templateVars);
417+
$this->assertArrayHasKey('availableTwoFactorProviders', $templateVars);
418+
$this->assertArrayHasKey('authenticationError', $templateVars);
419+
$this->assertArrayHasKey('authenticationErrorData', $templateVars);
420+
$this->assertArrayHasKey('displayTrustedOption', $templateVars);
421+
$this->assertArrayHasKey('authCodeParameterName', $templateVars);
422+
$this->assertArrayHasKey('trustedParameterName', $templateVars);
423+
$this->assertArrayHasKey('isCsrfProtectionEnabled', $templateVars);
424+
$this->assertArrayHasKey('csrfParameterName', $templateVars);
425+
$this->assertArrayHasKey('csrfTokenId', $templateVars);
426+
$this->assertArrayHasKey('checkPathRoute', $templateVars);
427+
$this->assertArrayHasKey('checkPathUrl', $templateVars);
428+
$this->assertArrayHasKey('logoutPath', $templateVars);
429+
430+
$this->assertEquals(self::CURRENT_TWO_FACTOR_PROVIDER, $templateVars['twoFactorProvider']);
431+
$this->assertEquals(['provider1', 'provider2'], $templateVars['availableTwoFactorProviders']);
432+
$this->assertEquals(self::AUTH_CODE_PARAM_NAME, $templateVars['authCodeParameterName']);
433+
$this->assertEquals(self::TRUSTED_PARAM_NAME, $templateVars['trustedParameterName']);
434+
$this->assertFalse($templateVars['isCsrfProtectionEnabled']);
435+
$this->assertEquals(self::CSRF_PARAMETER, $templateVars['csrfParameterName']);
436+
$this->assertEquals(self::CSRF_TOKEN_ID, $templateVars['csrfTokenId']);
437+
$this->assertEquals(self::LOGOUT_PATH, $templateVars['logoutPath']);
438+
$this->assertEquals('/2fa_check', $templateVars['checkPathUrl']);
439+
$this->assertNull($templateVars['checkPathRoute']);
440+
441+
return true;
442+
});
443+
444+
$this->controller->form($this->request);
445+
}
446+
447+
/**
448+
* @test
449+
*/
450+
public function form_renderForm_renderTemplateWithTemplateVarsSetsRoutePath(): void
451+
{
452+
$this->firewallConfig
453+
->expects($this->any())
454+
->method('getCheckPath')
455+
->willReturn('admin_2fa_check');
456+
408457
$this->stubTokenStorageHasTwoFactorToken();
409458

410459
$this->assertTemplateVars(function (array $templateVars) {
@@ -418,6 +467,8 @@ public function form_renderForm_renderTemplateWithTemplateVars(): void
418467
$this->assertArrayHasKey('isCsrfProtectionEnabled', $templateVars);
419468
$this->assertArrayHasKey('csrfParameterName', $templateVars);
420469
$this->assertArrayHasKey('csrfTokenId', $templateVars);
470+
$this->assertArrayHasKey('checkPathRoute', $templateVars);
471+
$this->assertArrayHasKey('checkPathUrl', $templateVars);
421472
$this->assertArrayHasKey('logoutPath', $templateVars);
422473

423474
$this->assertEquals(self::CURRENT_TWO_FACTOR_PROVIDER, $templateVars['twoFactorProvider']);
@@ -428,6 +479,8 @@ public function form_renderForm_renderTemplateWithTemplateVars(): void
428479
$this->assertEquals(self::CSRF_PARAMETER, $templateVars['csrfParameterName']);
429480
$this->assertEquals(self::CSRF_TOKEN_ID, $templateVars['csrfTokenId']);
430481
$this->assertEquals(self::LOGOUT_PATH, $templateVars['logoutPath']);
482+
$this->assertEquals('admin_2fa_check', $templateVars['checkPathRoute']);
483+
$this->assertNull($templateVars['checkPathUrl']);
431484

432485
return true;
433486
});

0 commit comments

Comments
 (0)