Skip to content

Commit 677b6e4

Browse files
committed
OXDEV-10012 Refactor login and redirect the user after login
1 parent 1f62811 commit 677b6e4

17 files changed

Lines changed: 207 additions & 174 deletions

File tree

src/Authentication/TwoFactorAuth/Controller/RedirectView.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/Authentication/TwoFactorAuth/Controller/TwoFactorAuthController.php

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

1212
use OxidEsales\Eshop\Application\Controller\FrontendController;
13-
use OxidEsales\Eshop\Core\Registry;
13+
use OxidEsales\Eshop\Core\Language;
14+
use OxidEsales\Eshop\Core\UtilsView;
1415
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception\OTPValidationException;
1516
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\AuthorizeServiceInterface;
1617
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\UserServiceInterface;
@@ -30,6 +31,8 @@ public function __construct(
3031
private readonly AuthorizeServiceInterface $authService,
3132
private readonly UserServiceInterface $userService,
3233
private readonly AuthCodeRequestInterface $authCodeRequest,
34+
private readonly Language $language,
35+
private readonly UtilsView $utilsView,
3336
) {
3437
parent::__construct();
3538
}
@@ -44,8 +47,8 @@ public function handleOTP(): ?string
4447
$this->userService->finalizeLogin();
4548

4649
} catch (OTPValidationException $e) {
47-
$translatedMessage = Registry::getLang()->translateString($e->getMessage());
48-
Registry::getUtilsView()->addErrorToDisplay($translatedMessage);
50+
$translatedMessage = $this->language->translateString($e->getMessage());
51+
$this->utilsView->addErrorToDisplay($translatedMessage);
4952
}
5053

5154
return null;

src/Authentication/TwoFactorAuth/Controller/services.yaml

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

69
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Controller\TwoFactorAuthController:
710
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/Infrastructure/Repository/UserRepository.php

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,21 @@ public function __construct(
2828
) {
2929
}
3030

31-
public function getUserOTPData(string $userName): UserInterface
31+
public function getUserOTPData(string $userId): UserInterface
3232
{
3333
$builder = $this->queryBuilderFactory->create();
3434
$builder->select([
3535
'OXID',
36+
'OXUSERNAME',
3637
'OESMOTPCODE',
3738
'OESMOTPATTEMPTS',
3839
'OESMOTPEXPTIME',
3940
'OESMOTPLASTSENT',
4041
])
4142
->from('oxuser')
42-
->where('oxusername = :userName')
43+
->where('oxid = :userId')
4344
->andWhere('oxshopid = :shopId')
44-
->setParameter('userName', $userName)
45+
->setParameter('userId', $userId)
4546
->setParameter('shopId', $this->context->getCurrentShopId());
4647

4748
/** @var Result $queryResult */
@@ -53,7 +54,8 @@ public function getUserOTPData(string $userName): UserInterface
5354

5455
return new UserDTO(
5556
$userData['OXID'],
56-
$userData['OESMOTPATTEMPTS'],
57+
$userData['OXUSERNAME'],
58+
(int)$userData['OESMOTPATTEMPTS'],
5759
$userData['OESMOTPCODE'],
5860
$userData['OESMOTPEXPTIME'] ? new DateTime($userData['OESMOTPEXPTIME']) : null,
5961
$userData['OESMOTPLASTSENT'] ? new DateTime($userData['OESMOTPLASTSENT']) : null,
@@ -97,42 +99,6 @@ public function resetCodeFields(string $userId): void
9799
$userModel->save();
98100
}
99101

100-
public function getUserPasswordHash(string $userName): ?string
101-
{
102-
$builder = $this->queryBuilderFactory->create();
103-
$builder->select('OXPASSWORD')
104-
->from('oxuser')
105-
->where('oxusername = :userName')
106-
->andWhere('oxshopid = :shopId')
107-
->setParameter('userName', $userName)
108-
->setParameter('shopId', $this->context->getCurrentShopId());
109-
110-
/** @var Result $queryResult */
111-
$queryResult = $builder->execute();
112-
$userPass = $queryResult->fetchOne();
113-
114-
return $userPass ?: null;
115-
}
116-
117-
public function getUserIdByUserName(string $userName): ?string
118-
{
119-
$builder = $this->queryBuilderFactory->create();
120-
$builder->select('OXID')
121-
->from('oxuser')
122-
->where('oxusername = :userName')
123-
->andWhere('oxshopid = :shopId')
124-
->andWhere('oxactive = 1')
125-
->andWhere("oxpassword != ''")
126-
->setParameter('userName', $userName)
127-
->setParameter('shopId', $this->context->getCurrentShopId());
128-
129-
/** @var Result $queryResult */
130-
$queryResult = $builder->execute();
131-
$userId = $queryResult->fetchOne();
132-
133-
return $userId ?: null;
134-
}
135-
136102
public function markOtpAsSent(string $userId): void
137103
{
138104
$userModel = $this->userFactory->create();

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +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 $userName): ?string;
24-
25-
public function getUserIdByUserName(string $userName): ?string;
26-
2723
public function markOtpAsSent(string $userId): void;
2824
}

src/Authentication/TwoFactorAuth/Service/AuthorizeService.php

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

1212
use OxidEsales\EshopCommunity\Internal\Framework\Session\SessionInterface;
13+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Repository\UserRepositoryInterface;
1314

1415
class AuthorizeService implements AuthorizeServiceInterface
1516
{
@@ -22,6 +23,7 @@ public function __construct(
2223
private VerificationCollectorServiceInterface $verifyCollector,
2324
private NotifierCollectorInterface $notifierCollector,
2425
private ResendOTPServiceInterface $resendOTPService,
26+
private UserRepositoryInterface $userRepository,
2527
private SessionInterface $session
2628
) {
2729
}
@@ -34,9 +36,9 @@ public function validate(string $inputCode): void
3436
$activeVerificator
3537
);
3638

37-
$userName = $this->session->get(self::USER_SESSION_KEY);
39+
$userId = $this->session->get(self::USER_SESSION_KEY);
3840

39-
$verificator->validateCode($userName, $inputCode);
41+
$verificator->validateCode($userId, $inputCode);
4042
}
4143

4244
public function generate(): void
@@ -47,16 +49,17 @@ public function generate(): void
4749
$activeVerificator
4850
);
4951

50-
$userName = $this->session->get(self::USER_SESSION_KEY);
51-
if (!$this->resendOTPService->canSend($userName)) {
52+
$userId = $this->session->get(self::USER_SESSION_KEY);
53+
if (!$this->resendOTPService->canSend($userId)) {
5254
return;
5355
}
5456

55-
$OTPCode = $verificator->generate($userName);
57+
$OTPCode = $verificator->generate($userId);
5658

59+
$user = $this->userRepository->getUserOTPData($userId);
5760
$notifier = $this->notifierCollector->getNotifier('email');
58-
$notifier->notify($userName, $OTPCode);
59-
$this->resendOTPService->markAsSent($userName);
61+
$notifier->notify($user->getEmail(), $OTPCode);
62+
$this->resendOTPService->markAsSent($userId);
6063
}
6164

6265
public function getVerificationUrl(): string

src/Authentication/TwoFactorAuth/Service/ResendOTPService.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,14 @@ public function __construct(
2121
) {
2222
}
2323

24-
public function markAsSent(string $userName): void
24+
public function markAsSent(string $userId): void
2525
{
26-
$otpData = $this->userRepository->getUserOTPData($userName);
27-
28-
$this->userRepository->markOtpAsSent($otpData->getId());
26+
$this->userRepository->markOtpAsSent($userId);
2927
}
3028

31-
public function canSend(string $userName): bool
29+
public function canSend(string $userId): bool
3230
{
33-
$otpData = $this->userRepository->getUserOTPData($userName);
31+
$otpData = $this->userRepository->getUserOTPData($userId);
3432

3533
$lastSentAt = $otpData->getLastSentAt();
3634
if ($lastSentAt === null) {

src/Authentication/TwoFactorAuth/Service/ResendOTPServiceInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
interface ResendOTPServiceInterface
1111
{
12-
public function markAsSent(string $userName): void;
12+
public function markAsSent(string $userId): void;
1313

14-
public function canSend(string $userName): bool;
14+
public function canSend(string $userId): bool;
1515
}

0 commit comments

Comments
 (0)