Skip to content

Commit 50fc1a0

Browse files
OXDEV-9927 Create VerificatorCollector and move classes
1 parent 8aee630 commit 50fc1a0

31 files changed

Lines changed: 500 additions & 227 deletions

metadata.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177
'group' => 'two_factor_auth',
178178
'name' => TwoFactorAuthModuleSettings::TWO_FACTOR_TYPE,
179179
'type' => 'select',
180-
'constraints' => 'otp|totp|both',
180+
'constraints' => 'otp|totp',
181181
'value' => ''
182182
],
183183
],

src/Authentication/TwoFactorAuth/Controller/TwoFactorAuthController.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
use OxidEsales\Eshop\Application\Controller\FrontendController;
66
use OxidEsales\Eshop\Core\Registry;
7-
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\Provider\OTP\Service\OTPServiceInterface;
7+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\AuthorizeServiceInterface;
88

99
class TwoFactorAuthController extends FrontendController
1010
{
11-
1211
protected $_sThisTemplate = '@oe_security_module/templates/two_factor_auth';
1312

1413
private function handleOTP(): void
@@ -23,6 +22,9 @@ private function handleOTP(): void
2322

2423
public function render()
2524
{
26-
return parent::render(); // TODO: Change the autogenerated stub
25+
$authorizeService = $this->getService(AuthorizeServiceInterface::class);
26+
$authorizeService->generate();
27+
28+
exit;
2729
}
2830
}

src/Authentication/TwoFactorAuth/DTO/User.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,23 @@
99

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

12-
use DateTimeInterface;
12+
use DateTimeImmutable;
1313

1414
class User implements UserInterface
1515
{
1616
public function __construct(
17+
private readonly string $userId,
1718
private readonly ?string $code,
1819
private readonly ?int $attempts,
19-
private readonly ?DateTimeInterface $expiresAt,
20+
private readonly ?int $expiresAt,
2021
) {
2122
}
2223

24+
public function getId(): string
25+
{
26+
return $this->userId;
27+
}
28+
2329
public function getCode(): ?string
2430
{
2531
return $this->code;
@@ -30,8 +36,10 @@ public function getAttempts(): ?int
3036
return $this->attempts;
3137
}
3238

33-
public function getExpiresAt(): ?DateTimeInterface
39+
public function getExpiresAt(): ?DateTimeImmutable
3440
{
35-
return $this->expiresAt;
41+
$dateTime = new DateTimeImmutable();
42+
43+
return $dateTime->setTimestamp($this->expiresAt);
3644
}
3745
}

src/Authentication/TwoFactorAuth/DTO/UserInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77

88
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\DTO;
99

10-
use DateTimeInterface;
10+
use DateTimeImmutable;
1111

1212
interface UserInterface
1313
{
14+
public function getId(): string;
15+
1416
public function getCode(): ?string;
1517

1618
public function getAttempts(): ?int;
1719

18-
public function getExpiresAt(): ?DateTimeInterface;
20+
public function getExpiresAt(): ?DateTimeImmutable;
1921
}
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 VerificatorNotFoundException extends \Exception
13+
{
14+
}

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

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

1010
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Repository;
1111

12+
use DateTime;
1213
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
13-
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\DataObject\UserInterface;
14+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\DTO\User as UserDTO;
1415
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Factory\UserFactoryInterface;
1516

1617
class UserRepository implements UserRepositoryInterface
@@ -21,21 +22,36 @@ public function __construct(
2122
) {
2223
}
2324

25+
public function getUserOTPData(string $userId): UserDTO
26+
{
27+
//todo: exception if not found
28+
//todo: use query builder
29+
$userModel = $this->userFactory->create();
30+
$userModel->load($userId);
31+
32+
return new UserDTO(
33+
$userModel->getId(),
34+
$userModel->getFieldData('OTPCODE'),
35+
(int) $userModel->getFieldData('OTPATTEMPTS'),
36+
new DateTime($userModel->getFieldData('OTPEXPIRETIME'))
37+
);
38+
}
39+
2440
public function addOTPtoUser(string $userId, string $otp, int $expiresAt): bool
2541
{
2642
$userModel = $this->userFactory->create();
2743
$userModel->load($userId);
2844
$userModel->assign([
2945
'OESMOTPCODE' => $otp,
30-
'OESMOTPEXPTIME' => $expiresAt,
46+
'OESMOTPEXPTIME' => $expiresAt,
3147
'OESMOTPATTEMPTS' => 0,
3248
]);
3349
$userModel->save();
3450

3551
return true;
3652
}
3753

38-
public function updateAttempts(string $userId, int $attempts): int
54+
public function updateAttempts(string $userId, int $attempts): void
3955
{
4056
$userModel = $this->userFactory->create();
4157
$userModel->load($userId);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77

88
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Repository;
99

10+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\DTO\User as UserDTO;
11+
1012
interface UserRepositoryInterface
1113
{
12-
public function updateAttempts(string $userId, int $attempts): int;
14+
public function getUserOTPData(string $userId): UserDTO;
15+
16+
public function updateAttempts(string $userId, int $attempts): void;
1317

1418
public function resetCodeFields(string $userId): void;
1519

src/Authentication/TwoFactorAuth/Service/AuthorizeService.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,37 @@
1111

1212
class AuthorizeService implements AuthorizeServiceInterface
1313
{
14+
public function __construct(
15+
private ModuleSettingsServiceInterface $moduleSettings,
16+
private VerificationCollectorServiceInterface $verificationCollectorService,
17+
private NotifierCollectorInterface $notifierCollectorService,
18+
) {
19+
}
20+
1421
public function validate(): void
1522
{
16-
//todo: call correct provider service to validate the code
23+
$activeVerificator = $this->moduleSettings->getTwoFactorAuthType();
24+
25+
$verificator = $this->verificationCollectorService->getVerificator(
26+
$activeVerificator
27+
);
28+
//todo: use transput to get the code from request
29+
//todo: use session to get user id
30+
$verificator->validateCode(uniqid(), uniqid());
1731
}
1832

19-
public function generate($userName): void
33+
public function generate(): void
2034
{
21-
//todo: call correct provider service to generate the code
35+
$activeVerificator = $this->moduleSettings->getTwoFactorAuthType();
36+
37+
$verificator = $this->verificationCollectorService->getVerificator(
38+
$activeVerificator
39+
);
40+
//todo: use session to get user id
41+
$OTPCode = $verificator->generate(uniqid());
42+
43+
//todo: module setting?
44+
$notifier = $this->notifierCollectorService->getNotifier('email');
45+
$notifier->notify('localhost@localhost.local', $OTPCode);
2246
}
2347
}

src/Authentication/TwoFactorAuth/Service/AuthorizeServiceInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ interface AuthorizeServiceInterface
1111
{
1212
public function validate(): void;
1313

14-
public function generate($userName): void;
14+
public function generate(): void;
1515
}

src/Authentication/TwoFactorAuth/Service/Provider/OTP/Generator/OTPGeneratorInterface.php

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

0 commit comments

Comments
 (0)