Skip to content

Commit f9ce596

Browse files
committed
Merge branch 'b-7.4.x-finalize-validation-OXDEV-10012' into b-7.4.x-2fa-OXDEV-9078
2 parents bea74a0 + 62bfa65 commit f9ce596

34 files changed

Lines changed: 1275 additions & 414 deletions

src/Authentication/TwoFactorAuth/Controller/TwoFactorAuthController.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Controller;
1111

1212
use OxidEsales\Eshop\Application\Controller\FrontendController;
13-
use OxidEsales\Eshop\Core\Registry;
14-
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Repository\UserRepositoryInterface;
13+
use OxidEsales\Eshop\Core\UtilsView;
14+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception\OTPValidationException;
1515
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\AuthorizeServiceInterface;
16+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\UserServiceInterface;
1617
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Transput\AuthCodeRequestInterface;
1718

1819
class TwoFactorAuthController extends FrontendController
@@ -27,25 +28,26 @@ class TwoFactorAuthController extends FrontendController
2728

2829
public function __construct(
2930
private readonly AuthorizeServiceInterface $authService,
31+
private readonly UserServiceInterface $userService,
3032
private readonly AuthCodeRequestInterface $authCodeRequest,
33+
private readonly UtilsView $utilsView,
3134
) {
3235
parent::__construct();
3336
}
3437

35-
public function handleOTP(): void
38+
public function handleOTP(): ?string
3639
{
37-
//todo: catch only OTP exception that will be shown to user, maybe some abstract OTP exception?
3840
try {
3941
$this->authService->validate(
4042
$this->authCodeRequest->getCode()
4143
);
4244

43-
//todo: redirect to originally requested page after successful OTP validation
44-
//todo: create correct session for logged in user (from service, handleLogin?)
45-
} catch (\Exception $e) {
46-
//todo: display translated error message to user
47-
Registry::getUtilsView()->addErrorToDisplay($e->getMessage());
45+
$this->userService->finalizeLogin();
46+
} catch (OTPValidationException $e) {
47+
$this->utilsView->addErrorToDisplay($e);
4848
}
49+
50+
return null;
4951
}
5052

5153
public function resendCode(): void

src/Authentication/TwoFactorAuth/Controller/services.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ services:
22
_defaults:
33
autowire: true
44
public: false
5+
bind:
6+
OxidEsales\Eshop\Core\UtilsView: '@=service("OxidEsales\\SecurityModule\\Core\\Registry").getUtilsView()'
57

68
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Controller\TwoFactorAuthController:
79
public: true

src/Authentication/TwoFactorAuth/DTO/User.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class User implements UserInterface
1515
{
1616
public function __construct(
1717
private readonly string $userId,
18+
private readonly string $email,
1819
private readonly int $attempts,
1920
private readonly ?string $code,
2021
private readonly ?DateTimeInterface $expiresAt,
@@ -46,4 +47,9 @@ public function getLastSentAt(): ?DateTimeInterface
4647
{
4748
return $this->lastSentAt;
4849
}
50+
51+
public function getEmail(): string
52+
{
53+
return $this->email;
54+
}
4955
}

src/Authentication/TwoFactorAuth/DTO/UserInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ public function getAttempts(): int;
2020
public function getExpiresAt(): ?DateTimeInterface;
2121

2222
public function getLastSentAt(): ?DateTimeInterface;
23+
24+
public function getEmail(): string;
2325
}

src/Authentication/TwoFactorAuth/Exception/AttemptLimitExceededException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception;
1111

12-
class AttemptLimitExceededException extends \Exception
12+
class AttemptLimitExceededException extends OTPValidationException
1313
{
1414
public function __construct()
1515
{

src/Authentication/TwoFactorAuth/Exception/InvalidCodeException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception;
1111

12-
class InvalidCodeException extends \Exception
12+
class InvalidCodeException extends OTPValidationException
1313
{
1414
public function __construct()
1515
{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception;
11+
12+
use OxidEsales\Eshop\Core\Exception\StandardException;
13+
14+
class OTPValidationException extends StandardException
15+
{
16+
}

src/Authentication/TwoFactorAuth/Exception/TimeExpiredException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception;
1111

12-
class TimeExpiredException extends \Exception
12+
class TimeExpiredException extends OTPValidationException
1313
{
1414
public function __construct()
1515
{

src/Authentication/TwoFactorAuth/Infrastructure/Repository/UserRepository.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use DateTimeImmutable;
1414
use Doctrine\DBAL\Result;
1515
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
16+
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;
1617
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\DTO\UserInterface;
1718
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\DTO\User as UserDTO;
1819
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception\UserNotFoundException;
@@ -23,22 +24,26 @@ class UserRepository implements UserRepositoryInterface
2324
public function __construct(
2425
private readonly UserFactoryInterface $userFactory,
2526
private readonly QueryBuilderFactoryInterface $queryBuilderFactory,
27+
private readonly ContextInterface $context,
2628
) {
2729
}
2830

29-
public function getUserOTPData(string $userName): UserInterface
31+
public function getUserOTPData(string $userId): UserInterface
3032
{
3133
$builder = $this->queryBuilderFactory->create();
3234
$builder->select([
3335
'OXID',
36+
'OXUSERNAME',
3437
'OESMOTPCODE',
3538
'OESMOTPATTEMPTS',
3639
'OESMOTPEXPTIME',
3740
'OESMOTPLASTSENT',
3841
])
3942
->from('oxuser')
40-
->where('oxusername = :userName')
41-
->setParameter('userName', $userName);
43+
->where('oxid = :userId')
44+
->andWhere('oxshopid = :shopId')
45+
->setParameter('userId', $userId)
46+
->setParameter('shopId', $this->context->getCurrentShopId());
4247

4348
/** @var Result $queryResult */
4449
$queryResult = $builder->execute();
@@ -49,7 +54,8 @@ public function getUserOTPData(string $userName): UserInterface
4954

5055
return new UserDTO(
5156
$userData['OXID'],
52-
$userData['OESMOTPATTEMPTS'],
57+
$userData['OXUSERNAME'],
58+
(int)$userData['OESMOTPATTEMPTS'],
5359
$userData['OESMOTPCODE'],
5460
$userData['OESMOTPEXPTIME'] ? new DateTime($userData['OESMOTPEXPTIME']) : null,
5561
$userData['OESMOTPLASTSENT'] ? new DateTime($userData['OESMOTPLASTSENT']) : null,
@@ -93,21 +99,6 @@ public function resetCodeFields(string $userId): void
9399
$userModel->save();
94100
}
95101

96-
public function getUserPasswordHash(string $userName): ?string
97-
{
98-
$builder = $this->queryBuilderFactory->create();
99-
$builder->select('OXPASSWORD')
100-
->from('oxuser')
101-
->where('oxusername = :userName')
102-
->setParameter('userName', $userName);
103-
104-
/** @var Result $queryResult */
105-
$queryResult = $builder->execute();
106-
$userPass = $queryResult->fetchOne();
107-
108-
return $userPass ?: null;
109-
}
110-
111102
public function markOtpAsSent(string $userId): void
112103
{
113104
$userModel = $this->userFactory->create();

src/Authentication/TwoFactorAuth/Infrastructure/Repository/UserRepositoryInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@
1212

1313
interface UserRepositoryInterface
1414
{
15-
public function getUserOTPData(string $userName): UserInterface;
15+
public function getUserOTPData(string $userId): UserInterface;
1616

1717
public function updateAttempts(string $userId, int $attempts): void;
1818

1919
public function resetCodeFields(string $userId): void;
2020

2121
public function addOTPtoUser(string $userId, string $otp, DateTime $expiresAt): bool;
2222

23-
public function getUserPasswordHash(string $userId): ?string;
24-
2523
public function markOtpAsSent(string $userId): void;
2624
}

0 commit comments

Comments
 (0)