Skip to content

Commit 69be264

Browse files
committed
Fix compat with league/oauth2-server:^8.3 & symfony/*:^5.3
1 parent 216c0cc commit 69be264

File tree

13 files changed

+74
-40
lines changed

13 files changed

+74
-40
lines changed

.github/workflows/unit-tests.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
strategy:
1212
matrix:
1313
symfony-version:
14-
- "5.2.*"
14+
- "5.3.*"
1515
php-version:
1616
- "7.2"
1717
- "7.3"
@@ -49,10 +49,7 @@ jobs:
4949

5050
- name: "install lowest dependencies"
5151
if: ${{ matrix.dependencies == 'lowest' }}
52-
run: |
53-
composer require --no-update symfony/config=${{ matrix.symfony-version }} symfony/http-kernel=${{ matrix.symfony-version }} symfony/dependency-injection=${{ matrix.symfony-version }} symfony/options-resolver=${{ matrix.symfony-version }}
54-
composer require --no-update --dev symfony/framework-bundle=${{ matrix.symfony-version }} symfony/yaml=${{ matrix.symfony-version }}
55-
composer update --prefer-lowest --no-interaction --no-progress --prefer-dist
52+
run: composer update --prefer-lowest --no-interaction --no-progress --prefer-dist
5653

5754
- name: "install highest dependencies"
5855
if: ${{ matrix.dependencies == 'highest' }}

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
2222
"league/oauth2-server": "^8.0",
2323
"nyholm/psr7": "^1.4",
2424
"psr/http-factory": "^1.0",
25-
"symfony/framework-bundle": "^5.2",
25+
"symfony/framework-bundle": "^5.3",
2626
"symfony/psr-http-message-bridge": "^2.0",
27-
"symfony/security-bundle": "^5.2"
27+
"symfony/security-bundle": "^5.3"
2828
},
2929
"require-dev": {
3030
"ext-pdo": "*",
3131
"ext-pdo_sqlite": "*",
3232
"psalm/plugin-symfony": "^2.2",
33-
"symfony/browser-kit": "^5.2",
34-
"symfony/phpunit-bridge": "^5.2",
33+
"symfony/browser-kit": "^5.3",
34+
"symfony/phpunit-bridge": "^5.3",
3535
"vimeo/psalm": "^4.6"
3636
},
3737
"autoload": {

src/Converter/UserConverter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010

1111
final class UserConverter implements UserConverterInterface
1212
{
13+
/**
14+
* @psalm-suppress DeprecatedMethod
15+
*/
1316
public function toLeague(?UserInterface $user): UserEntityInterface
1417
{
1518
$userEntity = new User();
1619
if ($user instanceof UserInterface) {
17-
$userEntity->setIdentifier($user->getUsername());
20+
$userEntity->setIdentifier(method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername());
1821
}
1922

2023
return $userEntity;

src/DBAL/Type/ImplodedArray.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ abstract class ImplodedArray extends TextType
1818
private const VALUE_DELIMITER = ' ';
1919

2020
/**
21-
* {@inheritdoc}
21+
* @psalm-suppress MixedArgumentTypeCoercion
2222
*/
2323
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
2424
{

src/Repository/AuthCodeRepository.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ public function getNewAuthCode(): AuthCode
5454
*
5555
* @return void
5656
*/
57-
public function persistNewAuthCode(AuthCodeEntityInterface $authCode)
57+
public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity)
5858
{
59-
$authorizationCode = $this->authorizationCodeManager->find($authCode->getIdentifier());
59+
$authorizationCode = $this->authorizationCodeManager->find($authCodeEntity->getIdentifier());
6060

6161
if (null !== $authorizationCode) {
6262
throw UniqueTokenIdentifierConstraintViolationException::create();
6363
}
6464

65-
$authorizationCode = $this->buildAuthorizationCode($authCode);
65+
$authorizationCode = $this->buildAuthorizationCode($authCodeEntity);
6666

6767
$this->authorizationCodeManager->save($authorizationCode);
6868
}
@@ -97,22 +97,22 @@ public function isAuthCodeRevoked($codeId): bool
9797
return $authorizationCode->isRevoked();
9898
}
9999

100-
private function buildAuthorizationCode(AuthCodeEntityInterface $authCode): AuthorizationCode
100+
private function buildAuthorizationCode(AuthCodeEntityInterface $authCodeEntity): AuthorizationCode
101101
{
102102
/** @var AbstractClient $client */
103-
$client = $this->clientManager->find($authCode->getClient()->getIdentifier());
103+
$client = $this->clientManager->find($authCodeEntity->getClient()->getIdentifier());
104104

105-
$userIdentifier = $authCode->getUserIdentifier();
105+
$userIdentifier = $authCodeEntity->getUserIdentifier();
106106
if (null !== $userIdentifier) {
107107
$userIdentifier = (string) $userIdentifier;
108108
}
109109

110110
return new AuthorizationCode(
111-
$authCode->getIdentifier(),
112-
$authCode->getExpiryDateTime(),
111+
$authCodeEntity->getIdentifier(),
112+
$authCodeEntity->getExpiryDateTime(),
113113
$client,
114114
$userIdentifier,
115-
$this->scopeConverter->toDomainArray(array_values($authCode->getScopes()))
115+
$this->scopeConverter->toDomainArray(array_values($authCodeEntity->getScopes()))
116116
);
117117
}
118118
}

src/Security/Authenticator/OAuth2Authenticator.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ public function start(Request $request, AuthenticationException $authException =
7272
return new Response('', 401, ['WWW-Authenticate' => 'Bearer']);
7373
}
7474

75+
/**
76+
* @psalm-suppress DeprecatedMethod
77+
*/
7578
public function authenticate(Request $request): PassportInterface
7679
{
7780
try {
@@ -89,11 +92,21 @@ public function authenticate(Request $request): PassportInterface
8992
/** @var list<string> $scopes */
9093
$scopes = $psr7Request->getAttribute('oauth_scopes', []);
9194

92-
$passport = new SelfValidatingPassport(new UserBadge($userIdentifier, function (string $userIdentifier): UserInterface {
93-
return '' !== $userIdentifier ? $this->userProvider->loadUserByUsername($userIdentifier) : new NullUser();
94-
}), [
95+
$userLoader = function (string $userIdentifier): UserInterface {
96+
if ('' === $userIdentifier) {
97+
return new NullUser();
98+
}
99+
if (!method_exists($this->userProvider, 'loadUserByIdentifier')) {
100+
return $this->userProvider->loadUserByUsername($userIdentifier);
101+
}
102+
103+
return $this->userProvider->loadUserByIdentifier($userIdentifier);
104+
};
105+
106+
$passport = new SelfValidatingPassport(new UserBadge($userIdentifier, $userLoader), [
95107
new ScopeBadge($scopes),
96108
]);
109+
97110
$passport->setAttribute('accessTokenId', $accessTokenId);
98111

99112
return $passport;

src/Security/EventListener/CheckScopeListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function checkPassport(CheckPassportEvent $event): void
4444
}
4545

4646
/** @var Request $request */
47-
$request = $this->requestStack->getMasterRequest();
47+
$request = $this->requestStack->{method_exists($this->requestStack, 'getMainRequest') ? 'getMainRequest' : 'getMasterRequest'}();
4848

4949
/** @var list<string> $requestedScopes */
5050
$requestedScopes = $request->attributes->get('oauth2_scopes', []);

src/Security/User/NullUser.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@ public function getUsername(): string
2121
return '';
2222
}
2323

24+
public function getUserIdentifier(): string
25+
{
26+
return '';
27+
}
28+
2429
/**
2530
* @psalm-mutation-free
2631
*/
27-
public function getPassword(): string
32+
public function getPassword(): ?string
2833
{
29-
return '';
34+
return null;
3035
}
3136

3237
/**

src/Service/CredentialsRevoker/DoctrineCredentialsRevoker.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ public function __construct(EntityManagerInterface $entityManager, ClientManager
3131
$this->clientManager = $clientManager;
3232
}
3333

34+
/**
35+
* @psalm-suppress DeprecatedMethod
36+
*/
3437
public function revokeCredentialsForUser(UserInterface $user): void
3538
{
36-
$userIdentifier = $user->getUsername();
39+
$userIdentifier = method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername();
3740

3841
$this->entityManager->createQueryBuilder()
3942
->update(AccessToken::class, 'at')

tests/Fixtures/SecurityTestController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function helloAction(): Response
2828
$user = $this->getUser();
2929

3030
return new Response(
31-
sprintf('Hello, %s', $user instanceof NullUser ? 'guest' : $user->getUsername())
31+
sprintf('Hello, %s', $user instanceof NullUser ? 'guest' : (method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername()))
3232
);
3333
}
3434

0 commit comments

Comments
 (0)