fix(auth): Single-User Multiple-Email Issue using CM-Keycloak #7728
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem Statement
Natively, Keycloak handles the case of different emails as different entities entirely, even though they might actually be used by the same user (Coursemology supports multiple emails to be associated with one user). This leads to the problem of when a user logs in using 2 different emails, then in other services (such as Cikgo or Koditsu) those 2 emails will be regarded as belonging to 2 different users, which is wrong. This PR resolves this particular issue
Approach
Previously we setup the user ID inside Keycloak by using an ID registered under
user_emailstable. Because we want to regard all emails from a single user as the same entity under Keycloak, we instead use an ID fromuserstable. In this way, no matter which email is being used by the user, they will all be directed towards one single entity by Keycloak and hence will be regarded correctly as being the same user across platform as well.This approach has a drawback, though, in which should a user has an unconfirmed email being registered, then logging in using any confirmed email, then later on logging in using this unconfirmed one within different session will gives user a successful login (since Keycloak already cached the user's data inside their own DB)
Therefore, to mitigate this issue, we also add the filtering of all unconfirmed email out of the search results everytime Keycloak makes a query to Coursemology DB. This will ensure that user won't be able to login using unconfirmed email and hence only be able to login using confirmed one
Trade-Off
Even though the proposed approach resolves the single-user multiple-email issue, as well as keeping the logging in through unconfirmed email impossible, there is a trade-off in the behaviour of logging in through unconfirmed email.
Previously, if we do that, we will be redirected towards the
Unconfirmed Emailpage, in which there will be a hyperlink to trigger Coursemology to resend the confirmation email to user. Now, doing so will only render the email invalid and hence only the warning of "Invalid username or password" will be generated, and no redirection will happenOther Issues being Resolved
Other than single-user multiple-email issue, I also noticed that there is no validation that occurs while trying to delete the email through controller. By rights, we should not be able to delete a primary email (this has been handled well in our Frontend), but right now we can merely call the API for destroy primary email directly and it will be successful. Therefore, I added the gate-keeping of such in this PR as well