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

Commit ebbd21a

Browse files
committed
Ensure session is saved and closed when preparation is executed #257
1 parent a7ebfe5 commit ebbd21a

4 files changed

Lines changed: 64 additions & 13 deletions

File tree

Security/TwoFactor/Provider/TwoFactorProviderPreparationListener.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,22 @@ public function onKernelFinishRequest(FinishRequestEvent $event): void
103103
$providerName = $this->twoFactorToken->getCurrentTwoFactorProvider();
104104
$firewallName = $this->twoFactorToken->getProviderKey();
105105

106-
if ($this->preparationRecorder->isProviderPrepared($firewallName, $providerName)) {
106+
try {
107+
if ($this->preparationRecorder->isProviderPrepared($firewallName, $providerName)) {
108+
if ($this->logger) {
109+
$this->logger->info(sprintf('Two-factor provider "%s" was already prepared.', $providerName));
110+
}
111+
112+
return;
113+
}
114+
$user = $this->twoFactorToken->getUser();
115+
$this->providerRegistry->getProvider($providerName)->prepareAuthentication($user);
116+
$this->preparationRecorder->recordProviderIsPrepared($firewallName, $providerName);
107117
if ($this->logger) {
108-
$this->logger->info(sprintf('Two-factor provider "%s" was already prepared.', $providerName));
118+
$this->logger->info(sprintf('Two-factor provider "%s" prepared.', $providerName));
109119
}
110-
111-
return;
112-
}
113-
114-
$user = $this->twoFactorToken->getUser();
115-
$this->providerRegistry->getProvider($providerName)->prepareAuthentication($user);
116-
$this->preparationRecorder->recordProviderIsPrepared($firewallName, $providerName);
117-
118-
if ($this->logger) {
119-
$this->logger->info(sprintf('Two-factor provider "%s" prepared.', $providerName));
120+
} finally {
121+
$this->preparationRecorder->saveSession();
120122
}
121123
}
122124

Security/TwoFactor/Provider/TwoFactorProviderPreparationRecorder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,11 @@ public function recordProviderIsPrepared(string $firewallName, string $providerN
3737
$calledProviders[$firewallName][] = $providerName;
3838
$this->session->set(self::CALLED_PROVIDERS_SESSION_KEY, $calledProviders);
3939
}
40+
41+
public function saveSession(): void
42+
{
43+
if ($this->session->isStarted()) {
44+
$this->session->save();
45+
}
46+
}
4047
}

Tests/Security/TwoFactor/Provider/TwoFactorProviderPreparationListenerTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ private function expectPrepareCurrentProvider(): void
131131
->expects($this->once())
132132
->method('prepareAuthentication')
133133
->with($this->identicalTo($this->user));
134+
135+
$this->preparationRecorder
136+
->expects($this->once())
137+
->method('saveSession');
134138
}
135139

136140
private function expectNotPrepareCurrentProvider(): void
@@ -217,7 +221,7 @@ public function onTwoFactorForm_onEvent_twoFactorProviderIsPrepared(): void
217221
/**
218222
* @test
219223
*/
220-
public function onKernelFinishRequest_providerAlreadyPrepared_doNothing(): void
224+
public function onKernelFinishRequest_providerAlreadyPrepared_saveSession(): void
221225
{
222226
$this->initTwoFactorProviderPreparationListener(true, true);
223227
$event = $this->createTwoFactorAuthenticationEvent();
@@ -228,6 +232,10 @@ public function onKernelFinishRequest_providerAlreadyPrepared_doNothing(): void
228232
->with(self::FIREWALL_NAME, self::CURRENT_PROVIDER_NAME)
229233
->willReturn(true);
230234

235+
$this->preparationRecorder
236+
->expects($this->once())
237+
->method('saveSession');
238+
231239
$this->preparationRecorder
232240
->expects($this->never())
233241
->method('recordProviderIsPrepared');

Tests/Security/TwoFactor/Provider/TwoFactorProviderPreparationRecorderTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,38 @@ public function onTwoFactorAuthenticationRequest_authenticationRequired_alreadyP
8484

8585
$this->recorder->recordProviderIsPrepared(self::FIREWALL_NAME, self::CURRENT_PROVIDER_NAME);
8686
}
87+
88+
/**
89+
* @test
90+
*/
91+
public function saveSession_sessionIsStarted_save(): void
92+
{
93+
$this->session
94+
->expects($this->any())
95+
->method('isStarted')
96+
->willReturn(true);
97+
98+
$this->session
99+
->expects($this->once())
100+
->method('save');
101+
102+
$this->recorder->saveSession();
103+
}
104+
105+
/**
106+
* @test
107+
*/
108+
public function saveSession_sessionNotStarted_doNotSave(): void
109+
{
110+
$this->session
111+
->expects($this->any())
112+
->method('isStarted')
113+
->willReturn(false);
114+
115+
$this->session
116+
->expects($this->never())
117+
->method('save');
118+
119+
$this->recorder->saveSession();
120+
}
87121
}

0 commit comments

Comments
 (0)