Skip to content

Commit df5995c

Browse files
OXDEV-9885 Move provider to infrastructure
1 parent 553eed0 commit df5995c

29 files changed

Lines changed: 383 additions & 249 deletions

metadata.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
//OAuth2 settings
117117
[
118118
'group' => 'oauth',
119-
'name' => OAuthModuleSettings::FACEBOOK_ACTIVE,
119+
'name' => OAuthModuleSettings::FACEBOOK_LOGIN_ENABLED,
120120
'type' => 'bool',
121121
'value' => false
122122
],
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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\Exception;
11+
12+
class ProviderNotActiveException extends \Exception
13+
{
14+
}

src/Authentication/OAuth2/Service/Exception/ProviderNotActive.php renamed to src/Authentication/OAuth2/Exception/ProviderNotFoundException.php

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

88
declare(strict_types=1);
99

10-
namespace OxidEsales\SecurityModule\Authentication\OAuth2\Service\Exception;
10+
namespace OxidEsales\SecurityModule\Authentication\OAuth2\Exception;
1111

12-
class ProviderNotActive extends \Exception
12+
class ProviderNotFoundException extends \Exception
1313
{
1414
}

src/Authentication/OAuth2/Service/Exception/UserBlockedException.php renamed to src/Authentication/OAuth2/Exception/UserBlockedException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
declare(strict_types=1);
99

10-
namespace OxidEsales\SecurityModule\Authentication\OAuth2\Service\Exception;
10+
namespace OxidEsales\SecurityModule\Authentication\OAuth2\Exception;
1111

1212
class UserBlockedException extends \Exception
1313
{

src/Authentication/OAuth2/Service/Exception/UserNotFoundException.php renamed to src/Authentication/OAuth2/Exception/UserNotFoundException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
declare(strict_types=1);
99

10-
namespace OxidEsales\SecurityModule\Authentication\OAuth2\Service\Exception;
10+
namespace OxidEsales\SecurityModule\Authentication\OAuth2\Exception;
1111

1212
class UserNotFoundException extends \Exception
1313
{

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace OxidEsales\SecurityModule\Authentication\OAuth2\Infrastructure\Factory;
1111

1212
use League\OAuth2\Client\Provider\FacebookUser;
13+
use League\OAuth2\Client\Provider\GoogleUser;
1314
use OxidEsales\SecurityModule\Authentication\OAuth2\DTO\OAuth2UserDTO;
1415
use OxidEsales\SecurityModule\Authentication\OAuth2\DTO\OAuth2UserDTOInterface;
1516

@@ -23,4 +24,14 @@ public function createFromFacebookUser(FacebookUser $facebookUser): OAuth2UserDT
2324
$facebookUser->getEmail(),
2425
);
2526
}
27+
28+
//TODO: use one factory for all providers
29+
public function createFromGoogleUser(GoogleUser $googleUser): OAuth2UserDTOInterface
30+
{
31+
return new OAuth2UserDTO(
32+
$googleUser->getFirstName(),
33+
$googleUser->getLastName(),
34+
$googleUser->getEmail(),
35+
);
36+
}
2637
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
namespace OxidEsales\SecurityModule\Authentication\OAuth2\Infrastructure\Factory;
99

1010
use League\OAuth2\Client\Provider\FacebookUser;
11+
use League\OAuth2\Client\Provider\GoogleUser;
1112
use OxidEsales\SecurityModule\Authentication\OAuth2\DTO\OAuth2UserDTOInterface;
1213

1314
interface OAuth2UserDTOFactoryInterface
1415
{
1516
public function createFromFacebookUser(FacebookUser $facebookUser): OAuth2UserDTOInterface;
17+
18+
public function createFromGoogleUser(GoogleUser $googleUser): OAuth2UserDTOInterface;
1619
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ class FacebookAdapter implements ProviderAdapterInterface
2525

2626
public function __construct(
2727
private readonly ModuleSettingsServiceInterface $moduleSettings,
28-
private readonly FacebookProviderFactoryInterface $facebookProviderFactory,
28+
private readonly FacebookProviderFactoryInterface $facebookProvider,
2929
private readonly OAuth2UserDTOFactoryInterface $oAuth2UserDTOFactory,
3030
) {
31-
$this->provider = $this->facebookProviderFactory->create();
31+
$this->provider = $this->facebookProvider->create();
3232
}
3333

34-
public function isActive(): bool
34+
public function getName(): string
3535
{
36-
return $this->moduleSettings->isFacebookLoginEnabled();
36+
return 'facebook';
3737
}
3838

39-
public function getName(): string
39+
public function isActive(): bool
4040
{
41-
return 'facebook';
41+
return $this->moduleSettings->isFacebookLoginEnabled();
4242
}
4343

4444
public function getAuthorizationUrl(array $options = []): string
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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\Infrastructure\Provider\Google;
11+
12+
use League\OAuth2\Client\Provider\Google as GoogleProvider;
13+
use League\OAuth2\Client\Provider\GoogleUser;
14+
use League\OAuth2\Client\Token\AccessToken;
15+
use League\OAuth2\Client\Token\AccessTokenInterface;
16+
use OxidEsales\SecurityModule\Authentication\OAuth2\DTO\OAuth2UserDTO;
17+
use OxidEsales\SecurityModule\Authentication\OAuth2\DTO\OAuth2UserDTOInterface;
18+
use OxidEsales\SecurityModule\Authentication\OAuth2\Infrastructure\Factory\OAuth2UserDTOFactoryInterface;
19+
use OxidEsales\SecurityModule\Authentication\OAuth2\Infrastructure\Provider\ProviderAdapterInterface;
20+
use OxidEsales\SecurityModule\Authentication\OAuth2\Service\ModuleSettingsServiceInterface;
21+
use Exception;
22+
23+
class GoogleAdapter implements ProviderAdapterInterface
24+
{
25+
private GoogleProvider $provider;
26+
27+
public function __construct(
28+
private readonly ModuleSettingsServiceInterface $moduleSettings,
29+
private readonly GoogleProviderFactoryInterface $googleProvider,
30+
private readonly OAuth2UserDTOFactoryInterface $oAuth2UserDTOFactory,
31+
) {
32+
$this->provider = $this->googleProvider->create();
33+
}
34+
35+
public function getName(): string
36+
{
37+
return 'google';
38+
}
39+
40+
public function isActive(): bool
41+
{
42+
return $this->moduleSettings->isGoogleLoginEnabled();
43+
}
44+
45+
public function getAuthorizationUrl(array $options = []): string
46+
{
47+
return $this->provider->getAuthorizationUrl($options);
48+
}
49+
50+
public function getAccessToken(string $code): AccessTokenInterface
51+
{
52+
return $this->provider->getAccessToken('authorization_code', [
53+
'code' => $code,
54+
]);
55+
}
56+
57+
public function getUserInfo(AccessTokenInterface $token): OAuth2UserDTOInterface
58+
{
59+
if (!$token instanceof AccessToken) {
60+
throw new Exception('Access token must be an instance of AccessToken.');
61+
}
62+
63+
/** @var GoogleUser $user */
64+
$user = $this->provider->getResourceOwner($token);
65+
66+
return $this->oAuth2UserDTOFactory->createFromGoogleUser($user);
67+
}
68+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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\Infrastructure\Provider\Google;
11+
12+
use League\OAuth2\Client\Provider\Google as GoogleProvider;
13+
use OxidEsales\SecurityModule\Authentication\OAuth2\Service\ModuleSettingsServiceInterface;
14+
15+
class GoogleProviderFactory implements GoogleProviderFactoryInterface
16+
{
17+
public function __construct(
18+
private readonly ModuleSettingsServiceInterface $moduleSettings,
19+
) {
20+
}
21+
22+
/**
23+
* @inheritDoc
24+
*/
25+
public function create(): GoogleProvider
26+
{
27+
return new GoogleProvider([
28+
'clientId' => $this->moduleSettings->getGoogleClientId(),
29+
'clientSecret' => $this->moduleSettings->getFacebookClientSecret(),
30+
'redirectUri' => $this->moduleSettings->getGoogleRedirectUrl(),
31+
]);
32+
}
33+
}

0 commit comments

Comments
 (0)