-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] Fix loading user from UserBadge #51104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...fony/Bundle/SecurityBundle/Tests/Functional/app/AccessToken/config_custom_user_loader.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
imports: | ||
- { resource: ./../config/framework.yml } | ||
|
||
framework: | ||
http_method_override: false | ||
serializer: ~ | ||
|
||
security: | ||
password_hashers: | ||
Symfony\Component\Security\Core\User\InMemoryUser: plaintext | ||
|
||
providers: | ||
in_memory: | ||
memory: | ||
users: | ||
dunglas: { password: foo, roles: [ROLE_MISSING] } | ||
|
||
firewalls: | ||
main: | ||
pattern: ^/ | ||
stateless: true | ||
access_token: | ||
token_handler: access_token.access_token_handler | ||
token_extractors: 'header' | ||
realm: 'My API' | ||
|
||
access_control: | ||
- { path: ^/foo, roles: ROLE_USER } | ||
|
||
services: | ||
access_token.access_token_handler: | ||
class: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AccessTokenBundle\Security\Handler\AccessTokenHandler |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/Symfony/Component/Security/Http/Authenticator/FallbackUserLoader.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Http\Authenticator; | ||
|
||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
/** | ||
* This wrapper serves as a marker interface to indicate badge user loaders that should not be overridden by the | ||
* default user provider. | ||
* | ||
* @internal | ||
*/ | ||
final class FallbackUserLoader | ||
{ | ||
public function __construct(private $inner) | ||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
} | ||
|
||
public function __invoke(mixed ...$args): ?UserInterface | ||
{ | ||
return ($this->inner)(...$args); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Authenticator; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Security\Core\Exception\BadCredentialsException; | ||
use Symfony\Component\Security\Core\User\InMemoryUser; | ||
use Symfony\Component\Security\Core\User\InMemoryUserProvider; | ||
use Symfony\Component\Security\Http\AccessToken\AccessTokenExtractorInterface; | ||
use Symfony\Component\Security\Http\AccessToken\AccessTokenHandlerInterface; | ||
use Symfony\Component\Security\Http\Authenticator\AccessTokenAuthenticator; | ||
use Symfony\Component\Security\Http\Authenticator\FallbackUserLoader; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; | ||
|
||
class AccessTokenAuthenticatorTest extends TestCase | ||
{ | ||
private AccessTokenHandlerInterface $accessTokenHandler; | ||
private AccessTokenExtractorInterface $accessTokenExtractor; | ||
private InMemoryUserProvider $userProvider; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->accessTokenHandler = $this->createMock(AccessTokenHandlerInterface::class); | ||
$this->accessTokenExtractor = $this->createMock(AccessTokenExtractorInterface::class); | ||
$this->userProvider = new InMemoryUserProvider(['test' => ['password' => 's$cr$t']]); | ||
} | ||
|
||
public function testAuthenticateWithoutAccessToken() | ||
{ | ||
$this->expectException(BadCredentialsException::class); | ||
$this->expectExceptionMessage('Invalid credentials.'); | ||
|
||
$request = Request::create('/test'); | ||
|
||
$this->accessTokenExtractor | ||
->expects($this->once()) | ||
->method('extractAccessToken') | ||
->with($request) | ||
->willReturn(null); | ||
|
||
$authenticator = new AccessTokenAuthenticator( | ||
$this->accessTokenHandler, | ||
$this->accessTokenExtractor, | ||
); | ||
|
||
$authenticator->authenticate($request); | ||
} | ||
|
||
public function testAuthenticateWithoutProvider() | ||
{ | ||
$request = Request::create('/test'); | ||
|
||
$this->accessTokenExtractor | ||
->expects($this->once()) | ||
->method('extractAccessToken') | ||
->with($request) | ||
->willReturn('test'); | ||
$this->accessTokenHandler | ||
->expects($this->once()) | ||
->method('getUserBadgeFrom') | ||
->with('test') | ||
->willReturn(new UserBadge('john', fn () => new InMemoryUser('john', null))); | ||
|
||
$authenticator = new AccessTokenAuthenticator( | ||
$this->accessTokenHandler, | ||
$this->accessTokenExtractor, | ||
$this->userProvider, | ||
); | ||
|
||
$passport = $authenticator->authenticate($request); | ||
|
||
$this->assertEquals('john', $passport->getUser()->getUserIdentifier()); | ||
} | ||
|
||
public function testAuthenticateWithoutUserLoader() | ||
{ | ||
$request = Request::create('/test'); | ||
|
||
$this->accessTokenExtractor | ||
->expects($this->once()) | ||
->method('extractAccessToken') | ||
->with($request) | ||
->willReturn('test'); | ||
$this->accessTokenHandler | ||
->expects($this->once()) | ||
->method('getUserBadgeFrom') | ||
->with('test') | ||
->willReturn(new UserBadge('test')); | ||
|
||
$authenticator = new AccessTokenAuthenticator( | ||
$this->accessTokenHandler, | ||
$this->accessTokenExtractor, | ||
$this->userProvider, | ||
); | ||
|
||
$passport = $authenticator->authenticate($request); | ||
|
||
$this->assertEquals('test', $passport->getUser()->getUserIdentifier()); | ||
} | ||
|
||
public function testAuthenticateWithUserLoader() | ||
{ | ||
$request = Request::create('/test'); | ||
|
||
$this->accessTokenExtractor | ||
->expects($this->once()) | ||
->method('extractAccessToken') | ||
->with($request) | ||
->willReturn('test'); | ||
$this->accessTokenHandler | ||
->expects($this->once()) | ||
->method('getUserBadgeFrom') | ||
->with('test') | ||
->willReturn(new UserBadge('john', fn () => new InMemoryUser('john', null))); | ||
|
||
$authenticator = new AccessTokenAuthenticator( | ||
$this->accessTokenHandler, | ||
$this->accessTokenExtractor, | ||
$this->userProvider, | ||
); | ||
|
||
$passport = $authenticator->authenticate($request); | ||
|
||
$this->assertEquals('john', $passport->getUser()->getUserIdentifier()); | ||
} | ||
|
||
public function testAuthenticateWithFallbackUserLoader() | ||
{ | ||
$request = Request::create('/test'); | ||
|
||
$this->accessTokenExtractor | ||
->expects($this->once()) | ||
->method('extractAccessToken') | ||
->with($request) | ||
->willReturn('test'); | ||
$this->accessTokenHandler | ||
->expects($this->once()) | ||
->method('getUserBadgeFrom') | ||
->with('test') | ||
->willReturn(new UserBadge('test', new FallbackUserLoader(fn () => new InMemoryUser('john', null)))); | ||
|
||
$authenticator = new AccessTokenAuthenticator( | ||
$this->accessTokenHandler, | ||
$this->accessTokenExtractor, | ||
$this->userProvider, | ||
); | ||
|
||
$passport = $authenticator->authenticate($request); | ||
|
||
$this->assertEquals('test', $passport->getUser()->getUserIdentifier()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.