Skip to content

Commit a022b93

Browse files
committed
Merge branch 'b-7.4.x-generate-exceptions-OXDEV-9992' into b-7.4.x-2fa-OXDEV-9078
2 parents 8a3f8e6 + b76cc70 commit a022b93

28 files changed

Lines changed: 951 additions & 155 deletions

File tree

metadata.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@
178178
'name' => TwoFactorAuthModuleSettings::TWO_FACTOR_TYPE,
179179
'type' => 'select',
180180
'constraints' => 'otp|totp',
181-
'value' => ''
181+
'value' => 'otp'
182182
],
183183
],
184184
];

src/Authentication/TwoFactorAuth/Controller/TwoFactorAuthController.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,4 @@ public function handleOTP(): void
4242
Registry::getUtilsView()->addErrorToDisplay($e->getMessage());
4343
}
4444
}
45-
46-
public function generate(): void
47-
{
48-
//todo: stop execution if not ajax
49-
//todo: prevent spam by rate limiting
50-
//todo: should return json response with success or error message
51-
$authorizeService = $this->getService(AuthorizeServiceInterface::class);
52-
$authorizeService->generate();
53-
}
5445
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
class UserNotFoundException extends \Exception
13+
{
14+
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
use Doctrine\DBAL\Result;
1414
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
1515
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\DTO\User as UserDTO;
16+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception\UserNotFoundException;
1617
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Factory\UserFactoryInterface;
17-
use RuntimeException;
1818

1919
class UserRepository implements UserRepositoryInterface
2020
{
@@ -26,7 +26,6 @@ public function __construct(
2626

2727
public function getUserOTPData(string $userName): UserDTO
2828
{
29-
//todo: exception if not found
3029
$builder = $this->queryBuilderFactory->create();
3130
$builder->select([
3231
'OXID',
@@ -42,15 +41,14 @@ public function getUserOTPData(string $userName): UserDTO
4241
$queryResult = $builder->execute();
4342
$userData = $queryResult->fetchAssociative();
4443
if (!$userData) {
45-
//todo: throw correct exception
46-
throw new RuntimeException('User not found');
44+
throw new UserNotFoundException();
4745
}
4846

4947
return new UserDTO(
5048
$userData['OXID'],
5149
$userData['OESMOTPATTEMPTS'],
5250
$userData['OESMOTPCODE'],
53-
new DateTime($userData['OESMOTPEXPTIME'])
51+
$userData['OESMOTPEXPTIME'] ? new DateTime($userData['OESMOTPEXPTIME']) : null
5452
);
5553
}
5654

@@ -90,7 +88,7 @@ public function resetCodeFields(string $userId): void
9088
$userModel->save();
9189
}
9290

93-
public function getUserPasswordHash(string $userName): string
91+
public function getUserPasswordHash(string $userName): ?string
9492
{
9593
$builder = $this->queryBuilderFactory->create();
9694
$builder->select('OXPASSWORD')
@@ -100,6 +98,8 @@ public function getUserPasswordHash(string $userName): string
10098

10199
/** @var Result $queryResult */
102100
$queryResult = $builder->execute();
103-
return $queryResult->fetchOne();
101+
$userPass = $queryResult->fetchOne();
102+
103+
return $userPass ?: null;
104104
}
105105
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ 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;
23+
public function getUserPasswordHash(string $userId): ?string;
2424
}

src/Authentication/TwoFactorAuth/Service/ModuleSettingsService.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ public function getTwoFactorAuthType(): string
3333
return $this->getStringValue(self::TWO_FACTOR_TYPE);
3434
}
3535

36+
public function saveIsTwoFactorAuthEnabled(bool $value): void
37+
{
38+
$this->moduleSettingService->saveBoolean(
39+
self::ACTIVE,
40+
$value,
41+
Module::MODULE_ID
42+
);
43+
}
44+
3645
private function getStringValue(string $key): string
3746
{
3847
return $this->moduleSettingService->getString(

src/Authentication/TwoFactorAuth/Service/ModuleSettingsServiceInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ interface ModuleSettingsServiceInterface
1212
public function isTwoFactorAuthEnabled(): bool;
1313

1414
public function getTwoFactorAuthType(): string;
15+
16+
public function saveIsTwoFactorAuthEnabled(bool $value): void;
1517
}

src/Authentication/TwoFactorAuth/Service/UserService.php

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

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

12-
use OxidEsales\Eshop\Core\Registry;
12+
use OxidEsales\Eshop\Core\Request;
13+
use OxidEsales\Eshop\Core\Utils;
1314
use OxidEsales\EshopCommunity\Internal\Domain\Authentication\Bridge\PasswordServiceBridgeInterface;
1415
use OxidEsales\EshopCommunity\Internal\Framework\Session\SessionInterface;
1516
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Repository\UserRepositoryInterface;
@@ -20,7 +21,9 @@ public function __construct(
2021
private AuthorizeServiceInterface $authorizeService,
2122
private UserRepositoryInterface $userRepository,
2223
private PasswordServiceBridgeInterface $pwdServiceBridge,
23-
private SessionInterface $session
24+
private SessionInterface $session,
25+
private Request $request,
26+
private Utils $utils,
2427
) {
2528
}
2629

@@ -29,21 +32,27 @@ public function handleLogin(string $userName): void
2932
$this->session->set(AuthorizeService::USER_SESSION_KEY, $userName);
3033
$this->session->set(
3134
AuthorizeService::OTP_TARGET_URL,
32-
//todo: bind registry
33-
Registry::getRequest()->getRequestUrl()
35+
$this->request->getRequestUrl()
3436
);
3537

38+
//todo: prevent spam by rate limiting
3639
$this->authorizeService->generate();
3740

38-
//todo: return full url
3941
$redirectUrl = $this->authorizeService->getVerificationUrl();
40-
Registry::getUtils()->redirect(Registry::getConfig()->getShopHomeUrl() . 'cl=' . $redirectUrl);
42+
$this->utils->redirect($redirectUrl);
4143
}
4244

4345
public function checkPassword(string $userName, string $password): bool
4446
{
45-
//todo: got exception if user not found
46-
$userPasswordHash = $this->userRepository->getUserPasswordHash($userName);
47+
try {
48+
$userPasswordHash = $this->userRepository->getUserPasswordHash($userName);
49+
} catch (\Throwable $e) {
50+
return false;
51+
}
52+
53+
if ($userPasswordHash === null) {
54+
return false;
55+
}
4756

4857
return $this->pwdServiceBridge
4958
->verifyPassword($password, $userPasswordHash);

src/Authentication/TwoFactorAuth/Service/Verificator/OTP/OTPVerificator.php

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

1010
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\Verificator\OTP;
1111

12-
use OxidEsales\Eshop\Core\Registry;
12+
use OxidEsales\Eshop\Core\Config;
1313
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception\InvalidCodeException;
1414
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Repository\UserRepositoryInterface;
1515
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\Verificator\OTP\Generator\OTPGeneratorInterface;
@@ -22,6 +22,7 @@ public function __construct(
2222
private OTPGeneratorInterface $otpGenerator,
2323
private OTPValidatorInterface $otpValidator,
2424
private UserRepositoryInterface $userRepository,
25+
private Config $config,
2526
) {
2627
}
2728

@@ -50,10 +51,7 @@ public function validateCode(string $userName, string $inputCode): void
5051

5152
public function generate(string $userName): string
5253
{
53-
//todo: userId from parameter or DTO
5454
$otpData = $this->userRepository->getUserOTPData($userName);
55-
56-
//todo: stop if user not found?
5755
//todo: wait time between generations, in case of abuse like
5856
//spamming the generate button or
5957
//user hit limit and try to generate new code to bypass it
@@ -63,6 +61,6 @@ public function generate(string $userName): string
6361

6462
public function getVerificationUrl(): string
6563
{
66-
return 'twofactorauth';
64+
return $this->config->getShopHomeUrl() . 'cl=twofactorauth';
6765
}
6866
}

src/Authentication/TwoFactorAuth/Service/Verificator/services.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ services:
55
_defaults:
66
autowire: true
77
public: false
8+
bind:
9+
OxidEsales\Eshop\Core\Config: '@=service("OxidEsales\\SecurityModule\\Core\\Registry").getConfig()'
810

911
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\Verificator\OTP\OTPVerificator:
1012
tags: [ 'security.twofa.tag.verificator' ]

0 commit comments

Comments
 (0)