Skip to content

Add EntraAppClient for Microsoft Entra ID (Azure AD) oauth2 - #920

Open
urskog84 wants to merge 2 commits into
AnswerDotAI:mainfrom
urskog84:entra-oauth-client
Open

urskog84 wants to merge 2 commits into
AnswerDotAI:mainfrom
urskog84:entra-oauth-client

Conversation

@urskog84

@urskog84 urskog84 commented Aug 16, 2026 •

Copy link
Copy Markdown

Closes #718.

Adds EntraAppClient (also exported as AzureAppClient) for Microsoft Entra ID, formerly Azure AD.

from fasthtml.oauth import EntraAppClient, OAuth

client = EntraAppClient(os.getenv("AUTH_CLIENT_ID"),
                        os.getenv("AUTH_CLIENT_SECRET"),
                        os.getenv("AUTH_TENANT_ID"))

class Auth(OAuth):
    def get_auth(self, info, ident, session, state):
        if info.tid == MY_TENANT: return RedirectResponse('/', status_code=303)

Why it isn't just another endpoint triple

  1. Entra normalises the scopes it grants. Requesting https://graph.microsoft.com/User.Read, the form Microsoft's own samples use, comes back as User.Read, and oauthlib then raises Warning: Scope has changed at the token exchange. parse_request_body_response skips that check and restores the requested scope afterwards.
  2. Directory identity isn't in Graph's userinfo. oid (the user's immutable ID) and tid (the tenant they signed in from) are only in the id_token, so get_info/get_info_async merge its claims into the userinfo. The payload is decoded with stdlib base64 and no signature check, since the token comes straight from the token endpoint over TLS.

API choices worth a second opinion

  • tenant_id is required rather than defaulting to 'common', so the default case is the one where Entra itself rejects other directories. 'organizations' and 'common' still work; the docs say those need a tid check in get_auth.
  • ident stays sub, as for the other clients. Set client.id_key = 'oid' for the directory-wide ID.
  • logout_link() is included because /logout alone leaves the user signed in to Microsoft.
  • The merged info also carries the id_token's metadata claims (aud, iss, exp, ...). Happy to filter those out if you'd rather keep info lean.

Testing

  • Run end to end against a real single-tenant Entra app registration: login, token exchange, Graph userinfo, logout. That run confirmed the scope behaviour in point 1.
  • tests/test_oauth.py covers the same paths offline against mocked endpoints: authorize URL and pass-through params, token exchange payload, scope restoration, sync and async retr_info, id_key override, both logout_link forms.

nbs/api/08_oauth.ipynb is the edited source; fasthtml/oauth.py and _modidx.py come from nbdev-export. Rebased on current main.

🤖 Generated with Claude Code

@urskog84
urskog84 force-pushed the entra-oauth-client branch from 8bea150 to f1cf9c8 Compare August 16, 2026 15:17
urskog84 and others added 2 commits November 30, 2026 21:34
Closes AnswerDotAI#718.

Entra needs two things beyond the usual client boilerplate. It normalises
the scopes it grants, so they don't always match what was requested:
asking for `https://graph.microsoft.com/User.Read`, the form Microsoft's
samples use, comes back as plain `User.Read`, and oauthlib raises
`Warning: Scope has changed` on the difference. `parse_request_body_response`
skips that check, then restores the requested scope. And directory-level
identity lives in the `id_token` rather than in Graph's userinfo, so
`get_info` merges the two, making `oid` (immutable user id) and `tid`
(tenant) available to `get_auth`.

`tenant_id` is required rather than defaulting to 'common', so that the
single-tenant case - where Entra itself turns away other directories -
is what you get unless you ask for more.

Verified against a live single-tenant app registration: login, token
exchange, Graph userinfo and logout.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
@urskog84

Copy link
Copy Markdown
Author

Rebased on current main, so the conflicts are gone, and added the offline tests as tests/test_oauth.py. I also trimmed the description.

CI is waiting for workflow approval since this comes from a fork. Could a maintainer approve the run and take a look when there's time? Happy to adjust the API or cut scope if that makes it easier to review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] OAuth Azure AD/Entra

1 participant