Skip to content

Commit 33ec3ef

Browse files
TitaKolevatkcreateit
authored andcommitted
OXDEV-10037 Get provider dynamicaly and redirect to correct page
1 parent 3bde242 commit 33ec3ef

18 files changed

Lines changed: 218 additions & 30 deletions

File tree

src/Authentication/OAuth2/Controller/OAuthController.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,28 @@
1111
use OxidEsales\Eshop\Core\Registry;
1212
use OxidEsales\SecurityModule\Authentication\OAuth2\Service\ProviderCollectorInterface;
1313
use OxidEsales\SecurityModule\Authentication\OAuth2\Service\UserServiceInterface;
14+
use OxidEsales\SecurityModule\Authentication\Session\SessionKeys;
1415

1516
class OAuthController extends FrontendController
1617
{
1718
public function login(): void
1819
{
20+
$providerName = $_GET['provider'] ?? '';
21+
1922
$providerCollector = $this->getService(ProviderCollectorInterface::class);
2023

21-
$provider = $providerCollector->getProvider($_GET['provider']);
24+
$provider = $providerCollector->getProvider($providerName);
2225

2326
Registry::getUtils()->redirect($provider->getAuthorizationUrl());
2427
}
2528

2629
public function redirect(): void
2730
{
28-
//todo: get provider dynamically
31+
$providerName = $_GET['provider'] ?? '';
32+
2933
$provider = $this
3034
->getService(ProviderCollectorInterface::class)
31-
->getProvider('google');
35+
->getProvider($providerName);
3236

3337
$accessToken = $provider->getAccessToken($_GET['code']);
3438

@@ -38,6 +42,11 @@ public function redirect(): void
3842
->getService(UserServiceInterface::class)
3943
->login($userDTO);
4044

41-
Registry::getUtils()->redirect('');
45+
$redirectUrl = Registry::getSession()->getVariable(SessionKeys::AUTH_REDIRECT_URL);
46+
if (!$redirectUrl) {
47+
$redirectUrl = Registry::getConfig()->getShopHomeUrl();
48+
}
49+
50+
Registry::getUtils()->redirect($redirectUrl, false);
4251
}
4352
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function create(): GoogleProvider
2626
{
2727
return new GoogleProvider([
2828
'clientId' => $this->moduleSettings->getGoogleClientId(),
29-
'clientSecret' => $this->moduleSettings->getFacebookClientSecret(),
29+
'clientSecret' => $this->moduleSettings->getGoogleClientSecret(),
3030
'redirectUri' => $this->moduleSettings->getGoogleRedirectUrl(),
3131
]);
3232
}

src/Authentication/OAuth2/Service/UserService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

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

12+
use OxidEsales\Eshop\Application\Model\User;
13+
use OxidEsales\Eshop\Core\Registry;
1214
use OxidEsales\EshopCommunity\Internal\Framework\Session\SessionInterface;
1315
use OxidEsales\SecurityModule\Authentication\OAuth2\DTO\OAuth2UserDTOInterface;
1416
use OxidEsales\SecurityModule\Authentication\OAuth2\Exception\UserBlockedException;
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+
declare(strict_types=1);
9+
10+
namespace OxidEsales\SecurityModule\Authentication\Session;
11+
12+
final class SessionKeys
13+
{
14+
public const AUTH_REDIRECT_URL = 'otp_target_url';
15+
}

src/Authentication/TwoFactorAuth/Subscriber/StoreCurrentUrlSubscriber.php renamed to src/Authentication/Subscriber/StoreCurrentUrlSubscriber.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,33 @@
77

88
declare(strict_types=1);
99

10-
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Subscriber;
10+
namespace OxidEsales\SecurityModule\Authentication\Subscriber;
1111

1212
use OxidEsales\Eshop\Core\Config;
1313
use OxidEsales\Eshop\Core\Request;
1414
use OxidEsales\EshopCommunity\Internal\Framework\Session\SessionInterface;
1515
use OxidEsales\EshopCommunity\Internal\Transition\ShopEvents\ViewRenderedEvent;
16-
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\AuthorizeService;
16+
use OxidEsales\SecurityModule\Authentication\Session\SessionKeys;
1717
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1818

1919
readonly class StoreCurrentUrlSubscriber implements EventSubscriberInterface
2020
{
21-
public function __construct(private SessionInterface $session, private Config $config, private Request $request)
22-
{
23-
}
24-
2521
private const EXCLUDED_CONTROLLERS = [
2622
'twofactorauth',
23+
'oauth',
2724
];
2825

2926
private const EXCLUDED_FUNCTIONS = [
3027
'logout',
3128
];
3229

30+
public function __construct(
31+
private SessionInterface $session,
32+
private Config $config,
33+
private Request $request
34+
) {
35+
}
36+
3337
public static function getSubscribedEvents(): array
3438
{
3539
return [
@@ -49,7 +53,6 @@ public function onViewRendered(ViewRenderedEvent $event): void
4953
$currentController = $this->getCurrentController();
5054
$currentFunction = $this->getCurrentFunction();
5155

52-
// Skip widgets, excluded controllers, and excluded functions (like logout)
5356
if (
5457
$this->isWidget($currentController)
5558
|| $this->shouldExcludeController($currentController)
@@ -60,7 +63,7 @@ public function onViewRendered(ViewRenderedEvent $event): void
6063

6164
$currentUrl = $this->getCurrentPageUrl();
6265
if ($currentUrl) {
63-
$this->session->set(AuthorizeService::OTP_TARGET_URL, $currentUrl);
66+
$this->session->set(SessionKeys::AUTH_REDIRECT_URL, $currentUrl);
6467
}
6568
}
6669

src/Authentication/TwoFactorAuth/Subscriber/services.yaml renamed to src/Authentication/Subscriber/services.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ services:
66
OxidEsales\Eshop\Core\Config: '@=service("OxidEsales\\SecurityModule\\Core\\Registry").getConfig()'
77
OxidEsales\Eshop\Core\Request: '@=service("OxidEsales\\SecurityModule\\Core\\Registry").getRequest()'
88

9-
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Subscriber\StoreCurrentUrlSubscriber:
9+
OxidEsales\SecurityModule\Authentication\Subscriber\StoreCurrentUrlSubscriber:
1010
tags:
1111
- { name: kernel.event_subscriber }

src/Authentication/TwoFactorAuth/Service/AuthorizeService.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ class AuthorizeService implements AuthorizeServiceInterface
1616
{
1717
public const USER_SESSION_KEY = 'pending_authorized_user';
1818

19-
public const OTP_TARGET_URL = 'otp_target_url';
20-
2119
public function __construct(
2220
private ModuleSettingsServiceInterface $moduleSettings,
2321
private VerificationCollectorServiceInterface $verifyCollector,

src/Authentication/TwoFactorAuth/Service/UserService.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use OxidEsales\Eshop\Core\Config;
1313
use OxidEsales\Eshop\Core\Utils;
1414
use OxidEsales\EshopCommunity\Internal\Framework\Session\SessionInterface;
15+
use OxidEsales\SecurityModule\Authentication\Session\SessionKeys;
1516
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Factory\UserFactoryInterface;
1617

1718
readonly class UserService implements UserServiceInterface
@@ -52,13 +53,13 @@ public function finalizeLogin(): void
5253
public function clearOTPSessionVariables(): void
5354
{
5455
$this->session->remove(AuthorizeService::USER_SESSION_KEY);
55-
$this->session->remove(AuthorizeService::OTP_TARGET_URL);
56+
$this->session->remove(SessionKeys::AUTH_REDIRECT_URL);
5657
$this->session->remove('OTP_PASS');
5758
}
5859

5960
private function getRedirectUrl(): string
6061
{
61-
$storedUrl = $this->session->get(AuthorizeService::OTP_TARGET_URL);
62+
$storedUrl = $this->session->get(SessionKeys::AUTH_REDIRECT_URL);
6263

6364
if ($storedUrl && $this->isInternalUrl($storedUrl)) {
6465
return $storedUrl;

src/Authentication/TwoFactorAuth/services.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ imports:
33
- { resource: Service/services.yaml }
44
- { resource: Transput/services.yaml }
55
- { resource: Controller/services.yaml }
6-
- { resource: Subscriber/services.yaml }

src/Authentication/services.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
imports:
22
- { resource: OAuth2/services.yaml }
33
- { resource: TwoFactorAuth/services.yaml }
4+
- { resource: Subscriber/services.yaml }

0 commit comments

Comments
 (0)