Skip to content

[Security] added userChecker to SimpleAuthenticationProvider #26370

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

Closed
wants to merge 4 commits into from

Conversation

i3or1s
Copy link
Contributor

@i3or1s i3or1s commented Mar 2, 2018

[Security] added userChecker to SimpleAuthenticationProvider
[SecurityBundle] [DependencyInjection] updated SimpleFormFactory

Q A
Branch? 2.8
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #26314
License MIT
Doc PR no

Introduces user checker to the simple authentication provider.


public function __construct(SimpleAuthenticatorInterface $simpleAuthenticator, UserProviderInterface $userProvider, $providerKey)
public function __construct(SimpleAuthenticatorInterface $simpleAuthenticator, UserProviderInterface $userProvider, $providerKey, UserCheckerInterface $userChecker)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

must be default null for BC

Copy link
Contributor Author

@i3or1s i3or1s Mar 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chalasr Should it use Symfony\Component\Security\Core\User\UserChecker as default if null is present?
This would leave the method as is (without if statements)?

Added UserChecker to SimpleAuthenticationProvider and updated SimpleFormFactory
}

public function authenticate(TokenInterface $token)
{
$user = $this->userProvider->loadUserByUsername($token->getUsername());
$this->userChecker->checkPreAuth($user);
$authToken = $this->simpleAuthenticator->authenticateToken($token, $this->userProvider, $this->providerKey);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I understand the need to have the user here, it would be loaded twice now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iltar maybe then skip the pre auth check since it can be done during authentication of the token.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if you do $this->userChecker->checkPreAuth($authToken->getUser())? I know it's not exactly correct flow wise, but it beats fetching the user twice 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iltar I was thinking about it but it sounds strange to me to pre authenticate authenticated token.
Changin interface to exclude the userProvider would cause BC breaks.
You are correct about twice loading the user and simple authenticator could do the pre-check if needed.
Maybe best is to remove the checkPreAuth

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it might seem strange, guard does it before/after the credentials check:

$this->userChecker->checkPreAuth($user);
if (true !== $guardAuthenticator->checkCredentials($token->getCredentials(), $user)) {
throw new BadCredentialsException(sprintf('Authentication failed because %s::checkCredentials() did not return true.', get_class($guardAuthenticator)));
}
$this->userChecker->checkPostAuth($user);

I don't see another option to be honest, leaving it out would be as not having it at all.

retrieve user from authToken, rather then fetching it from userProvider
}

public function authenticate(TokenInterface $token)
{
$authToken = $this->simpleAuthenticator->authenticateToken($token, $this->userProvider, $this->providerKey);
$this->userChecker->checkPreAuth($authToken->getUser());
$this->userChecker->checkPostAuth($authToken->getUser());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be moved after the instanceof check below

apply userChecker after authToken is authenticated
}

throw new AuthenticationException('Simple authenticator failed to return an authenticated token.');
$this->userChecker->checkPreAuth($authToken->getUser());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to call $authToken->getUser() twice ?

@Simperfit
Copy link
Contributor

Can we add a test for this class ?

test for userChecker postAuth and preAuth
@chalasr
Copy link
Member

chalasr commented Mar 6, 2018

For 2.7

@chalasr chalasr modified the milestones: 2.8, 2.7 Mar 6, 2018
@nicolas-grekas
Copy link
Member

Thank you @i3or1s.

nicolas-grekas added a commit that referenced this pull request Mar 19, 2018
…er (i3or1s)

This PR was submitted for the 2.8 branch but it was squashed and merged into the 2.7 branch instead (closes #26370).

Discussion
----------

[Security] added userChecker to SimpleAuthenticationProvider

[Security] added userChecker to SimpleAuthenticationProvider
[SecurityBundle] [DependencyInjection] updated SimpleFormFactory

| Q             | A
| ------------- | ---
| Branch?       | 2.8
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #26314
| License       | MIT
| Doc PR        | no

Introduces user checker to the simple authentication provider.

Commits
-------

cb9c92d [Security] added userChecker to SimpleAuthenticationProvider
This was referenced Apr 2, 2018
@fabpot fabpot mentioned this pull request Apr 3, 2018
@Arthur-min
Copy link

Hello,

I have an issue with this update:

Service "security.authentication.provider.simple_form.main": The index "3" is not in the range [0, 2].
in Definition.php (line 263)

Any idea how to fix that?

Thanks!

@nicolas-grekas
Copy link
Member

@Arthur-min see #26762 (please don't comment on closed issues/PRs)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants