Skip to content

Commit 1009968

Browse files
committed
OXDEV-9078 Use new 2FA service in the controller resend
1 parent b02bfca commit 1009968

2 files changed

Lines changed: 43 additions & 42 deletions

File tree

src/Authentication/TwoFactorAuth/Controller/TwoFactorAuthController.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use OxidEsales\Eshop\Application\Controller\FrontendController;
1313
use OxidEsales\Eshop\Core\UtilsView;
1414
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception\InvalidCodeException;
15-
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\AuthorizeServiceInterface;
15+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception\ResendCooldownException;
1616
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAServiceInterface;
1717
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAUserServiceInterface;
1818
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Transput\AuthCodeRequestInterface;
@@ -31,7 +31,6 @@ class TwoFactorAuthController extends FrontendController
3131
public function __construct(
3232
private readonly TwoFAServiceInterface $twoFAService,
3333
private readonly TwoFAUserServiceInterface $twoFAUserService,
34-
private readonly AuthorizeServiceInterface $authService,
3534
private readonly AuthCodeRequestInterface $authCodeRequest,
3635
private readonly UtilsView $utilsView,
3736
private readonly JsonResponseInterface $jsonResponse,
@@ -56,12 +55,12 @@ public function handleOTP(): ?string
5655

5756
public function resendCode(): void
5857
{
59-
$success = $this->authService->resend();
60-
61-
if (!$success) {
62-
$this->jsonResponse->setStatusCode(429);
58+
$userId = $this->twoFAUserService->getPendingUserId();
59+
try {
60+
$this->twoFAService->resend($userId);
61+
$this->jsonResponse->send(['success' => true]);
62+
} catch (ResendCooldownException) {
63+
$this->jsonResponse->send(['success' => false], 429);
6364
}
64-
65-
$this->jsonResponse->send(['success' => $success]);
6665
}
6766
}

tests/Unit/Authentication/TwoFactorAuth/Controller/TwoFactorAuthControllerTest.php

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use OxidEsales\Eshop\Core\UtilsView;
1313
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Controller\TwoFactorAuthController;
1414
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception\InvalidCodeException;
15-
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\AuthorizeServiceInterface;
15+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception\ResendCooldownException;
1616
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAServiceInterface;
1717
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAUserServiceInterface;
1818
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Transput\AuthCodeRequestInterface;
@@ -72,63 +72,65 @@ public function handleOTPDisplaysErrorOnInvalidCode(): void
7272
$sut->handleOTP();
7373
}
7474

75-
public function testResendCodeSendsSuccessResponse(): void
75+
#[Test]
76+
public function resendCodeSendsSuccessResponse(): void
7677
{
77-
$authServiceMock = $this->createMock(AuthorizeServiceInterface::class);
78-
$authServiceMock->expects($this->once())
78+
$twoFAUserServiceStub = $this->createStub(TwoFAUserServiceInterface::class);
79+
$twoFAUserServiceStub->method('getPendingUserId')
80+
->willReturn($userId = uniqid());
81+
82+
$twoFAServiceSpy = $this->createMock(TwoFAServiceInterface::class);
83+
$twoFAServiceSpy->expects($this->once())
7984
->method('resend')
80-
->willReturn(true);
85+
->with($userId);
8186

82-
$jsonResponseMock = $this->createMock(JsonResponseInterface::class);
83-
$jsonResponseMock->expects($this->never())
84-
->method('setStatusCode');
85-
$jsonResponseMock->expects($this->once())
87+
$jsonResponseSpy = $this->createMock(JsonResponseInterface::class);
88+
$jsonResponseSpy->expects($this->once())
8689
->method('send')
87-
->with(['success' => true]);
88-
89-
$controller = $this->getSut(
90-
authService: $authServiceMock,
91-
jsonResponse: $jsonResponseMock,
92-
);
90+
->with(['success' => true], 200);
9391

94-
$controller->resendCode();
92+
$this->getSut(
93+
twoFAService: $twoFAServiceSpy,
94+
twoFAUserService: $twoFAUserServiceStub,
95+
jsonResponse: $jsonResponseSpy,
96+
)->resendCode();
9597
}
9698

97-
public function testResendCodeSends429WhenCooldownActive(): void
99+
#[Test]
100+
public function resendCodeSends429OnCooldown(): void
98101
{
99-
$authServiceMock = $this->createMock(AuthorizeServiceInterface::class);
100-
$authServiceMock->expects($this->once())
102+
$twoFAUserServiceStub = $this->createStub(TwoFAUserServiceInterface::class);
103+
$twoFAUserServiceStub->method('getPendingUserId')
104+
->willReturn($userId = uniqid());
105+
106+
$twoFAServiceStub = $this->createMock(TwoFAServiceInterface::class);
107+
$twoFAServiceStub->expects($this->once())
101108
->method('resend')
102-
->willReturn(false);
109+
->with($userId)
110+
->willThrowException(new ResendCooldownException());
103111

104-
$jsonResponseMock = $this->createMock(JsonResponseInterface::class);
105-
$jsonResponseMock->expects($this->once())
106-
->method('setStatusCode')
107-
->with(429);
108-
$jsonResponseMock->expects($this->once())
112+
$jsonResponseSpy = $this->createMock(JsonResponseInterface::class);
113+
$jsonResponseSpy->expects($this->once())
109114
->method('send')
110-
->with(['success' => false]);
115+
->with(['success' => false], 429);
111116

112-
$controller = $this->getSut(
113-
authService: $authServiceMock,
114-
jsonResponse: $jsonResponseMock,
115-
);
116-
117-
$controller->resendCode();
117+
$this->getSut(
118+
twoFAService: $twoFAServiceStub,
119+
twoFAUserService: $twoFAUserServiceStub,
120+
jsonResponse: $jsonResponseSpy,
121+
)->resendCode();
118122
}
119123

120124
private function getSut(
121125
TwoFAServiceInterface $twoFAService = null,
122126
TwoFAUserServiceInterface $twoFAUserService = null,
123-
AuthorizeServiceInterface $authService = null,
124127
AuthCodeRequestInterface $authCodeRequest = null,
125128
UtilsView $utilsView = null,
126129
JsonResponseInterface $jsonResponse = null,
127130
): TwoFactorAuthController {
128131
return new TwoFactorAuthController(
129132
twoFAService: $twoFAService ?? $this->createStub(TwoFAServiceInterface::class),
130133
twoFAUserService: $twoFAUserService ?? $this->createStub(TwoFAUserServiceInterface::class),
131-
authService: $authService ?? $this->createStub(AuthorizeServiceInterface::class),
132134
authCodeRequest: $authCodeRequest ?? $this->createStub(AuthCodeRequestInterface::class),
133135
utilsView: $utilsView ?? $this->createStub(UtilsView::class),
134136
jsonResponse: $jsonResponse ?? $this->createStub(JsonResponseInterface::class),

0 commit comments

Comments
 (0)