Skip to content

Commit dc89d46

Browse files
committed
Prevent empty userIdentifier
1 parent 0771240 commit dc89d46

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/Security/Authenticator/OAuth2Authenticator.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Symfony\Component\HttpFoundation\Response;
1717
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1818
use Symfony\Component\Security\Core\Exception\AuthenticationException;
19+
use Symfony\Component\Security\Core\User\AttributesBasedUserProviderInterface;
20+
use Symfony\Component\Security\Core\User\ChainUserProvider;
1921
use Symfony\Component\Security\Core\User\UserInterface;
2022
use Symfony\Component\Security\Core\User\UserProviderInterface;
2123
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
@@ -70,6 +72,14 @@ public function authenticate(Request $request): Passport
7072

7173
/** @var string $userIdentifier */
7274
$userIdentifier = $psr7Request->getAttribute('oauth_user_id', '');
75+
if ('' === $userIdentifier) {
76+
/**
77+
* BC layer for Symfony < 8.0
78+
*/
79+
if (is_a(ChainUserProvider::class, AttributesBasedUserProviderInterface::class, true)) {
80+
throw OAuth2AuthenticationFailedException::create('The access token has either an empty or missing "oauth_user_id" attribute.');
81+
}
82+
}
7383

7484
/** @var string $accessTokenId */
7585
$accessTokenId = $psr7Request->getAttribute('oauth_access_token_id');
@@ -81,7 +91,10 @@ public function authenticate(Request $request): Passport
8191
$oauthClientId = $psr7Request->getAttribute('oauth_client_id', '');
8292

8393
$userLoader = function (string $userIdentifier) use ($oauthClientId): UserInterface {
84-
if ('' === $userIdentifier || $oauthClientId === $userIdentifier) {
94+
if (
95+
$oauthClientId === $userIdentifier
96+
|| ('' === $userIdentifier && is_a(ChainUserProvider::class, AttributesBasedUserProviderInterface::class, true)) // BC layer for Symfony < 8.0
97+
) {
8598
return new ClientCredentialsUser($oauthClientId);
8699
}
87100

tests/Unit/OAuth2AuthenticatorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ public function testAuthenticateCreatePassportWithClientCredentialsUser(): void
9797
{
9898
$serverRequest = (new ServerRequest('GET', '/foo'))
9999
->withAttribute('oauth_access_token_id', 'accessTokenId')
100+
->withAttribute('oauth_user_id', 'clientId')
101+
->withAttribute('oauth_client_id', 'clientId')
100102
;
101103

102104
$httpMessageFactory = $this->createMock(HttpMessageFactoryInterface::class);

0 commit comments

Comments
 (0)