Skip to content

Commit 5695774

Browse files
committed
Drop support for Symfony < 5.2
1 parent 61d4c2f commit 5695774

File tree

8 files changed

+31
-142
lines changed

8 files changed

+31
-142
lines changed

.github/workflows/unit-tests.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ jobs:
1111
strategy:
1212
matrix:
1313
symfony-version:
14-
- "5.1.*"
1514
- "5.2.*"
1615
php-version:
1716
- "7.2"

.psalm.baseline.xml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="4.7.0@d4377c0baf3ffbf0b1ec6998e8d1be2a40971005">
2+
<files psalm-version="4.7.1@cd53e047a58f71f646dd6bf45476076ab07b5d44">
3+
<file src="src/Resources/config/routes.php">
4+
<InvalidArgument occurrences="2">
5+
<code>['league.oauth2_server.controller.authorization', 'indexAction']</code>
6+
<code>['league.oauth2_server.controller.token', 'indexAction']</code>
7+
</InvalidArgument>
8+
</file>
39
<file src="src/Resources/config/services.php">
410
<ImplicitToStringCast occurrences="1">
511
<code>service(GrantConfigurator::class)</code>
612
</ImplicitToStringCast>
713
</file>
8-
<file src="src/Security/Authenticator/OAuth2Authenticator.php">
9-
<PossiblyInvalidArgument occurrences="1">
10-
<code>$this-&gt;getUserBadge($userIdentifier)</code>
11-
</PossiblyInvalidArgument>
12-
</file>
1314
</files>

composer.json

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

src/Resources/config/services.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
// Security layer
121121
->set('league.oauth2_server.authenticator.oauth2', OAuth2Authenticator::class)
122122
->args([
123-
service('league.oauth2-server.psr_http_factory'),
123+
service('league.oauth2_server.factory.psr_http'),
124124
service(ResourceServer::class),
125125
service(UserProviderInterface::class),
126126
null,
@@ -305,7 +305,7 @@
305305
])
306306
->alias(AuthorizationRequestResolveEventFactory::class, 'league.oauth2_server.factory.authorization_request_resolve_event')
307307

308-
->set('league.oauth2_server.factory.legacy_oauth2_token', OAuth2TokenFactory::class)
308+
->set('league.oauth2_server.factory.legacy_oauth2_token', LegacyOAuth2TokenFactory::class)
309309
->alias(LegacyOAuth2TokenFactory::class, 'league.oauth2_server.factory.legacy_oauth2_token')
310310

311311
// Storage managers

src/Security/Authentication/Token/OAuth2Token.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
*/
1313
final class OAuth2Token extends AbstractToken
1414
{
15+
/**
16+
* @param list<string> $scopes
17+
*/
1518
public function __construct(
1619
?UserInterface $user,
1720
string $accessTokenId,

src/Security/Authenticator/OAuth2Authenticator.php

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use League\Bundle\OAuth2ServerBundle\Security\Authentication\Token\OAuth2Token;
88
use League\Bundle\OAuth2ServerBundle\Security\Exception\OAuth2AuthenticationException;
99
use League\Bundle\OAuth2ServerBundle\Security\Exception\OAuth2AuthenticationFailedException;
10-
use League\Bundle\OAuth2ServerBundle\Security\Passport\Badge\OAuth2Badge;
1110
use League\Bundle\OAuth2ServerBundle\Security\User\NullUser;
1211
use League\OAuth2\Server\Exception\OAuthServerException;
1312
use League\OAuth2\Server\ResourceServer;
@@ -24,7 +23,6 @@
2423
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
2524
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
2625
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
27-
use Symfony\Component\Security\Http\Authenticator\Passport\UserPassportInterface;
2826
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
2927

3028
/**
@@ -93,15 +91,11 @@ public function authenticate(Request $request): PassportInterface
9391
/** @var list<string> $scopes */
9492
$scopes = $psr7Request->getAttribute('oauth_scopes', []);
9593

96-
$passport = new SelfValidatingPassport($this->getUserBadge($userIdentifier));
97-
98-
// BC Layer for 5.1 version
99-
if (!method_exists($passport, 'setAttribute')) {
100-
$passport->addBadge(new OAuth2Badge($accessTokenId, $scopes));
101-
} else {
102-
$passport->setAttribute('accessTokenId', $accessTokenId);
103-
$passport->setAttribute('scopes', $scopes);
104-
}
94+
$passport = new SelfValidatingPassport(new UserBadge($userIdentifier, function (string $userIdentifier): UserInterface {
95+
return '' !== $userIdentifier ? $this->userProvider->loadUserByUsername($userIdentifier) : new NullUser();
96+
}));
97+
$passport->setAttribute('accessTokenId', $accessTokenId);
98+
$passport->setAttribute('scopes', $scopes);
10599

106100
return $passport;
107101
}
@@ -111,25 +105,15 @@ public function authenticate(Request $request): PassportInterface
111105
*/
112106
public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
113107
{
114-
if (!$passport instanceof UserPassportInterface) {
115-
throw new \RuntimeException(sprintf('Cannot create a OAuth2 authenticated token. $passport should be a %s', UserPassportInterface::class));
108+
if (!$passport instanceof Passport) {
109+
throw new \RuntimeException(sprintf('Cannot create a OAuth2 authenticated token. $passport should be a %s', Passport::class));
116110
}
117111

118-
// BC Layer for 5.1 version
119-
/** @var Passport $passport */
120-
if (!method_exists($passport, 'getAttribute')) {
121-
/** @var OAuth2Badge $oauth2Badge */
122-
$oauth2Badge = $passport->getBadge(OAuth2Badge::class);
123-
124-
$accessTokenId = $oauth2Badge->getAccessTokenId();
125-
$scopes = $oauth2Badge->getScopes();
126-
} else {
127-
/** @var string $accessTokenId */
128-
$accessTokenId = $passport->getAttribute('accessTokenId');
112+
/** @var string $accessTokenId */
113+
$accessTokenId = $passport->getAttribute('accessTokenId');
129114

130-
/** @var list<string> $scopes */
131-
$scopes = $passport->getAttribute('scopes');
132-
}
115+
/** @var list<string> $scopes */
116+
$scopes = $passport->getAttribute('scopes');
133117

134118
$token = new OAuth2Token($passport->getUser(), $accessTokenId, $scopes, $this->rolePrefix);
135119
$token->setAuthenticated(true);
@@ -151,21 +135,4 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio
151135

152136
throw new UnauthorizedHttpException('Bearer', $exception->getMessage(), $exception);
153137
}
154-
155-
/**
156-
* @return UserBadge|UserInterface
157-
*/
158-
private function getUserBadge(string $userIdentifier)
159-
{
160-
$getUserCallable = function (string $userIdentifier): UserInterface {
161-
return '' !== $userIdentifier ? $this->userProvider->loadUserByUsername($userIdentifier) : new NullUser();
162-
};
163-
164-
// BC Layer for 5.1 version
165-
if (!class_exists(UserBadge::class)) {
166-
return $getUserCallable($userIdentifier);
167-
}
168-
169-
return new UserBadge($userIdentifier, $getUserCallable);
170-
}
171138
}

src/Security/Passport/Badge/OAuth2Badge.php

Lines changed: 0 additions & 63 deletions
This file was deleted.

tests/Unit/OAuth2AuthenticatorTest.php

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use League\Bundle\OAuth2ServerBundle\Security\Authenticator\OAuth2Authenticator;
88
use League\Bundle\OAuth2ServerBundle\Security\Exception\OAuth2AuthenticationFailedException;
9-
use League\Bundle\OAuth2ServerBundle\Security\Passport\Badge\OAuth2Badge;
109
use League\Bundle\OAuth2ServerBundle\Security\User\NullUser;
1110
use League\OAuth2\Server\Exception\OAuthServerException;
1211
use League\OAuth2\Server\ResourceServer;
@@ -96,21 +95,10 @@ public function testAuthenticateCreatePassport(): void
9695
/** @var Passport $passport */
9796
$passport = $authenticator->authenticate(new Request());
9897

99-
// BC Layer for 5.1 version
100-
if (!method_exists(Passport::class, 'getAttribute')) {
101-
/** @var OAuth2Badge $oauth2Badge */
102-
$oauth2Badge = $passport->getBadge(OAuth2Badge::class);
103-
$this->assertSame('accessTokenId', $oauth2Badge->getAccessTokenId());
104-
$this->assertSame(['scope_one', 'scope_two'], $oauth2Badge->getScopes());
105-
} else {
106-
$this->assertSame('accessTokenId', $passport->getAttribute('accessTokenId'));
107-
$this->assertSame(['scope_one', 'scope_two'], $passport->getAttribute('scopes'));
108-
}
98+
$this->assertSame('accessTokenId', $passport->getAttribute('accessTokenId'));
99+
$this->assertSame(['scope_one', 'scope_two'], $passport->getAttribute('scopes'));
109100

110-
// BC Layer for 5.1 version
111-
if (class_exists(UserBadge::class)) {
112-
$passport->getUser();
113-
}
101+
$passport->getUser();
114102
}
115103

116104
public function testAuthenticateCreatePassportWithNullUser(): void
@@ -161,14 +149,8 @@ public function testCreateAuthenticatedToken(): void
161149
}
162150

163151
$passport = new SelfValidatingPassport($userBadge);
164-
165-
// BC Layer for 5.1 version
166-
if (!method_exists($passport, 'setAttribute')) {
167-
$passport->addBadge(new OAuth2Badge('accessTokenId', ['scope_one', 'scope_two']));
168-
} else {
169-
$passport->setAttribute('accessTokenId', 'accessTokenId');
170-
$passport->setAttribute('scopes', ['scope_one', 'scope_two']);
171-
}
152+
$passport->setAttribute('accessTokenId', 'accessTokenId');
153+
$passport->setAttribute('scopes', ['scope_one', 'scope_two']);
172154

173155
$authenticator = new OAuth2Authenticator(
174156
$this->createMock(HttpMessageFactoryInterface::class),

0 commit comments

Comments
 (0)