Skip to content

Commit 3c5914b

Browse files
OXDEV-9889 Implement provider collector, datatype
1 parent c28ce2c commit 3c5914b

14 files changed

Lines changed: 430 additions & 2 deletions

File tree

metadata.php

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

12+
use OxidEsales\SecurityModule\Authentication\OAuth2\Service\ModuleSettingsService;
1213
use OxidEsales\SecurityModule\PasswordPolicy\Service\ModuleSettingsService as PasswordPolicyModuleSettings;
1314
use OxidEsales\SecurityModule\Captcha\Service\ModuleSettingsService as CaptchaModuleSettings;
1415
use OxidEsales\SecurityModule\Core\Module;
@@ -109,6 +110,32 @@
109110
'type' => 'select',
110111
'constraints' => '5min|15min|30min',
111112
'value' => '15min'
112-
]
113+
],
114+
115+
//OAuth2 settings
116+
[
117+
'group' => 'oauth',
118+
'name' => ModuleSettingsService::FACEBOOK_ACTIVE,
119+
'type' => 'bool',
120+
'value' => false
121+
],
122+
[
123+
'group' => 'oauth',
124+
'name' => ModuleSettingsService::FACEBOOK_CLIENT_ID,
125+
'type' => 'string',
126+
'value' => ''
127+
],
128+
[
129+
'group' => 'oauth',
130+
'name' => ModuleSettingsService::FACEBOOK_CLIENT_SECRET,
131+
'type' => 'string',
132+
'value' => ''
133+
],
134+
[
135+
'group' => 'oauth',
136+
'name' => ModuleSettingsService::FACEBOOK_REDIRECT_URL,
137+
'type' => 'string',
138+
'value' => ''
139+
],
113140
],
114141
];

services.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
imports:
22
- { resource: src/Captcha/services.yaml }
33
- { resource: src/PasswordPolicy/services.yaml }
4+
- { resource: src/Authentication/services.yaml }
45

56
services:
67
_defaults:
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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\DataType;
11+
12+
class UserDataType implements UserDataTypeInterface
13+
{
14+
public function __construct(
15+
private readonly string $firstName,
16+
private readonly string $lastName,
17+
private readonly string $email,
18+
) {
19+
}
20+
21+
public function getFirstName(): string
22+
{
23+
return $this->firstName;
24+
}
25+
26+
public function getLastName(): string
27+
{
28+
return $this->lastName;
29+
}
30+
31+
public function getEmail(): string
32+
{
33+
return $this->email;
34+
}
35+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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\DataType;
9+
10+
interface UserDataTypeInterface
11+
{
12+
public function getFirstName(): string;
13+
14+
public function getLastName(): string;
15+
16+
public function getEmail(): string;
17+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface;
13+
use OxidEsales\SecurityModule\Core\Module;
14+
15+
class ModuleSettingsService implements ModuleSettingsServiceInterface
16+
{
17+
public const FACEBOOK_ACTIVE = 'oeSecurityFacebookEnable';
18+
public const FACEBOOK_CLIENT_ID = 'oeSecurityFacebookClientId';
19+
public const FACEBOOK_CLIENT_SECRET = 'oeSecurityFacebookSecret';
20+
public const FACEBOOK_REDIRECT_URL = 'oeSecurityFacebookRedirectUrl';
21+
22+
public function __construct(
23+
private readonly ModuleSettingServiceInterface $moduleSettingService
24+
) {
25+
}
26+
27+
public function isFacebookActive(): bool
28+
{
29+
return $this->moduleSettingService->getBoolean(self::FACEBOOK_ACTIVE, Module::MODULE_ID);
30+
}
31+
32+
public function getFacebookClientId(): string
33+
{
34+
return $this->getStringValue(self::FACEBOOK_CLIENT_ID);
35+
}
36+
37+
public function getFacebookClientSecret(): string
38+
{
39+
return $this->getStringValue(self::FACEBOOK_CLIENT_SECRET);
40+
}
41+
42+
public function getFacebookRedirectUrl(): string
43+
{
44+
return $this->getStringValue(self::FACEBOOK_REDIRECT_URL);
45+
}
46+
47+
private function getStringValue(string $key): string
48+
{
49+
return $this->moduleSettingService->getString(
50+
$key,
51+
Module::MODULE_ID
52+
)
53+
->trim()
54+
->toString();
55+
}
56+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
use Symfony\Component\String\UnicodeString;
11+
12+
interface ModuleSettingsServiceInterface
13+
{
14+
public function isFacebookActive(): bool;
15+
16+
public function getFacebookClientId(): string;
17+
18+
public function getFacebookClientSecret(): string;
19+
20+
public function getFacebookRedirectUrl(): string;
21+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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\Provider\Facebook;
11+
12+
use League\OAuth2\Client\Provider\AbstractProvider;
13+
use League\OAuth2\Client\Token\AccessTokenInterface;
14+
use League\OAuth2\Client\Provider\Facebook as FacebookProvider;
15+
use OxidEsales\SecurityModule\Authentication\OAuth2\DataType\UserDataType;
16+
use OxidEsales\SecurityModule\Authentication\OAuth2\DataType\UserDataTypeInterface;
17+
use OxidEsales\SecurityModule\Authentication\OAuth2\Service\ModuleSettingsServiceInterface;
18+
use OxidEsales\SecurityModule\Authentication\OAuth2\Service\Provider\ProviderInterface;
19+
20+
class Facebook implements ProviderInterface
21+
{
22+
private FacebookProvider $facebookProvider;
23+
24+
public function __construct(
25+
private readonly ModuleSettingsServiceInterface $moduleSettings,
26+
) {
27+
}
28+
29+
public function getName(): string
30+
{
31+
return 'facebook';
32+
}
33+
34+
public function getClient(): AbstractProvider
35+
{
36+
$this->facebookProvider = new FacebookProvider([
37+
'clientId' => $this->moduleSettings->getFacebookClientId(),
38+
'clientSecret' => $this->moduleSettings->getFacebookClientSecret(),
39+
'redirectUri' => $this->moduleSettings->getFacebookRedirectUrl(),
40+
'graphApiVersion' => 'v2.10',
41+
]);
42+
43+
return $this->facebookProvider;
44+
}
45+
46+
public function getAuthorizationUrl(string $state): string
47+
{
48+
return $this->facebookProvider->getAuthorizationUrl(['state' => $state]);
49+
}
50+
51+
public function getAccessToken(string $code): AccessTokenInterface
52+
{
53+
return $this->facebookProvider->getAccessToken('authorization_code', ['code' => $code]);
54+
}
55+
56+
public function getUserInfo(AccessTokenInterface $token): UserDataTypeInterface
57+
{
58+
//todo: move to service
59+
$user = $this->facebookProvider->getResourceOwner($token);
60+
61+
return new UserDataType(
62+
$user->getFirstName(),
63+
$user->getLastName(),
64+
$user->getEmail(),
65+
);
66+
}
67+
68+
public function validateToken(AccessTokenInterface $token): bool
69+
{
70+
}
71+
}

src/Authentication/OAuth2/Service/Provider/ProviderInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use League\OAuth2\Client\Provider\AbstractProvider;
66
use League\OAuth2\Client\Token\AccessTokenInterface;
7+
use OxidEsales\SecurityModule\Authentication\OAuth2\DataType\UserDataTypeInterface;
78

89
interface ProviderInterface
910
{
@@ -33,7 +34,7 @@ public function getAccessToken(string $code): AccessTokenInterface;
3334
* Fetch user information (claims) from the provider using the access token.
3435
* Should return standardized data: id, email, name, avatar, etc.
3536
*/
36-
public function getUserInfo(AccessTokenInterface $token): array;
37+
public function getUserInfo(AccessTokenInterface $token): UserDataTypeInterface;
3738

3839
/**
3940
* Validate the provider response.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
class ProviderCollector implements ProviderCollectorInterface
13+
{
14+
public function __construct(
15+
protected iterable $providers,
16+
) {
17+
}
18+
19+
public function getProviders(): iterable
20+
{
21+
return $this->providers;
22+
}
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
services:
2+
_defaults:
3+
public: false
4+
autowire: true
5+
6+
OxidEsales\SecurityModule\Authentication\OAuth2\Service\ModuleSettingsServiceInterface:
7+
class: OxidEsales\SecurityModule\Authentication\OAuth2\Service\ModuleSettingsService
8+
public: true
9+
10+
OxidEsales\SecurityModule\Authentication\OAuth2\Service\ProviderCollectorInterface:
11+
class: OxidEsales\SecurityModule\Authentication\OAuth2\Service\ProviderCollector
12+
public: true
13+
arguments:
14+
$providers: !tagged 'security.oauth.tag.provider'
15+
16+
OxidEsales\SecurityModule\Authentication\OAuth2\Service\Provider\Facebook\Facebook:
17+
tags: [ 'security.oauth.tag.provider' ]

0 commit comments

Comments
 (0)