Skip to content

Commit 7686eb0

Browse files
committed
OXDEV-9078 Guard the triggerChallenge with resend timeouts
Signed-off-by: Anton Fedurtsya <anton@fedurtsya.com>
1 parent 715c6ee commit 7686eb0

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

src/Authentication/TwoFactorAuth/OTP/OtpFacade.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ public function isVerified(string $userId): bool
4343

4444
public function triggerChallenge(string $userId): void
4545
{
46-
$code = $this->codeGenerator->generateCode();
47-
48-
// todo-critical: check if we can trigger the notification
46+
if (!$this->sendPolicy->canSend($userId)) {
47+
return;
48+
}
4949

50+
$code = $this->codeGenerator->generateCode();
5051
$this->stateService->createChallengeState($userId, $code);
5152
$this->notifierFactory->create($userId)->notify($userId, $code);
5253
}

tests/Unit/Authentication/TwoFactorAuth/OTP/OtpFacadeTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ public function verifyDoesNotMarkVerifiedWhenValidationFails(): void
154154
#[Test]
155155
public function triggerChallengeGeneratesCodeCreatesStateAndNotifies(): void
156156
{
157+
$sendPolicyStub = $this->createStub(OtpSendPolicyServiceInterface::class);
158+
$sendPolicyStub->method('canSend')->willReturn(true);
159+
157160
$codeGeneratorStub = $this->createStub(OtpCodeGeneratorServiceInterface::class);
158161
$codeGeneratorStub->method('generateCode')->willReturn($code = uniqid());
159162

@@ -174,11 +177,33 @@ public function triggerChallengeGeneratesCodeCreatesStateAndNotifies(): void
174177
stateService: $stateServiceSpy,
175178
codeGenerator: $codeGeneratorStub,
176179
notifierFactory: $notifierFactoryStub,
180+
sendPolicy: $sendPolicyStub,
177181
);
178182

179183
$sut->triggerChallenge(userId: $userId);
180184
}
181185

186+
#[Test]
187+
public function triggerChallengeDoesNothingWhenCooldownActive(): void
188+
{
189+
$sendPolicyStub = $this->createStub(OtpSendPolicyServiceInterface::class);
190+
$sendPolicyStub->method('canSend')->willReturn(false);
191+
192+
$stateServiceSpy = $this->createMock(OtpChallengeStateServiceInterface::class);
193+
$stateServiceSpy->expects($this->never())->method('createChallengeState');
194+
195+
$notifierFactorySpy = $this->createMock(OtpNotifierFactoryInterface::class);
196+
$notifierFactorySpy->expects($this->never())->method('create');
197+
198+
$sut = $this->getSut(
199+
stateService: $stateServiceSpy,
200+
notifierFactory: $notifierFactorySpy,
201+
sendPolicy: $sendPolicyStub,
202+
);
203+
204+
$sut->triggerChallenge(userId: uniqid());
205+
}
206+
182207
#[Test]
183208
public function resendThrowsCooldownExceptionWhenSendNotAllowed(): void
184209
{

0 commit comments

Comments
 (0)