Repository navigation
fix: keep authority port in user realm discovery - #990
Open
Chad Elliott (THEtheChad) wants to merge 1 commit into
Open
Chad Elliott (THEtheChad) wants to merge 1 commit into
Chad Elliott (THEtheChad) wants to merge 1 commit into
Conversation
User Realm Discovery built its URL from Authority.instance, the authority's host name without its port. Username/password sign-in against an authority on a non-default port therefore asked port 443 for the user realm. AzureAD#484 fixed the same thing for tenant discovery. Build the URL from the authority's netloc instead, and key the memo of hosts without User Realm Discovery the same way, so that a 404 from one port doesn't turn it off for another. Authority.instance is unchanged: the token cache uses it as the account's environment. Fixes AzureAD#989 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The authority-port preservation change has no active review issues.
No open issues 🎉
What changed in this PR
Fixes #989 by preserving authority ports during User Realm Discovery, extending the earlier tenant-discovery fix in #484 without changing token-cache account environments.
Changes:
- Store the lowercased authority host and port in both initializers and use it for user realm requests.
- Key the discovery-disable cache by host and port so a 404 does not affect other ports.
- Add offline regression tests for non-default ports, unchanged default-port URLs, and cache isolation.
| File | Description |
|---|---|
tests/test_authority.py |
Adds mocked tests for port preservation and discovery-cache isolation. |
msal/authority.py |
Preserves ports in user realm URLs and cache keys while leaving Authority.instance unchanged. |
Review effort: Balanced
Author
|
@microsoft-github-policy-service agree |
Chad Elliott (THEtheChad)
marked this pull request as ready for review
November 13, 2026 19:18
This branch has not been deployed
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.
Fixes #989.
User Realm Discovery built its URL from
Authority.instance, whichcanonicalize()sets to the authority's host name without its port. A username/password sign-in against an authority on a non-default port, such ashttps://localhost:8443/organizations, therefore asked port 443 for the user realm. #484 fixed the same problem for tenant discovery, for Azure CLI custom clouds whoseactiveDirectoryis on another port (Azure/azure-cli#22553). This fixes the one path it didn't cover.Changes
netloc(host plus port, lowercased).user_realm_discovery()buildshttps://{netloc}/common/userrealm/...from it._domains_without_user_realm_discoverymemo is keyed bynetloctoo, so a 404 fromhost:8443doesn't turn User Realm Discovery off forhost:443.Authority.instanceis unchanged. The token cache stores it as the account'senvironment, so changing it would split existing cache entries.For an authority on 443,
netlocis the host name the URL was built from before, so the request doesn't change.Tests
TestUserRealmDiscoveryOnNonDefaultPortruns offline: tenant discovery is patched and the HTTP client is a mock. It checks that:The first and third fail on
devwithout this change.I also checked it end to end. The repro script from #989 was pointed at a local Entra-compatible server on
https://localhost:8443. It hangs on MSAL 1.39.0 because the user realm request goes tolocalhost:443. With this branch it gets a token, and the server logsGET /common/userrealm/alice@contoso.com?api-version=1.0 -> 200on 8443.🤖 Generated with Claude Code