Skip to content

Commit 9a57d2c

Browse files
TitaKolevatkcreateit
authored andcommitted
OXDEV-10037 Auto generate redirect url for the settings
1 parent d6f7ff8 commit 9a57d2c

6 files changed

Lines changed: 97 additions & 12 deletions

File tree

metadata.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
* Metadata version
1010
*/
1111

12-
use OxidEsales\SecurityModule\Authentication\OAuth2\Service\ModuleSettingsService;
1312
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\ModuleSettingsService as TwoFactorAuthModuleSettings;
1413
use OxidEsales\SecurityModule\PasswordPolicy\Service\ModuleSettingsService as PasswordPolicyModuleSettings;
1514
use OxidEsales\SecurityModule\Captcha\Service\ModuleSettingsService as CaptchaModuleSettings;

src/Authentication/OAuth2/Service/ModuleSettingsService.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

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

12+
use OxidEsales\Eshop\Core\Config;
1213
use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface;
1314
use OxidEsales\SecurityModule\Core\Module;
1415

@@ -24,7 +25,8 @@ class ModuleSettingsService implements ModuleSettingsServiceInterface
2425
public const GOOGLE_REDIRECT_URL = 'oeSecurityGoogleRedirectUrl';
2526

2627
public function __construct(
27-
private readonly ModuleSettingServiceInterface $moduleSettingService
28+
private readonly ModuleSettingServiceInterface $moduleSettingService,
29+
private readonly Config $config
2830
) {
2931
}
3032

@@ -50,7 +52,7 @@ public function getFacebookClientSecret(): string
5052

5153
public function getFacebookRedirectUrl(): string
5254
{
53-
return $this->getStringValue(self::FACEBOOK_REDIRECT_URL);
55+
return $this->generateRedirectUrl('facebook', self::FACEBOOK_REDIRECT_URL);
5456
}
5557

5658
public function isGoogleLoginEnabled(): bool
@@ -75,7 +77,19 @@ public function getGoogleClientSecret(): string
7577

7678
public function getGoogleRedirectUrl(): string
7779
{
78-
return $this->getStringValue(self::GOOGLE_REDIRECT_URL);
80+
return $this->generateRedirectUrl('google', self::GOOGLE_REDIRECT_URL);
81+
}
82+
83+
private function generateRedirectUrl(string $provider, string $settingKey): string
84+
{
85+
$url = $this->config->getShopUrl() . 'index.php?cl=oauth&fnc=redirect&provider=' . $provider;
86+
87+
$storedValue = $this->getStringValue($settingKey);
88+
if ($storedValue !== $url) {
89+
$this->moduleSettingService->saveString($settingKey, $url, Module::MODULE_ID);
90+
}
91+
92+
return $url;
7993
}
8094

8195
private function getStringValue(string $key): string

src/Authentication/OAuth2/Service/services.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ services:
22
_defaults:
33
autowire: true
44
public: false
5+
bind:
6+
OxidEsales\Eshop\Core\Config: '@=service("OxidEsales\\SecurityModule\\Core\\Registry").getConfig()'
57

68
OxidEsales\SecurityModule\Authentication\OAuth2\Service\ModuleSettingsServiceInterface:
79
class: OxidEsales\SecurityModule\Authentication\OAuth2\Service\ModuleSettingsService

tests/Unit/Authentication/OAuth2/Service/ModuleSettingsServiceTest.php

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

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

12+
use OxidEsales\Eshop\Core\Config;
1213
use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingService;
1314
use OxidEsales\SecurityModule\Authentication\OAuth2\Service\ModuleSettingsService;
1415
use OxidEsales\SecurityModule\Core\Module;
@@ -25,13 +26,11 @@ public static function gettersDataProvider(): array
2526
self::prepareBooleanSetting('isFacebookLoginEnabled', ModuleSettingsService::FACEBOOK_LOGIN_ENABLED, false),
2627
self::prepareStringTestItem('getFacebookClientId', ModuleSettingsService::FACEBOOK_CLIENT_ID),
2728
self::prepareStringTestItem('getFacebookClientSecret', ModuleSettingsService::FACEBOOK_CLIENT_SECRET),
28-
self::prepareStringTestItem('getFacebookRedirectUrl', ModuleSettingsService::FACEBOOK_REDIRECT_URL),
2929

3030
self::prepareBooleanSetting('isGoogleLoginEnabled', ModuleSettingsService::GOOGLE_LOGIN_ENABLED, true),
3131
self::prepareBooleanSetting('isGoogleLoginEnabled', ModuleSettingsService::GOOGLE_LOGIN_ENABLED, false),
3232
self::prepareStringTestItem('getGoogleClientId', ModuleSettingsService::GOOGLE_CLIENT_ID),
3333
self::prepareStringTestItem('getGoogleClientSecret', ModuleSettingsService::GOOGLE_CLIENT_SECRET),
34-
self::prepareStringTestItem('getGoogleRedirectUrl', ModuleSettingsService::GOOGLE_REDIRECT_URL),
3534
];
3635
}
3736

@@ -44,7 +43,9 @@ public function testGetters($method, $systemMethod, $key, $mockValue, $expectedV
4443
Module::MODULE_ID
4544
)->willReturn($mockValue);
4645

47-
$sut = new ModuleSettingsService($mssMock);
46+
$configMock = $this->createMock(Config::class);
47+
48+
$sut = new ModuleSettingsService($mssMock, $configMock);
4849
$this->assertSame($expectedValue, $sut->$method());
4950
}
5051

@@ -94,7 +95,72 @@ public function testSaveBooleanSettings(string $method, string $key, bool $value
9495
->method('saveBoolean')
9596
->with($key, $value, Module::MODULE_ID);
9697

97-
$sut = new ModuleSettingsService($mssMock);
98+
$configMock = $this->createMock(Config::class);
99+
100+
$sut = new ModuleSettingsService($mssMock, $configMock);
98101
$sut->$method($value);
99102
}
103+
104+
public static function redirectUrlDataProvider(): array
105+
{
106+
return [
107+
'facebook' => [
108+
'method' => 'getFacebookRedirectUrl',
109+
'settingKey' => ModuleSettingsService::FACEBOOK_REDIRECT_URL,
110+
'provider' => 'facebook',
111+
],
112+
'google' => [
113+
'method' => 'getGoogleRedirectUrl',
114+
'settingKey' => ModuleSettingsService::GOOGLE_REDIRECT_URL,
115+
'provider' => 'google',
116+
],
117+
];
118+
}
119+
120+
#[DataProvider('redirectUrlDataProvider')]
121+
public function testRedirectUrlIsGeneratedFromConfig(
122+
string $method,
123+
string $settingKey,
124+
string $provider
125+
): void {
126+
$shopUrl = 'https://myshop.com/';
127+
$expectedUrl = $shopUrl . 'index.php?cl=oauth&fnc=redirect&provider=' . $provider;
128+
129+
$configMock = $this->createMock(Config::class);
130+
$configMock->method('getShopUrl')->willReturn($shopUrl);
131+
132+
$mssMock = $this->createPartialMock(ModuleSettingService::class, ['getString', 'saveString']);
133+
$mssMock->method('getString')
134+
->with($settingKey, Module::MODULE_ID)
135+
->willReturn(new UnicodeString('old-value'));
136+
$mssMock->expects($this->once())
137+
->method('saveString')
138+
->with($settingKey, $expectedUrl, Module::MODULE_ID);
139+
140+
$sut = new ModuleSettingsService($mssMock, $configMock);
141+
$this->assertSame($expectedUrl, $sut->$method());
142+
}
143+
144+
#[DataProvider('redirectUrlDataProvider')]
145+
public function testRedirectUrlSkipsSaveWhenUnchanged(
146+
string $method,
147+
string $settingKey,
148+
string $provider
149+
): void {
150+
$shopUrl = 'https://myshop.com/';
151+
$expectedUrl = $shopUrl . 'index.php?cl=oauth&fnc=redirect&provider=' . $provider;
152+
153+
$configMock = $this->createMock(Config::class);
154+
$configMock->method('getShopUrl')->willReturn($shopUrl);
155+
156+
$mssMock = $this->createPartialMock(ModuleSettingService::class, ['getString', 'saveString']);
157+
$mssMock->method('getString')
158+
->with($settingKey, Module::MODULE_ID)
159+
->willReturn(new UnicodeString($expectedUrl));
160+
$mssMock->expects($this->never())
161+
->method('saveString');
162+
163+
$sut = new ModuleSettingsService($mssMock, $configMock);
164+
$this->assertSame($expectedUrl, $sut->$method());
165+
}
100166
}

views/admin_twig/de/oesecuritymodule_lang.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131
'SHOP_MODULE_oeSecurityFacebookEnabled' => 'Facebook OAuth2 Login aktivieren',
3232
'SHOP_MODULE_oeSecurityFacebookClientId' => 'Facebook Client-ID',
3333
'SHOP_MODULE_oeSecurityFacebookSecret' => 'Facebook Secret',
34-
'SHOP_MODULE_oeSecurityFacebookRedirectUrl' => 'Facebook Weiterleitungs-URL',
34+
'SHOP_MODULE_oeSecurityFacebookRedirectUrl' => 'Facebook Weiterleitungs-URL (automatisch generiert)',
35+
'HELP_SHOP_MODULE_oeSecurityFacebookRedirectUrl' => 'Diese URL wird automatisch aus Ihrer Shop-URL generiert. Kopieren Sie sie in Ihre Facebook-App-Einstellungen.',
3536
'SHOP_MODULE_oeSecurityGoogleEnabled' => 'Google OAuth2 Login aktivieren',
3637
'SHOP_MODULE_oeSecurityGoogleClientId' => 'Google Client-ID',
3738
'SHOP_MODULE_oeSecurityGoogleSecret' => 'Google Secret',
38-
'SHOP_MODULE_oeSecurityGoogleRedirectUrl' => 'Google Weiterleitungs-URL',
39+
'SHOP_MODULE_oeSecurityGoogleRedirectUrl' => 'Google Weiterleitungs-URL (automatisch generiert)',
40+
'HELP_SHOP_MODULE_oeSecurityGoogleRedirectUrl' => 'Diese URL wird automatisch aus Ihrer Shop-URL generiert. Kopieren Sie sie in Ihre Google-App-Einstellungen.',
3941

4042
'SHOP_MODULE_GROUP_two_factor_auth' => 'Zwei-Faktor-Authentifizierung',
4143
'SHOP_MODULE_oeSecurityTwoFactorAuthEnabled' => 'Zwei-Faktor-Authentifizierung aktivieren',

views/admin_twig/en/oesecuritymodule_lang.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131
'SHOP_MODULE_oeSecurityFacebookEnabled' => 'Enable Facebook OAuth2 login',
3232
'SHOP_MODULE_oeSecurityFacebookClientId' => 'Facebook client id',
3333
'SHOP_MODULE_oeSecurityFacebookSecret' => 'Facebook client secret',
34-
'SHOP_MODULE_oeSecurityFacebookRedirectUrl' => 'Facebook redirect url',
34+
'SHOP_MODULE_oeSecurityFacebookRedirectUrl' => 'Facebook redirect URL (auto-generated)',
35+
'HELP_SHOP_MODULE_oeSecurityFacebookRedirectUrl' => 'This URL is auto-generated from your shop URL. Copy it to your Facebook App settings.',
3536
'SHOP_MODULE_oeSecurityGoogleEnabled' => 'Enable login with Google',
3637
'SHOP_MODULE_oeSecurityGoogleClientId' => 'Google client id',
3738
'SHOP_MODULE_oeSecurityGoogleSecret' => 'Google client secret',
38-
'SHOP_MODULE_oeSecurityGoogleRedirectUrl' => 'Google redirect url',
39+
'SHOP_MODULE_oeSecurityGoogleRedirectUrl' => 'Google redirect URL (auto-generated)',
40+
'HELP_SHOP_MODULE_oeSecurityGoogleRedirectUrl' => 'This URL is auto-generated from your shop URL. Copy it to your Google App settings.',
3941

4042
'SHOP_MODULE_GROUP_two_factor_auth' => 'Two Factor Authentication',
4143
'SHOP_MODULE_oeSecurityTwoFactorAuthEnabled' => 'Enable Two Factor Authentication',

0 commit comments

Comments
 (0)