Skip to content

Added missing security.yaml config #13418

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 1 commit into from
Oct 14, 2020
Merged

Conversation

ThomasLandauer
Copy link
Contributor

I just copied this over from https://symfony.com/doc/3.4/security/guard_authentication.html - since it probably just got lost somehow. When following the instructions at https://symfony.com/doc/current/security.html , you would still have property: email in there.

ThomasLandauer added a commit to ThomasLandauer/symfony-docs that referenced this pull request Apr 4, 2020
Renaming more occurrences of `apiKey` to `apiToken`, thanks to symfony#13418 (comment)
@xabbuh xabbuh added this to the 4.4 milestone Apr 21, 2020
@xabbuh
Copy link
Member

xabbuh commented Apr 21, 2020

Wouldn't this change mean that the user now would have to enter their api token when using the login form? That doesn't look right to me. And inside the authenticator we do not use the user provider at all so for that to make it work this change is not necessary if I do not miss anything.

@ThomasLandauer
Copy link
Contributor Author

ThomasLandauer commented Apr 21, 2020

Well, there is no login form ;-) This is about the API token. What I'm trying to fix is that at https://symfony.com/doc/current/security/guard_authentication.html#step-3-configure-the-authenticator there are some parts missing (commented out) of security.yaml, and I'm just telling people what's expected to be in there.

And inside the authenticator we do not use the user provider at all

Token authentication works without a user provider? Well, if that's really the case (and you certainly know that better than I do) this PR is obviously obsolete. (Usually I try things out before, but I can't remember the details).

So in this case there should be a note that people can delete their user provider (which they still will have, if they are coming from https://symfony.com/doc/current/security.html#guard-authenticators )

@xabbuh
Copy link
Member

xabbuh commented Apr 21, 2020

Token authentication works without a user provider? Well, if that's really the case (and you certainly know that better than I do) this PR is obviously obsolete. (Usually I try things out before, but I can't remember the details).

The authenticator will get the user provider passed to its getUser() method. But in our concrete example the method is implemented like this:

public function getUser($credentials, UserProviderInterface $userProvider)
    {
        if (null === $credentials) {
            // The token header was empty, authentication fails with HTTP Status
            // Code 401 "Unauthorized"
            return null;
        }

        // if a User is returned, checkCredentials() is called
        return $this->em->getRepository(User::class)
            ->findOneBy(['apiToken' => $credentials])
        ;
    }

I think this approach was used as we do not load the user by username (which we actually do not know) here.

@xabbuh
Copy link
Member

xabbuh commented Apr 21, 2020

So maybe it would be better to have an authentication method that does not only use a token, but also an identifier.

@ThomasLandauer
Copy link
Contributor Author

So maybe it would be better to have an authentication method that does not only use a token, but also an identifier.

No, I think that's OK; not having an identifier is the reason for having a token, after all.

I'll just drop in a short note somewhere à la "If you're doing it this way, you don't need a user provider".

OK?

@wouterj
Copy link
Member

wouterj commented Apr 21, 2020

Hmm, this is exactly one of the pain points of guards that will be fixed in the experimental authenticator system. As the user provider argument is not nullable, you must have a user provider, even though you don't use it at all (so it can be a completely empty InMemoryUserProvider).

As it's so ugly, it's probably best to "hide" it a bit in the docs. So I'm in favor of not adding more examples/text, but instead modify the configuration example in "Step 3) Configure the Authenticator". If we replace the first # ... comment with your user provider config, I think it should be clear enough that you must configure some sort of user provider. Please note that the article describes in "Step 1" that one must first follow the main guide, so most should have some sort of user provider set up already (as they also already have a working user entity).

(also note that with this config, you can use $userProvider->loadUserByUsername($apiToken), as you've configured apiToken to be the username, but I think it'll be too confusing if we would use this)

@ThomasLandauer
Copy link
Contributor Author

but instead modify the configuration example in "Step 3) Configure the Authenticator". If we replace the first # ... comment with your user provider config

Yes, good idea!

you can use $userProvider->loadUserByUsername($apiToken)

You mean inside getUser(), instead of ->findOneBy(['apiToken' => $credentials])? Well, why not? If we need to set some user provider anyway, why not set the "right" one right away? I don't think that's confusing. Which is the cleaner/better way?

@wouterj
Copy link
Member

wouterj commented Oct 4, 2020

Sorry for failing to react on your questions in this issue @ThomasLandauer.

Well, why not? If we need to set some user provider anyway, why not set the "right" one right away? I don't think that's confusing. Which is the cleaner/better way?

Yes, alright. But let's add a small comment that explains that "username" is the property configured by property (thus apiKey).

Do you want to update this PR, or shall I create a new one based on your changes?

ThomasLandauer added a commit to ThomasLandauer/symfony-docs that referenced this pull request Oct 5, 2020
@ThomasLandauer
Copy link
Contributor Author

I hope I got what you mean :-)

Copy link
Member

@wouterj wouterj left a comment

Choose a reason for hiding this comment

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

I like it this way. We show a (more complete) example, and we explain the "weird" behavior of the username property when using user providers.

Thanks for your quick response, @ThomasLandauer

Copy link
Contributor

@OskarStark OskarStark left a comment

Choose a reason for hiding this comment

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

👍

@wouterj
Copy link
Member

wouterj commented Oct 14, 2020

Thank you @ThomasLandauer!

@wouterj wouterj merged commit 9953a11 into symfony:4.4 Oct 14, 2020
@ThomasLandauer ThomasLandauer deleted the patch-6 branch October 14, 2020 12:17
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