Skip to content
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

Potential SF6 Compat Issue - JWTManager::addUserIdentityToPayload() by default requires a username property or getUsername method #881

Closed
mbabker opened this issue Jun 23, 2021 · 14 comments · Fixed by #1037
Labels

Comments

@mbabker
Copy link
Contributor

mbabker commented Jun 23, 2021

With the default bundle configuration, JWTManager::addUserIdentityToPayload() adds the user identity to the JWT payload by using a property accessor to fetch the configured userIdentityField from the User object, which defaults to "username". With UserInterface::getUsername() going away in Symfony 6, this default configuration looks like it could start to cause problems in one of two ways:

  • This bundle will require each UserInterface implementation to have either a $username property or a getUsername() method with the out-of-the-box configuration
  • Changing the $userIdentityField property to "userIdentifier" to match the updated UserInterface (since the implementation details of the property accessor say it will try to use a getter for that field name) by default also changes the name of the user ID claim in the JWT payload, unless you also configure the user_id_claim config node which cascades down to the $userIdClaim property; IMO these types of changes to the defaults in the bundle would be a B/C break so couldn't really be considered before a 3.0 release (but, bundle users can make updates through the bundle config)

Personally, I'd say to deprecate the user_identity_field bundle config and the userIdentityField property in the manager and just use the UserInterface by default, and if someone needs to use something that isn't the result of getUsername() or getUserIdentifier(), they can subclass the manager and override the addUserIdentityToPayload() method, but I also realize #40 added this for a specific reason so maybe that isn't the greatest of solutions.

@chalasr
Copy link
Collaborator

chalasr commented Jul 3, 2021

Thanks for the issue. You're right here.
We probably need to change the default identity field to user_identifier instead of username.
I'll take care of this.

@slowwriter
Copy link

Thanks for the issue. You're right here.
We probably need to change the default identity field to user_identifier instead of username.
I'll take care of this.

@chalasr Exactly as you write. It is a simple and very important thing. When can we count on a correction?

@chalasr
Copy link
Collaborator

chalasr commented Sep 16, 2021

Thanks for the ping @malkavi6, I totally forgot about this. I'm going to look at it in the coming days

@chalasr chalasr added the Bug label Sep 16, 2021
@slowwriter
Copy link

Bump. I need to be able to replace the username with another user property.

@tarlepp
Copy link

tarlepp commented Jan 15, 2022

same issue here, is there a way to configure what field is used?

@tarlepp
Copy link

tarlepp commented Jan 15, 2022

maybe there should just be check like;

if (method_exists($user, 'getUserIdentifier')

and not rely on hard coded userIdentityField

@mbabker
Copy link
Contributor Author

mbabker commented Jan 15, 2022

This is what I'm using right now, we haven't moved up to Symfony 6 yet but from what I remember this shouldn't cause any issues:

lexik_jwt_authentication:
    user_identity_field: 'email' # Any of the properties/getters in the user object
    user_id_claim: 'username' # The fieldname used in the JWT payload for the user identity

@chalasr
Copy link
Collaborator

chalasr commented Jan 16, 2022

I will look into this asap, hopefully next week.

@chalasr
Copy link
Collaborator

chalasr commented Feb 6, 2022

I agree that config option is not needed.
I'm going to deprecate both the user_identity_field config option and the set/getUserIdentityField() methods to just rely on Symfony UserInterface::getUserIdentifier() instead. Please tell me if you see any cons.

@slowwriter
Copy link

Perfect, that's what I meant.

@CaptainFalcon92
Copy link

Hello.
Any commit yet for this ? I'm still having this error.
Meanwhile i do use user_identity_field in config.

@chalasr
Copy link
Collaborator

chalasr commented Jun 11, 2022

See #1037

@Aweptimum
Copy link

Should the docs be updated to reflect this change? I was looking at setting up the bundle, realized it assumed a username field, and then went hunting for how to instead use something like email. I found this issue after a while, and it looks like the linked PR switches to using getUserIdentifier which, if I understand, means there's not really any config needed to handle the identifying field/key? If so, the Configuration Reference doc still contains the user_identity_field config. I also cannot find any reference in the docs on the 2.1x branch explaining that users of the bundle can rely on their UserProvider's UserInterface:getUserIdentifier() method.

I'd be willing to pr removing user_identity_field from the configuration reference, but not sure where to add some language for getUserIdentifier. Do you have a good spot in mind, chalasr?

Thanks for adding this change though 👌

@chalasr
Copy link
Collaborator

chalasr commented Oct 9, 2022

@Aweptimum I think a small comment around the user_id_claim in the config reference (yaml) would do the job. It should explain that the default value of that claim will equal the return value of UserInterface::getUserIdentifier() by default or, in case that method is not implemented, the value of the corresponding property (e.g. if user_id_claim is set to some_id, the bundle will try to fill it with the value of $user::$someId via a public accessor).
Note that I would welcome a PR that just removes the user_identity_field from the config reference so if phrasing what I said above feels too complicated, don't worry about it. Thanks!

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

Successfully merging a pull request may close this issue.

6 participants