fix: enhance Entra identity linking and error handling in authentication - #58
Draft
Dinssa wants to merge 1 commit into
Draft
fix: enhance Entra identity linking and error handling in authentication#58Dinssa wants to merge 1 commit into
Dinssa wants to merge 1 commit into
Conversation
katstevens
reviewed
Jul 28, 2026
| return HttpResponseRedirect(next_url) | ||
|
|
||
| return HttpResponseForbidden("You are not allowed to access this application.") | ||
| return render(request, "403.html", status=HTTPStatus.FORBIDDEN) |
Contributor
There was a problem hiding this comment.
Doesn't HttpResponseForbidden default to using the 403.html template anyway?
Dinssa
marked this pull request as draft
July 30, 2026 12:33
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
SSO sign-in returned a 500 for some users, with an unhandled IntegrityError.
Authentication.authenticateidentified users solely by(entra_tid, entra_oid). Migration 0025 added those columns in June 2025 as nullable with no backfill, so every account created by the Entra flow before that date holds nulls in both while still owning its email and username. Those accounts miss the lookup permanently, so the code treated each sign-in as a new user and tried to insert a row whose username was already taken. Anoidchanging (account recreated in the tenant, guest converted to member) causes the same failure.Changes:
_find_usernow tries three lookups in priority order: Entra identity, then email, then username (both case-insensitive). The first hit wins.entra_oid/entra_tidonto that account, so it is correctly identified from then on and matches on the identity lookup next time. Each affected account costs one extra pair of queries exactly once, which is why no backfill migration is needed._is_tenant_allowedstill runs first, so email matching only ever happens against an identity Microsoft has verified and an approved tenant.IntegrityErroris caught, reported to Sentry as a warning with theoid/tidin context, and the request is denied rather than 500ing. The message is static so the issue groups into one countable alert.templates/403.htmlinstead of a bare text response. It links Knowledge Hub and the support desk, and says nothing about an account existing, so it does not leak whether an email is registered.Ticket link
Checklist
Additional notes
Screenshots