Problem/Motivation
Drupal Core define username max length to 60 characters.
When setting the username in OpenIdConnect::generateUsername(), there is no check on the length of the retrieved value.
I propose to force a truncate to avoid errors when inserting into the database.
Proposed resolution
The original problem is outdated. Since 3.0.0-alpha7, the createUser() method logs an error message and throws a \RuntimeException if the user name or the email address is invalid. The code that calls createUser() catches the exception, shows an error message to the user, and cancels the login attempt.
For this issue, add test coverage for the createUser() method.
| Comment | File | Size | Author |
|---|---|---|---|
| #14 | 3252021-14.patch | 2.83 KB | tuutti |
| #11 | 3252021-11.patch | 1.14 KB | ajaypratapsingh |
| #8 | data_too_long_for_column_name- 3252021-8.patch | 904 bytes | s3b0un3t |
Issue fork openid_connect-3252021
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
s3b0un3tComment #3
s3b0un3tComment #4
s3b0un3tComment #5
jcnventuraSeems like a bug here. You need to shorten to max and 2/3 chars less and then append the _i to the username. Instead, you are effectively removing the _i suffix for the case where name length is max.
Also, don't just do "- 2". Instead, do -(number of digits + 1).
Comment #6
s3b0un3tYou are right ! I adjusted the patch accordingly.
Comment #7
jcnventuraI think the bug is still there.. Imagine there's 1000 'o' in the example below
If original is "really_looooong_string", you're then making it "really_looooong_string_1", and truncating it to be "really_looooo". You first need to truncate it to max - strlen - 1, and only then can you add the suffix.
Also, can you move
$original = $nameto the line before the for cycle. It's functionally the same, but I don't really think it is readable to instanciate multiple variables in the for like that.Comment #8
s3b0un3tIndeed ... thoughtlessness from me.
It's time to take holiday!
Comment #9
sanduhrsIt's not only length, but also other characters.
Could we port this from 7.x and also check whether it's still the same in D9/10?
#2872912: Username too long during creation
Comment #10
siliconmeadow commentedComment #11
ajaypratapsingh commentedAfter suggestions created a patch please review
Comment #13
tuutti commentedComment #14
tuutti commentedI've ported the Drupal 7 version of this as suggested in #9. We should probably add a proper test coverage for this tho.
Comment #15
bkosborneWouldn't it be safer to refuse to log the user in rather than manipulate the username like this?
Comment #16
j-leeWe did that. But I think it's a decision that has to be made individually for each project/website. This module must prevent Drupal errors, but should add a hook to change the behavior.
Comment #17
j-leePatch from #14 looks ok for me. But it should add a hook to change the behavior.
Comment #18
steinmb commentedTried to take a step back here as it felt like we tried to invent something that might already be invented, and looked at what core, example: class UserNameValidator and its use of https://symfony.com/doc/current/components/validator.html - Would what be more standardized way to going. I think the migration modules use it as it generate user names during a user migration from example uknown systems that is not Drupal.
Comment #19
tuutti commentedWhat behaviour?
The current implementation attempts to use user's
preferred_name, so you can already alter the username by modifying the$userinfo['preferred_username']inhook_openid_connect_userinfo_alter.The
UserNameValidatoronly validates the username, but doesn't allow us to generate a unique name.I couldn't find any username generation logic or migration examples with unique usernames besides this one: https://git.drupalcode.org/project/drupal/-/blob/main/core/modules/user/..., used in https://git.drupalcode.org/project/drupal/-/blob/main/core/modules/user/..., which isn't very helpful for this case.
Comment #20
j-leeI meant that the username can be changed from ODIC to Drupal.
With hook_openid_connect_userinfo_alter, that's already done.
Comment #23
pfrillingThe entity validation that was introduced in alpha7 is now handling the username length. I added a kernel test in MR 195 validating that the expected exception is thrown when a username is too long.
Comment #26
benjifisher@pfrilling:
I guess the original report is outdated as of 3.0.0-alpha7 (as you said in Comment #23). There is some discussion on this issue about what to do when the username (or email) does not validate, but the current behavior is to show an error message, log an error with more detail, and not log in the user.
I agree with that decision (also suggested in Comment #15).
The current MR does not change the behavior, but it adds a Kernel test for the validation added in 3.0.0-alpha7. I am updating the issue summary with a "Proposed resolution" section.
Instead of 6 separate test methods, I would prefer
If you can think of a way to combine (2) and (3), then that would be even better.
If you do not want to rewrite the tests, then I will not insist.
It looks as though there is a merge conflict in
.cspell-project-words.txt, so NW for that. If you always add new words to the end of the file, then you pretty much guarantee that there will be add/add conflicts. If you keep the file sorted, then you have a chance of avoiding that. But an even better strategy is to avoid cspell exceptions: I think you can keep cspell happy by usingfirst_userorfirstUserinstead offirstuser, for example.Comment #27
benjifisherComment #28
pfrillingThanks @benjifisher. I updated the test to utilize data providers instead of the original individual methods.
Comment #29
benjifisher@pfrilling:
Thanks for the updates. LGTM
Comment #30
pfrillingI rebased and added to the merge train.