Skip to content

Commit b113a13

Browse files
committed
OXDEV-9927 Add user service to handle OTP on login
1 parent d076eca commit b113a13

10 files changed

Lines changed: 55 additions & 12 deletions

File tree

metadata.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
'controllers' => [
4444
'captcha' => \OxidEsales\SecurityModule\Captcha\Controller\CaptchaController::class,
4545
'password' => \OxidEsales\SecurityModule\PasswordPolicy\Controller\PasswordAjaxController::class,
46-
'oauth' => \OxidEsales\SecurityModule\Authentication\OAuth2\Controller\OAuthController::class
46+
'oauth' => \OxidEsales\SecurityModule\Authentication\OAuth2\Controller\OAuthController::class,
47+
'twofactorauth' => \OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Controller\TwoFactorAuthController::class,
4748
],
4849
'templates' => [
4950
],

src/Authentication/TwoFactorAuth/Controller/TwoFactorAuthController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ private function handleOTP(): void
1717

1818
$this->getService(OTPServiceInterface::class)->validateCode($user, $code);
1919
}
20+
21+
public function render()
22+
{
23+
return parent::render(); // TODO: Change the autogenerated stub
24+
}
2025
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99

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

12+
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
13+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\DataObject\UserInterface;
1214
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Factory\UserFactoryInterface;
1315

1416
class UserRepository implements UserRepositoryInterface
1517
{
1618
public function __construct(
17-
private UserFactoryInterface $userFactory
19+
private UserFactoryInterface $userFactory,
20+
private readonly QueryBuilderFactoryInterface $queryBuilderFactory,
1821
) {
1922
}
2023

@@ -53,4 +56,15 @@ public function resetCodeFields(string $userId): void
5356
]);
5457
$userModel->save();
5558
}
59+
60+
public function getUserPasswordHash(string $userName): string
61+
{
62+
$qb = $this->queryBuilderFactory->create();
63+
$qb->select('OXPASSWORD')
64+
->from('oxuser')
65+
->where('oxusername = :userName')
66+
->setParameter('userName', $userName);
67+
68+
return $qb->execute()->fetchOne();
69+
}
5670
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ public function updateAttempts(string $userId, int $attempts): int;
1414
public function resetCodeFields(string $userId): void;
1515

1616
public function addOTPtoUser(string $userId, string $otp, int $expiresAt): bool;
17+
18+
public function getUserPasswordHash(string $userId): string;
1719
}

src/Authentication/TwoFactorAuth/Infrastructure/Repository/services.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ services:
55

66
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Repository\UserRepositoryInterface:
77
class: OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Repository\UserRepository
8+
public: true

src/Authentication/TwoFactorAuth/Service/AuthorizeService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
class AuthorizeService implements AuthorizeServiceInterface
1313
{
14-
public function validate()
14+
public function validate(): void
1515
{
1616
//todo: call correct provider service to validate the code
1717
}
1818

19-
public function generate($userName)
19+
public function generate($userName): void
2020
{
2121
//todo: call correct provider service to generate the code
2222
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
<?php
22

3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
38
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service;
49

510
interface AuthorizeServiceInterface
611
{
12+
public function validate(): void;
13+
14+
public function generate($userName): void;
715
}

src/Authentication/TwoFactorAuth/Service/services.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ services:
1414
class: OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\AuthorizeService
1515
public: true
1616

17+
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\UserServiceInterface:
18+
class: OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\UserService
19+
public: true
20+
1721
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\NotifierCollectorInterface:
1822
class: OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\NotifierCollector
1923
public: true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
imports:
22
- { resource: Infrastructure/services.yaml }
33
- { resource: Service/services.yaml }
4+
- { resource: Infrastructure/services.yaml }

src/Shared/Model/User.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use OxidEsales\Eshop\Core\Exception\InputException;
1313
use OxidEsales\Eshop\Core\Exception\UserException;
1414
use OxidEsales\Eshop\Core\Registry;
15-
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\AuthorizeService;
15+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\UserServiceInterface;
1616
use OxidEsales\SecurityModule\Captcha\Captcha\Image\Exception\CaptchaValidateException as ImageCaptchaException;
1717
use OxidEsales\SecurityModule\Captcha\Captcha\HoneyPot\Exception\CaptchaValidateException as HoneyPotCaptchaException;
1818
use OxidEsales\SecurityModule\Captcha\Service\CaptchaServiceInterface;
@@ -77,16 +77,23 @@ public function login($userName, $password, $setSessionCookie = false): bool
7777
}
7878
}
7979

80-
$login = parent::login($userName, $password, $setSessionCookie);
81-
82-
//todo: $userService->handleLogin($login);
80+
if (!$this->isOTPEnabled() || $this->isAdmin()) {
81+
return parent::login($userName, $password, $setSessionCookie);
82+
}
8383

84-
if (!$this->isOTPEnabled()) {
85-
return $login;
84+
$userService = $this->getService(UserServiceInterface::class);
85+
if (!$userService->checkPassword($password, $userName)) {
86+
return false; // invalid login
8687
}
8788

88-
$authorizeService = $this->getService(AuthorizeService::class);
89-
$authorizeService->generate($userName); //save to db and send to email
89+
$userService->handleLogin($userName);
90+
Registry::getSession()->setVariable('pending_otp_user', $this->getId());
91+
Registry::getUtils()->redirect(Registry::getConfig()->getShopHomeUrl() . 'cl=twofactorauth');
92+
93+
// We do NOT return success and do NOT create session
94+
return false;
95+
// $authorizeService = $this->getService(AuthorizeService::class);
96+
// $authorizeService->generate($userName); //save to db and send to email
9097
// redirect to template for code -> submit
9198
// validate code on submit
9299
//redirect to whatever

0 commit comments

Comments
 (0)