-
-
Notifications
You must be signed in to change notification settings - Fork 437
[make:registration] drop guard authentication support #1243
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
Changes from all commits
977835c
7c25f0b
13348d5
340ab01
824451f
ef75722
97fcbb1
caaae50
0d96a67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,8 @@ | |
|
|
||
| namespace Symfony\Bundle\MakerBundle\Security; | ||
|
|
||
| use Symfony\Bundle\MakerBundle\Security\Model\Authenticator; | ||
| use Symfony\Bundle\MakerBundle\Security\Model\AuthenticatorType; | ||
| use Symfony\Bundle\MakerBundle\Str; | ||
| use Symfony\Bundle\MakerBundle\Validator; | ||
| use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
|
@@ -140,24 +142,6 @@ public function guessPasswordField(SymfonyStyle $io, string $userClass): string | |
| ); | ||
| } | ||
|
|
||
| public function getAuthenticatorClasses(array $firewallData): array | ||
| { | ||
| if (isset($firewallData['guard'])) { | ||
| return array_filter($firewallData['guard']['authenticators'] ?? [], static fn ($authenticator) => class_exists($authenticator)); | ||
| } | ||
|
|
||
| if (isset($firewallData['custom_authenticator'])) { | ||
| $authenticators = $firewallData['custom_authenticator']; | ||
| if (\is_string($authenticators)) { | ||
| $authenticators = [$authenticators]; | ||
| } | ||
|
|
||
| return array_filter($authenticators, static fn ($authenticator) => class_exists($authenticator)); | ||
| } | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| public function guessPasswordSetter(SymfonyStyle $io, string $userClass): string | ||
| { | ||
| if (null === ($methodChoices = $this->methodNameGuesser($userClass, 'setPassword'))) { | ||
|
|
@@ -196,6 +180,96 @@ public function guessIdGetter(SymfonyStyle $io, string $userClass): string | |
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, array<string, mixed>> $firewalls Config data from security.firewalls | ||
| * | ||
| * @return Authenticator[] | ||
| */ | ||
| public function getAuthenticatorsFromConfig(array $firewalls): array | ||
| { | ||
| $authenticators = []; | ||
|
|
||
| /* Iterate over each firewall that exists e.g. security.firewalls.main | ||
| * $firewallName could be "main" or "dev", etc... | ||
| * $firewallConfig should be an array of the firewalls params | ||
| */ | ||
| foreach ($firewalls as $firewallName => $firewallConfig) { | ||
| if (!\is_array($firewallConfig)) { | ||
| continue; | ||
| } | ||
|
|
||
| $authenticators = [ | ||
| ...$authenticators, | ||
| ...$this->getAuthenticatorsFromConfigData($firewallConfig, $firewallName), | ||
| ]; | ||
| } | ||
|
|
||
| return $authenticators; | ||
| } | ||
|
|
||
| /** | ||
| * Pass in a firewalls config e.g. security.firewalls.main like: | ||
| * pattern: ^/path | ||
| * form_login: | ||
| * login_path: app_login | ||
| * custom_authenticator: | ||
| * - App\Security\MyAuthenticator | ||
| * | ||
| * @param array<string, mixed> $firewallConfig | ||
| * | ||
| * @return Authenticator[] | ||
| */ | ||
| private function getAuthenticatorsFromConfigData(array $firewallConfig, string $firewallName): array | ||
| { | ||
| $authenticators = []; | ||
|
|
||
| foreach ($firewallConfig as $potentialAuthenticator => $configData) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ahh... lost the review comment on push... @weaverryan: firewalls:
main:
form_login:
path: /loginWon't $configData at this point be ['path' => '/login']? Or am I totally misreading the method?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no I think you're thinking 1 level too deep.. i refactored this to split up iterating over We pass in main:
path: '/some-path'
form_login:
- option
....
custom_authenticators:
- some authenticator
anotherFirewall:
dev:
test:
Now If its Atleast this is what im trying todo, but i could be missing something myself 🤕
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, it looks clear now 👍 |
||
| // Check if $potentialAuthenticator is a supported authenticator or if its some other key. | ||
| if (null === ($authenticator = AuthenticatorType::tryFrom($potentialAuthenticator))) { | ||
| // $potentialAuthenticator is probably something like "pattern" or "lazy", not an authenticator | ||
| continue; | ||
| } | ||
|
|
||
| // $potentialAuthenticator is a supported authenticator. Check if it's a custom_authenticator. | ||
| if (AuthenticatorType::CUSTOM !== $authenticator) { | ||
| // We found a "built in" authenticator - "form_login", "json_login", etc... | ||
| $authenticators[] = new Authenticator($authenticator, $firewallName); | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| /* | ||
| * $potentialAuthenticator = custom_authenticator. | ||
| * $configData is either [App\MyAuthenticator] or (string) App\MyAuthenticator | ||
| */ | ||
| $customAuthenticators = $this->getCustomAuthenticators($configData, $firewallName); | ||
|
|
||
| $authenticators = [...$authenticators, ...$customAuthenticators]; | ||
| } | ||
|
|
||
| return $authenticators; | ||
| } | ||
|
|
||
| /** | ||
| * @param string|array<string> $customAuthenticators A single entry from custom_authenticators or an array of authenticators | ||
| * | ||
| * @return Authenticator[] | ||
| */ | ||
| private function getCustomAuthenticators(string|array $customAuthenticators, string $firewallName): array | ||
| { | ||
| if (\is_string($customAuthenticators)) { | ||
| $customAuthenticators = [$customAuthenticators]; | ||
| } | ||
|
|
||
| $authenticators = []; | ||
|
|
||
| foreach ($customAuthenticators as $customAuthenticatorClass) { | ||
| $authenticators[] = new Authenticator(AuthenticatorType::CUSTOM, $firewallName, $customAuthenticatorClass); | ||
| } | ||
|
|
||
| return $authenticators; | ||
| } | ||
|
|
||
| private function methodNameGuesser(string $className, string $suspectedMethodName): ?array | ||
| { | ||
| $reflectionClass = new \ReflectionClass($className); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony MakerBundle package. | ||
| * | ||
| * (c) Fabien Potencier <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\Bundle\MakerBundle\Security\Model; | ||
|
|
||
| /** | ||
| * @author Jesse Rushlow<[email protected]> | ||
| * | ||
| * @internal | ||
| */ | ||
| final class Authenticator | ||
| { | ||
| public function __construct( | ||
| public AuthenticatorType $type, | ||
| public string $firewallName, | ||
| public ?string $authenticatorClass = null, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * Useful for asking questions like "Which authenticator do you want to use?". | ||
| */ | ||
| public function __toString(): string | ||
| { | ||
| return sprintf( | ||
| '"%s" in the "%s" firewall', | ||
| $this->authenticatorClass ?? $this->type->value, | ||
| $this->firewallName, | ||
| ); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony MakerBundle package. | ||
| * | ||
| * (c) Fabien Potencier <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\Bundle\MakerBundle\Security\Model; | ||
|
|
||
| /** | ||
| * @author Jesse Rushlow <[email protected]> | ||
| * | ||
| * @internal | ||
| */ | ||
| enum AuthenticatorType: string | ||
| { | ||
| case FORM_LOGIN = 'form_login'; | ||
| case JSON_LOGIN = 'json_login'; | ||
| case HTTP_BASIC = 'http_basic'; | ||
| case LOGIN_LINK = 'login_link'; | ||
| case ACCESS_TOKEN = 'access_token'; | ||
| case X509 = 'x509'; | ||
| case REMOTE_USER = 'remote_user'; | ||
|
|
||
| case CUSTOM = 'custom_authenticator'; | ||
| } |
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.
getAuthenticatorsInFirewallsConfig?