-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] Lazily load the user during the check passport event #37846
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
fabpot
merged 1 commit into
symfony:master
from
wouterj:issue-37436/user-in-checkpassword-event
Aug 27, 2020
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
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
52 changes: 52 additions & 0 deletions
52
src/Symfony/Bundle/SecurityBundle/Tests/Functional/AuthenticatorTest.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,52 @@ | ||
<?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\Bundle\SecurityBundle\Tests\Functional; | ||
|
||
class AuthenticatorTest extends AbstractWebTestCase | ||
{ | ||
/** | ||
* @dataProvider provideEmails | ||
*/ | ||
public function testGlobalUserProvider($email) | ||
{ | ||
$client = $this->createClient(['test_case' => 'Authenticator', 'root_config' => 'implicit_user_provider.yml']); | ||
|
||
$client->request('GET', '/profile', [], [], [ | ||
'HTTP_X-USER-EMAIL' => $email, | ||
]); | ||
$this->assertJsonStringEqualsJsonString('{"email":"'.$email.'"}', $client->getResponse()->getContent()); | ||
} | ||
|
||
/** | ||
* @dataProvider provideEmails | ||
*/ | ||
public function testFirewallUserProvider($email, $withinFirewall) | ||
{ | ||
$client = $this->createClient(['test_case' => 'Authenticator', 'root_config' => 'firewall_user_provider.yml']); | ||
|
||
$client->request('GET', '/profile', [], [], [ | ||
'HTTP_X-USER-EMAIL' => $email, | ||
]); | ||
|
||
if ($withinFirewall) { | ||
$this->assertJsonStringEqualsJsonString('{"email":"'.$email.'"}', $client->getResponse()->getContent()); | ||
} else { | ||
$this->assertJsonStringEqualsJsonString('{"error":"Username could not be found."}', $client->getResponse()->getContent()); | ||
} | ||
} | ||
|
||
public function provideEmails() | ||
{ | ||
yield ['jane@example.org', true]; | ||
yield ['john@example.org', false]; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...ny/Bundle/SecurityBundle/Tests/Functional/Bundle/AuthenticatorBundle/ApiAuthenticator.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,53 @@ | ||
<?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\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle; | ||
|
||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
use Symfony\Component\Security\Core\Exception\BadCredentialsException; | ||
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport; | ||
|
||
class ApiAuthenticator extends AbstractAuthenticator | ||
{ | ||
public function supports(Request $request): ?bool | ||
{ | ||
return $request->headers->has('X-USER-EMAIL'); | ||
} | ||
|
||
public function authenticate(Request $request): PassportInterface | ||
{ | ||
$email = $request->headers->get('X-USER-EMAIL'); | ||
if (false === strpos($email, '@')) { | ||
throw new BadCredentialsException('Email is not a valid email address.'); | ||
} | ||
|
||
return new SelfValidatingPassport(new UserBadge($email)); | ||
} | ||
|
||
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response | ||
{ | ||
return null; | ||
} | ||
|
||
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response | ||
{ | ||
return new JsonResponse([ | ||
'error' => $exception->getMessageKey(), | ||
], JsonResponse::HTTP_FORBIDDEN); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...y/Bundle/SecurityBundle/Tests/Functional/Bundle/AuthenticatorBundle/ProfileController.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,24 @@ | ||
<?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\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
|
||
class ProfileController extends AbstractController | ||
{ | ||
public function __invoke() | ||
{ | ||
$this->denyAccessUnlessGranted('ROLE_USER'); | ||
|
||
return $this->json(['email' => $this->getUser()->getUsername()]); | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/bundles.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,15 @@ | ||
<?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. | ||
*/ | ||
|
||
return [ | ||
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), | ||
new Symfony\Bundle\SecurityBundle\SecurityBundle(), | ||
]; |
33 changes: 33 additions & 0 deletions
33
src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/config.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,33 @@ | ||
framework: | ||
secret: test | ||
router: { resource: "%kernel.project_dir%/%kernel.test_case%/routing.yml", utf8: true } | ||
test: ~ | ||
default_locale: en | ||
profiler: false | ||
session: | ||
storage_id: session.storage.mock_file | ||
|
||
services: | ||
logger: { class: Psr\Log\NullLogger } | ||
Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\ProfileController: | ||
public: true | ||
calls: | ||
- ['setContainer', ['@Psr\Container\ContainerInterface']] | ||
tags: [container.service_subscriber] | ||
Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\ApiAuthenticator: ~ | ||
|
||
security: | ||
enable_authenticator_manager: true | ||
|
||
encoders: | ||
Symfony\Component\Security\Core\User\User: plaintext | ||
|
||
providers: | ||
in_memory: | ||
memory: | ||
users: | ||
'jane@example.org': { password: test, roles: [ROLE_USER] } | ||
in_memory2: | ||
memory: | ||
users: | ||
'john@example.org': { password: test, roles: [ROLE_USER] } |
10 changes: 10 additions & 0 deletions
10
...mfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/firewall_user_provider.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,10 @@ | ||
imports: | ||
- { resource: ./config.yml } | ||
|
||
security: | ||
firewalls: | ||
api: | ||
pattern: / | ||
provider: in_memory | ||
custom_authenticator: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\ApiAuthenticator | ||
|
9 changes: 9 additions & 0 deletions
9
...mfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/implicit_user_provider.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,9 @@ | ||
imports: | ||
- { resource: ./config.yml } | ||
|
||
security: | ||
firewalls: | ||
api: | ||
pattern: / | ||
custom_authenticator: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\ApiAuthenticator | ||
|
4 changes: 4 additions & 0 deletions
4
src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/routing.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,4 @@ | ||
profile: | ||
path: /profile | ||
defaults: | ||
_controller: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\ProfileController |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this first service used? I may just be missing it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the idea is:
provider
set, register the same listener for that specific firewall with only the configured firewall at priority 2048The listener does not configure the user provider if a user loader is already set (e.g. by the authenticator using a closure or by the firewall specific listener).