Skip to content

Commit cc235a5

Browse files
TitaKolevatkcreateit
authored andcommitted
OXDEV-10037 Refactor and improve code after review
1 parent 33ec3ef commit cc235a5

38 files changed

Lines changed: 678 additions & 194 deletions

assets/out/src/js/module/resend-otp.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,22 @@ export class ResendOtp {
1010
submitButtonId = 'auth_submit',
1111
attemptsDisplayId = 'remaining-attempts',
1212
codeInputId = 'auth_code',
13-
maxAttempts = 5
13+
maxAttempts = 5,
1414
} = options;
1515

1616
this.btn = button;
1717
this.hint = document.getElementById(hintId);
1818
this.submitBtn = document.getElementById(submitButtonId);
1919
this.attemptsDisplay = document.getElementById(attemptsDisplayId);
2020
this.codeInput = document.getElementById(codeInputId);
21-
this.maxAttempts = maxAttempts;
2221

2322
this.cooldownSeconds = Number(button.dataset.cooldown || 60);
23+
this.maxAttempts = maxAttempts;
2424
this.url = button.dataset.url;
25+
this.textDefault = button.dataset.textDefault;
26+
this.textSending = button.dataset.textSending;
27+
this.textError = button.dataset.textError;
28+
this.textCountdown = button.dataset.textCountdown;
2529
this.storageKey = `otp_resend_until_${this.url}`;
2630

2731
this.timer = null;
@@ -39,10 +43,9 @@ export class ResendOtp {
3943
}
4044

4145
async resend() {
42-
console.log('Resend OTP code requested');
4346
if (this.btn.disabled) return;
4447

45-
this.lock('Sending…');
48+
this.lock(this.textSending);
4649

4750
try {
4851
await fetch(this.url, {
@@ -59,7 +62,7 @@ export class ResendOtp {
5962
} catch (e) {
6063
console.error(e);
6164
this.unlock();
62-
this.setHint('Could not resend code.');
65+
this.setHint(this.textError);
6366
}
6467
}
6568

@@ -90,7 +93,6 @@ export class ResendOtp {
9093

9194
startCooldown(until) {
9295
clearInterval(this.timer);
93-
console.log(`Starting OTP resend cooldown until ${new Date(until).toISOString()}`);
9496
const tick = () => {
9597
const remaining = Math.ceil((until - Date.now()) / 1000);
9698

@@ -99,7 +101,7 @@ export class ResendOtp {
99101
this.unlock();
100102
clearInterval(this.timer);
101103
} else {
102-
this.lock(`Resend in ${remaining}s`);
104+
this.lock(this.textCountdown.replace('%d', remaining));
103105
}
104106
};
105107

@@ -114,7 +116,7 @@ export class ResendOtp {
114116

115117
unlock() {
116118
this.btn.disabled = false;
117-
this.btn.textContent = 'Resend code';
119+
this.btn.textContent = this.textDefault;
118120
}
119121

120122
setHint(text) {
@@ -134,7 +136,6 @@ export class ResendOtp {
134136
}
135137

136138
restoreOnRefresh() {
137-
console.log('Restoring OTP resend cooldown if needed');
138139
const until = this.getStoredUntil();
139140
if (until && until > Date.now()) {
140141
this.startCooldown(until);

metadata.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
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,
4746
],
4847
'templates' => [
4948
],

src/Authentication/OAuth2/Controller/OAuthController.php

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,43 @@
88
namespace OxidEsales\SecurityModule\Authentication\OAuth2\Controller;
99

1010
use OxidEsales\Eshop\Application\Controller\FrontendController;
11-
use OxidEsales\Eshop\Core\Registry;
12-
use OxidEsales\SecurityModule\Authentication\OAuth2\Service\ProviderCollectorInterface;
13-
use OxidEsales\SecurityModule\Authentication\OAuth2\Service\UserServiceInterface;
14-
use OxidEsales\SecurityModule\Authentication\Session\SessionKeys;
11+
use OxidEsales\Eshop\Core\Utils;
12+
use OxidEsales\SecurityModule\Authentication\OAuth2\Service\AuthenticationServiceInterface;
13+
use OxidEsales\SecurityModule\Authentication\OAuth2\Transput\OAuthRequestInterface;
14+
use OxidEsales\SecurityModule\Authentication\Service\InternalRedirectServiceInterface;
1515

1616
class OAuthController extends FrontendController
1717
{
18+
public function __construct(
19+
private readonly AuthenticationServiceInterface $authenticationService,
20+
private readonly OAuthRequestInterface $oauthRequest,
21+
private readonly InternalRedirectServiceInterface $redirectService,
22+
private readonly Utils $utils,
23+
) {
24+
parent::__construct();
25+
}
26+
1827
public function login(): void
1928
{
20-
$providerName = $_GET['provider'] ?? '';
21-
22-
$providerCollector = $this->getService(ProviderCollectorInterface::class);
29+
$authorizationUrl = $this->authenticationService->getAuthorizationUrl(
30+
$this->oauthRequest->getProvider()
31+
);
2332

24-
$provider = $providerCollector->getProvider($providerName);
25-
26-
Registry::getUtils()->redirect($provider->getAuthorizationUrl());
33+
$this->utils->redirect($authorizationUrl);
2734
}
2835

2936
public function redirect(): void
3037
{
31-
$providerName = $_GET['provider'] ?? '';
32-
33-
$provider = $this
34-
->getService(ProviderCollectorInterface::class)
35-
->getProvider($providerName);
36-
37-
$accessToken = $provider->getAccessToken($_GET['code']);
38-
39-
$userDTO = $provider->getUserInfo($accessToken);
40-
41-
$this
42-
->getService(UserServiceInterface::class)
43-
->login($userDTO);
44-
45-
$redirectUrl = Registry::getSession()->getVariable(SessionKeys::AUTH_REDIRECT_URL);
46-
if (!$redirectUrl) {
47-
$redirectUrl = Registry::getConfig()->getShopHomeUrl();
38+
if ($this->oauthRequest->hasError()) {
39+
$this->utils->redirect($this->redirectService->getRedirectUrl(), false);
40+
return;
4841
}
4942

50-
Registry::getUtils()->redirect($redirectUrl, false);
43+
$this->authenticationService->handleCallback(
44+
$this->oauthRequest->getProvider(),
45+
$this->oauthRequest->getCode()
46+
);
47+
48+
$this->utils->redirect($this->redirectService->getRedirectUrl(), false);
5149
}
5250
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
services:
2+
_defaults:
3+
autowire: true
4+
public: false
5+
bind:
6+
OxidEsales\Eshop\Core\Utils: '@=service("OxidEsales\\SecurityModule\\Core\\Registry").getUtils()'
7+
8+
OxidEsales\SecurityModule\Authentication\OAuth2\Controller\OAuthController:
9+
public: true
10+
tags:
11+
- { name: 'oxid.view_controller', controller_key: 'oauth' }

src/Authentication/OAuth2/Infrastructure/Provider/Facebook/FacebookAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function getAuthorizationUrl(array $options = []): string
4646
return $this->provider->getAuthorizationUrl($options);
4747
}
4848

49-
public function getAccessToken(string $code): AccessTokenInterface
49+
public function getAccessToken(#[\SensitiveParameter] string $code): AccessTokenInterface
5050
{
5151
return $this->provider->getAccessToken('authorization_code', ['code' => $code]);
5252
}

src/Authentication/OAuth2/Infrastructure/Provider/Google/GoogleAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function getAuthorizationUrl(array $options = []): string
4747
return $this->provider->getAuthorizationUrl($options);
4848
}
4949

50-
public function getAccessToken(string $code): AccessTokenInterface
50+
public function getAccessToken(#[\SensitiveParameter] string $code): AccessTokenInterface
5151
{
5252
return $this->provider->getAccessToken('authorization_code', [
5353
'code' => $code,

src/Authentication/OAuth2/Infrastructure/Provider/ProviderAdapterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function getAuthorizationUrl(array $options = []): string;
3131
/**
3232
* Exchange the authorization code for an access token.
3333
*/
34-
public function getAccessToken(string $code): AccessTokenInterface;
34+
public function getAccessToken(#[\SensitiveParameter] string $code): AccessTokenInterface;
3535

3636
/**
3737
* Fetch user information (claims) from the provider using the access token.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\OAuth2\Service;
11+
12+
readonly class AuthenticationService implements AuthenticationServiceInterface
13+
{
14+
public function __construct(
15+
private ProviderCollectorInterface $providerCollector,
16+
private UserServiceInterface $userService,
17+
) {
18+
}
19+
20+
public function getAuthorizationUrl(string $providerName): string
21+
{
22+
$provider = $this->providerCollector->getProvider($providerName);
23+
24+
return $provider->getAuthorizationUrl();
25+
}
26+
27+
public function handleCallback(string $providerName, #[\SensitiveParameter] string $code): void
28+
{
29+
$provider = $this->providerCollector->getProvider($providerName);
30+
31+
$accessToken = $provider->getAccessToken($code);
32+
$userDTO = $provider->getUserInfo($accessToken);
33+
34+
$this->userService->login($userDTO);
35+
}
36+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
namespace OxidEsales\SecurityModule\Authentication\OAuth2\Service;
9+
10+
interface AuthenticationServiceInterface
11+
{
12+
public function getAuthorizationUrl(string $providerName): string;
13+
14+
public function handleCallback(string $providerName, #[\SensitiveParameter] string $code): void;
15+
}

src/Authentication/OAuth2/Service/services.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ services:
1717
arguments:
1818
$providers: !tagged 'security.oauth.tag.provider'
1919

20+
OxidEsales\SecurityModule\Authentication\OAuth2\Service\AuthenticationServiceInterface:
21+
class: OxidEsales\SecurityModule\Authentication\OAuth2\Service\AuthenticationService
22+
public: true
23+

0 commit comments

Comments
 (0)