[Bug] User Realm Discovery drops the authority's port, so username/password sign-in fails on non-443 authorities
Maintainers usually reply within 7 days
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 25/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- azure, python
- Domain
- authentication
Research direction
The bug is in msal/authority.py, where the User Realm Discovery URL is built from Authority.instance (hostname only) instead of netloc, and the _domains_without_user_realm_discovery memo is keyed the same way. Read canonicalize() and the user realm lookup, then reproduce with a local HTTPS server on a non-443 port. Done when the userrealm request goes to the authority's port and tests cover a non-default port. Note that PR #990 is already open against this issue.
Written by the indexing model from the issue text.
Description
Describe the bug
acquire_token_by_username_password() ignores the port of an authority on a non-default port when it does User Realm Discovery. With authority="https://localhost:8443/organizations", tenant discovery correctly goes to https://localhost:8443/organizations/v2.0/.well-known/openid-configuration, but the user realm request goes to https://localhost/common/userrealm/{username}?api-version=1.0, which is port 443.
#484 fixed the same thing for tenant discovery, for an Azure CLI custom cloud whose activeDirectory endpoint is https://localhost:3001 (Azure/azure-cli#22553). User Realm Discovery still builds its URL from Authority.instance, which canonicalize() sets to authority.hostname, without the port:
So in that same custom-cloud setup, az login --use-device-code and az login --service-principal work, but az login -u USER -p PASSWORD dials port 443 instead.
To Reproduce
Any OIDC-compliant authority served on a port other than 443 works. Here it is a local Entra-compatible server behind HTTPS on 8443:
import msal
app = msal.PublicClientApplication(
"04b07795-8ddb-461a-bbee-02f9e1bf7b46",
authority="https://localhost:8443/organizations",
instance_discovery=False)
app.acquire_token_by_username_password(
"[email protected]", "password",
scopes=["https://management.core.windows.net//.default"])
Expected behavior
GET https://localhost:8443/common/userrealm/[email protected]?api-version=1.0, then the token request.
What you see instead
The server on 8443 receives only the tenant discovery request. MSAL then sends GET https://localhost/common/userrealm/... to port 443. Depending on the machine, that is refused, hangs (WSL2 with mirrored networking), or reaches whatever else is listening on 443.
The MSAL Python version you are using
1.39.0, and dev at e86a1aa.
Additional context
The fix is to build the URL from the authority's netloc instead of instance. The same applies to the _domains_without_user_realm_discovery memo, so that a 404 from one port doesn't turn off User Realm Discovery for another. instance should stay as it is, because the token cache uses it as the account's environment. I have a PR with tests ready to open.
- Dominant language
- Python
- Stars
- 971
- Forks
- 223
- Avg merge
- 4d 2h
- Merged PRs (30d)
- 3
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from AzureAD/microsoft-authentication-library-for-python
-
needs attention untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
AzureAD/microsoft-authentication-library-for-python#997 ·
Maintainers usually reply within 7 days
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
AzureAD/microsoft-authentication-library-for-python#980 ·
Maintainers usually reply within 7 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 55/100
AzureAD/microsoft-authentication-library-for-python#984 ·
Maintainers usually reply within 7 days
-
[Bug] Support library-agnostic HTTP clients for Service Fabric managed identityPossibly taken @4gust claimed this 20 days ago. OpenAzureSDK bug P1
Difficulty 4/5 3-5 days Newbie friendliness 48/100
AzureAD/microsoft-authentication-library-for-python#965 · 1 comment · 1 assignee ·
Maintainers usually reply within 7 days
-
http_client docstring does not say that timeout, verify and proxies only configure the default sessionPossibly taken @HardMax71 claimed this 84 days ago. Open
Difficulty 1/5 Under an hour Newbie friendliness 25/100
AzureAD/microsoft-authentication-library-for-python#960 ·
Maintainers usually reply within 7 days
All issues in AzureAD/microsoft-authentication-library-for-python
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
plone/plone.observability#114 ·
Maintainers usually reply within 1 day
-
area:docs type:docs
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
jejjohnson/gauss_flows#246 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
openvax/varcode#824 · 1 comment ·
Maintainers usually reply within 1 day
-
netbox status: needs triage type: feature
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
netbox-community/netbox#23868 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
emeryberger/CSrankings#15757 ·
Maintainers usually reply within 9 days