Skip to content

Commit 6473b82

Browse files
OXDEV-9927 Handle user session
1 parent b7c436b commit 6473b82

10 files changed

Lines changed: 19 additions & 28 deletions

File tree

src/Authentication/OAuth2/Infrastructure/Factory/OAuth2UserDTOFactory.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public function createFromFacebookUser(FacebookUser $facebookUser): OAuth2UserDT
2525
);
2626
}
2727

28-
//TODO: use one factory for all providers
2928
public function createFromGoogleUser(GoogleUser $googleUser): OAuth2UserDTOInterface
3029
{
3130
return new OAuth2UserDTO(

src/Authentication/TwoFactorAuth/Controller/TwoFactorAuthController.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,10 @@ public function handleOTP(): void
3737

3838
public function generate(): void
3939
{
40-
//todo: use session to get email/username
41-
$username = uniqid();
42-
4340
//todo: stop execution if not ajax
4441
//todo: prevent spam by rate limiting
4542
//todo: should return json response with success or error message
4643
$authorizeService = $this->getService(AuthorizeServiceInterface::class);
47-
$authorizeService->generate($username);
44+
$authorizeService->generate();
4845
}
4946
}

src/Authentication/TwoFactorAuth/DTO/User.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public function getAttempts(): ?int
3838

3939
public function getExpiresAt(): ?DateTime
4040
{
41-
//todo: possible bug - should be null if not set
4241
return $this->expiresAt;
4342
}
4443
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public function __construct(
2525
public function getUserOTPData(string $userName): UserDTO
2626
{
2727
//todo: exception if not found
28-
//todo: separate method for id and username?
2928
$builder = $this->queryBuilderFactory->create();
3029
$builder->select([
3130
'OXID',
@@ -35,7 +34,6 @@ public function getUserOTPData(string $userName): UserDTO
3534
])
3635
->from('oxuser')
3736
->where('oxusername = :userName')
38-
->orWhere('oxid = :userName')
3937
->setParameter('userName', $userName);
4038

4139
$userData = $builder->execute()->fetchAssociative();

src/Authentication/TwoFactorAuth/Service/AuthorizeService.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
class AuthorizeService implements AuthorizeServiceInterface
1515
{
16+
public const USER_SESSION_KEY = 'pending_authorized_user';
17+
1618
public function __construct(
1719
private ModuleSettingsServiceInterface $moduleSettings,
1820
private VerificationCollectorServiceInterface $verificationCollectorService,
@@ -29,14 +31,15 @@ public function validate(string $inputCode): void
2931
$activeVerificator
3032
);
3133

32-
//todo: use session to get email/username or userId
33-
$userName = $this->session->get('pending_otp_user');
34+
$userName = $this->session->get(self::USER_SESSION_KEY);
3435

3536
$verificator->validateCode($userName, $inputCode);
3637
}
3738

38-
public function generate(string $userName): void
39+
public function generate(): void
3940
{
41+
$userName = $this->session->get(self::USER_SESSION_KEY);
42+
4043
$activeVerificator = $this->moduleSettings->getTwoFactorAuthType();
4144

4245
$verificator = $this->verificationCollectorService->getVerificator(
@@ -45,7 +48,6 @@ public function generate(string $userName): void
4548

4649
$OTPCode = $verificator->generate($userName);
4750

48-
//todo: module setting?
4951
$notifier = $this->notifierCollectorService->getNotifier('email');
5052
$notifier->notify($userName, $OTPCode);
5153
}

src/Authentication/TwoFactorAuth/Service/AuthorizeServiceInterface.php

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

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

1616
public function getVerificationUrl(): string;
1717
}

src/Authentication/TwoFactorAuth/Service/UserService.php

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

1212
use OxidEsales\EshopCommunity\Internal\Domain\Authentication\Bridge\PasswordServiceBridgeInterface;
1313
use OxidEsales\EshopCommunity\Internal\Framework\Session\SessionInterface;
14-
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Factory\UserFactoryInterface;
1514
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Repository\UserRepositoryInterface;
1615

1716
class UserService implements UserServiceInterface
1817
{
1918
public function __construct(
2019
private AuthorizeServiceInterface $authorizeService,
21-
private UserFactoryInterface $userFactory,
2220
private UserRepositoryInterface $userRepository,
2321
private PasswordServiceBridgeInterface $passwordServiceBridge,
2422
private SessionInterface $session
@@ -27,20 +25,19 @@ public function __construct(
2725

2826
public function handleLogin($userName): void
2927
{
30-
$this->authorizeService->generate($userName);
28+
$this->session->set(AuthorizeService::USER_SESSION_KEY, $userName);
3129

32-
//todo: this should be handled by authorize service?
33-
$this->session->set('pending_otp_user', $userName);
30+
$this->authorizeService->generate();
3431
// redirect to controller and rende template
3532
// use template renderer to render the template
3633
}
3734

3835
public function checkPassword(string $userName, string $password): bool
3936
{
40-
// var_dump($userId);
4137
// $userModel = $this->userFactory->create();
4238
// $userModel->load($userId);
4339

40+
//todo: got exception if user not found
4441
$userPasswordHash = $this->userRepository->getUserPasswordHash($userName);
4542
// var_dump($userPasswordHash);
4643
return $this->passwordServiceBridge

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public function getName(): string
2929
return 'otp';
3030
}
3131

32-
public function validateCode(string $userId, string $inputCode): void
32+
public function validateCode(string $userName, string $inputCode): void
3333
{
3434
//todo: userId from parameter or DTO
35-
$otpData = $this->userRepository->getUserOTPData($userId);
35+
$otpData = $this->userRepository->getUserOTPData($userName);
3636

3737
$this->otpValidator->checkLoginAttempts($otpData->getAttempts());
3838
$this->otpValidator->checkExpirationTime($otpData->getExpiresAt());

src/Shared/Model/User.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public function login($userName, $password, $setSessionCookie = false): bool
8787
return false; // invalid login
8888
}
8989

90-
//todo: re-login will send new OTP, should we avoid that?
9190
$userService->handleLogin($userName);
9291

9392
//todo: redirect to correct page decided by verificator method? (otp: otp page, TOTP: totp page, etc)

tests/Unit/Authentication/TwoFactorAuth/Service/Verificator/OTP/OTPVerificatorTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testVerificatorName()
3131

3232
public function testInvalidCode(): void
3333
{
34-
$userId = uniqid();
34+
$userName = uniqid();
3535
$attempts = rand();
3636
$code = uniqid();
3737

@@ -55,12 +55,12 @@ public function testInvalidCode(): void
5555

5656
$this->expectException(InvalidCodeException::class);
5757

58-
$otpService->validateCode($userId, $code);
58+
$otpService->validateCode($userName, $code);
5959
}
6060

6161
public function testExpiredCode(): void
6262
{
63-
$userId = uniqid();
63+
$userName = uniqid();
6464
$attempts = rand();
6565
$code = uniqid();
6666

@@ -84,12 +84,12 @@ public function testExpiredCode(): void
8484

8585
$this->expectException(TimeExpiredException::class);
8686

87-
$otpService->validateCode($userId, $code);
87+
$otpService->validateCode($userName, $code);
8888
}
8989

9090
public function testAttemptsCode(): void
9191
{
92-
$userId = uniqid();
92+
$userName = uniqid();
9393
$attempts = rand();
9494
$code = uniqid();
9595

@@ -113,7 +113,7 @@ public function testAttemptsCode(): void
113113

114114
$this->expectException(AttemptLimitExceededException::class);
115115

116-
$otpService->validateCode($userId, $code);
116+
$otpService->validateCode($userName, $code);
117117
}
118118

119119
public function getSut(

0 commit comments

Comments
 (0)