Skip to content

Commit b1506d2

Browse files
committed
OXDEV-9919: Some fixes and refactorings during review
1 parent 460b298 commit b1506d2

File tree

27 files changed

+321
-222
lines changed

27 files changed

+321
-222
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Added
1010
- Extracted reusable Twig code into captcha.html.twig and password.html.twig
11+
- Facebook login OAuth-provider
1112

1213
### Changed
1314
- Show multiple errors on invalid password

services.yaml

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

67
services:
78
_defaults:
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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\Factory;
11+
12+
use League\OAuth2\Client\Provider\FacebookUser;
13+
use OxidEsales\SecurityModule\Authentication\OAuth2\DTO\OAuth2UserDTO;
14+
use OxidEsales\SecurityModule\Authentication\OAuth2\DTO\OAuth2UserDTOInterface;
15+
16+
class OAuth2UserDTOFactory implements OAuth2UserDTOFactoryInterface
17+
{
18+
public function createFromFacebookUser(FacebookUser $facebookUser): OAuth2UserDTOInterface
19+
{
20+
return new OAuth2UserDTO(
21+
$facebookUser->getFirstName(),
22+
$facebookUser->getLastName(),
23+
$facebookUser->getEmail(),
24+
);
25+
}
26+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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\Infrastructure\Factory;
9+
10+
use League\OAuth2\Client\Provider\FacebookUser;
11+
use OxidEsales\SecurityModule\Authentication\OAuth2\DTO\OAuth2UserDTOInterface;
12+
13+
interface OAuth2UserDTOFactoryInterface
14+
{
15+
public function createFromFacebookUser(FacebookUser $facebookUser): OAuth2UserDTOInterface;
16+
}

src/Authentication/OAuth2/Infrastructure/Factory/services.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ services:
88

99
OxidEsales\SecurityModule\Authentication\OAuth2\Infrastructure\Factory\UserDTOFactoryInterface:
1010
class: OxidEsales\SecurityModule\Authentication\OAuth2\Infrastructure\Factory\UserDTOFactory
11+
12+
OxidEsales\SecurityModule\Authentication\OAuth2\Infrastructure\Factory\OAuth2UserDTOFactoryInterface:
13+
class: OxidEsales\SecurityModule\Authentication\OAuth2\Infrastructure\Factory\OAuth2UserDTOFactory

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
use League\OAuth2\Client\Provider\FacebookUser;
1313
use League\OAuth2\Client\Token\AccessToken;
1414
use League\OAuth2\Client\Token\AccessTokenInterface;
15-
use OxidEsales\SecurityModule\Authentication\OAuth2\DTO\OAuth2UserDTO;
1615
use OxidEsales\SecurityModule\Authentication\OAuth2\DTO\OAuth2UserDTOInterface;
16+
use OxidEsales\SecurityModule\Authentication\OAuth2\Infrastructure\Factory\OAuth2UserDTOFactoryInterface;
1717
use OxidEsales\SecurityModule\Authentication\OAuth2\Infrastructure\Provider\ProviderAdapterInterface;
1818
use OxidEsales\SecurityModule\Authentication\OAuth2\Service\ModuleSettingsServiceInterface;
1919
use League\OAuth2\Client\Provider\Facebook as FacebookProvider;
@@ -25,9 +25,10 @@ class FacebookAdapter implements ProviderAdapterInterface
2525

2626
public function __construct(
2727
private readonly ModuleSettingsServiceInterface $moduleSettings,
28-
private readonly FacebookProviderFactoryInterface $facebookProvider,
28+
private readonly FacebookProviderFactoryInterface $facebookProviderFactory,
29+
private readonly OAuth2UserDTOFactoryInterface $oAuth2UserDTOFactory,
2930
) {
30-
$this->provider = $this->facebookProvider->create();
31+
$this->provider = $this->facebookProviderFactory->create();
3132
}
3233

3334
public function isActive(): bool
@@ -59,10 +60,6 @@ public function getUserInfo(AccessTokenInterface $token): OAuth2UserDTOInterface
5960
/** @var FacebookUser $user */
6061
$user = $this->provider->getResourceOwner($token);
6162

62-
return new OAuth2UserDTO(
63-
$user->getFirstName(),
64-
$user->getLastName(),
65-
$user->getEmail(),
66-
);
63+
return $this->oAuth2UserDTOFactory->createFromFacebookUser($user);
6764
}
6865
}

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

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

88
namespace OxidEsales\SecurityModule\Authentication\OAuth2\Infrastructure\Provider;
99

10+
use Exception;
1011
use League\OAuth2\Client\Token\AccessTokenInterface;
1112
use OxidEsales\SecurityModule\Authentication\OAuth2\DTO\OAuth2UserDTOInterface;
1213

@@ -35,6 +36,7 @@ public function getAccessToken(string $code): AccessTokenInterface;
3536
/**
3637
* Fetch user information (claims) from the provider using the access token.
3738
* Should return UserInterface
39+
* @throws Exception
3840
*/
3941
public function getUserInfo(AccessTokenInterface $token): OAuth2UserDTOInterface;
4042
}

src/Authentication/OAuth2/Infrastructure/Repository/UserRepository.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
use OxidEsales\SecurityModule\Authentication\OAuth2\Exception\UserNotFoundException;
1515
use OxidEsales\SecurityModule\Authentication\OAuth2\Infrastructure\Factory\UserDTOFactoryInterface;
1616
use OxidEsales\SecurityModule\Authentication\OAuth2\Infrastructure\Factory\UserFactoryInterface;
17+
use OxidEsales\SecurityModule\Shared\Service\PasswordGeneratorServiceInterface;
1718

1819
class UserRepository implements UserRepositoryInterface
1920
{
2021
public function __construct(
2122
private UserFactoryInterface $userFactory,
2223
private UserDTOFactoryInterface $userDTOFactory,
24+
private PasswordGeneratorServiceInterface $passwordGenerator
2325
) {
2426
}
2527

@@ -28,10 +30,7 @@ public function getUserByEmail(string $username): UserDTOInterface
2830
$userModel = $this->userFactory->create();
2931

3032
$userId = $userModel->getIdByUserName($username);
31-
if (
32-
!$userId ||
33-
!$userModel->load($userId)
34-
) {
33+
if (!$userId || !$userModel->load($userId)) {
3534
throw new UserNotFoundException();
3635
}
3736

@@ -45,9 +44,8 @@ public function createUser(OAuth2UserDTOInterface $userDTO): UserDTOInterface
4544
'OXFNAME' => $userDTO->getFirstName(),
4645
'OXLNAME' => $userDTO->getLastName(),
4746
'OXUSERNAME' => $userDTO->getEmail(),
48-
'OXREGISTER' => date('Y-m-d H:i:s')
4947
]);
50-
$userModel->setPassword(bin2hex(random_bytes(20)));
48+
$userModel->setPassword($this->passwordGenerator->generatePasswordForOAuthUser());
5149
$userModel->createUser();
5250

5351
return $this->userDTOFactory->createFromModel($userModel);

src/Authentication/OAuth2/Infrastructure/services.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,3 @@ imports:
22
- { resource: Factory/services.yaml }
33
- { resource: Provider/services.yaml }
44
- { resource: Repository/services.yaml }
5-
6-
services:
7-
_defaults:
8-
autowire: true
9-
public: false

src/Authentication/OAuth2/Service/UserService.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ public function login(OAuth2UserDTOInterface $auth2UserDTO): void
3030
}
3131

3232
try {
33-
$userModel = $this->userRepository->getUserByEmail($auth2UserDTO->getEmail());
34-
if ($userModel->isBlocked()) {
33+
$userDTO = $this->userRepository->getUserByEmail($auth2UserDTO->getEmail());
34+
if ($userDTO->isBlocked()) {
3535
throw new UserBlockedException();
3636
}
3737
} catch (UserNotFoundException $e) {
38-
$userModel = $this->userRepository->createUser($auth2UserDTO);
38+
$userDTO = $this->userRepository->createUser($auth2UserDTO);
3939
}
4040

41-
$this->session->set('usr', $userModel->getId());
41+
$this->session->set('usr', $userDTO->getId());
4242
}
4343
}

0 commit comments

Comments
 (0)