Problem/Motivation
If registrations are disabled on a site, but a user that exists in the identity portal tries to log in, the module will try to create the user, fail silently (messages will be added in watchdog and messenger) and try to redirect to the afterLogin route.
It will also trigger a LogginEvent, which is wrong as well.
If this route happens to be /user, you will end up in an infinite loop, as it will try to log the user in again. If it's not, then the user will see the error messages in a 403 page, if the "Post login path" route was restricted. If the page is public, then they will just see the error messages.
So, 2 out of 3 outcomes are not desired here.
Watchdog messages are:
- Failed to create user. User registration is disabled. Name: john-doe, email: john-doe@test.com
- Login for user john-doe prevented. Account is blocked. <=== this is wrong, it's just because it is the anonymous user, which is not active.
Proposed resolution
I suggest:
- Checking that registrations are disabled before trying to create a user, and redirect to the front page in that case.
- Checking that the user is authenticated before triggering the LoggingEvent
Remaining tasks
MR. I have the code already, I'm creating it.
Issue fork social_auth-3473324
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 #3
fjgarlin commentedThe changes are ready for review.
Comment #4
heddnCan/should we add a test to https://git.drupalcode.org/project/social_auth/-/blob/4.1.x/tests/src/Fu...?
Comment #5
wellsYes, we should include a test for this one.
Comment #6
fjgarlin commentedI'm not sure I'd know how to begin. We'd need to mock remote data, set an integration and settings in it (like disallow registrations) and then test the workflow. I don't see any test doing something similar yet.
For context, I found this bug integrating the new D10 version of www.drupal.org with the new keycloak system. We want registrations to always go via D7, therefore not allowing registrations on D10.
When working with existing users, everything works well, but when the user does not exist yet in D10, then we get the above watchdog messages and behaviour.
If I get some boiler plate code for the above from any of you that know the module better I could finish it off. In this case, writing the fix seems way easier than writing the test for it.
Comment #7
wellsAck. Good point. This would be difficult to test and I don't want to hold it up on that. Happy to merge it if someone else is able to do some functional review and get it to RTBC. Otherwise, I'll try to do so eventually. Thanks, @fjgarlin.
Comment #8
fjgarlin commentedI actually found another case of infinite loop to "user.login" or a nasty error which is not captured.
If the credentials are wrong, we don't get a token back, so I'm checking for that before going any further. I discovered that the "processCallback" function does not handle any possible error, so I added a differentiation and multiple error codes that can help when debugging.
So, the current MR avoid two cases of infinite redirects to "user.login" and a case for an exception being thrown, and instead it captures the errors and redirects to the homepage instead.
--
Note that the phpcs warnings in the pipeline do not come from this MR. These are already present in the main branch.
Comment #9
wellsThanks for your work on this, @fjgarlin -- I left a question on the MR that I think will ultimately lead to some additional changes needed. Let me know your thoughts.
Comment #10
fjgarlin commentedMaybe you couldn’t see the whole MR code? The changes you were asking about were addressed already and shown in the MR.
Comment #11
fjgarlin commentedI refactored the solution as per the feedback in the MR. It can be reviewed again. The approach is the same, it's about catching the error in the "processCallback" function, and then being able to act on it in the "callback" method to avoid an infinite login loop.
Comment #13
wellsLooks good to go -- thanks!