Skip to content

DO NOT REVIEW: OAuthServer so apps such as Claude can sign in to a FastHTML app - #961

Draft
erikgaas wants to merge 3 commits into
mainfrom
erikgaas/oauth-server
Draft

erikgaas wants to merge 3 commits into
mainfrom
erikgaas/oauth-server

Conversation

@erikgaas

@erikgaas erikgaas commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

DO NOT REVIEW

What this does (written by Claude)

fasthtml.oauth_server, a new module from nbs/api/08a_oauth_server.ipynb, adds OAuthServer: an OAuth 2.1 authorization server that lets apps such as Claude act as a FastHTML app's signed-in users at a protected route, usually an MCP endpoint at /mcp. It follows the MCP authorization spec. It serves the discovery documents (RFC 9728 and RFC 8414), dynamic registration (RFC 7591, up to 5 https or loopback return addresses), an Allow page that requires PKCE S256, checks a session nonce and sends X-Frame-Options: DENY, and a token endpoint. The app passes current_user(session), and optionally its own allow_page and a token_user(uid, issued_at) hook that can refuse tokens, for example after a sign-out from all devices. Its protected route calls srv.user(req) and returns srv.unauthorized(req) when that is None, and srv.skip lists the paths for the sign-in Beforeware to skip.

By default the server stores nothing. It signs client ids, one-time codes and access tokens with the app's secret_key through itsdangerous, with a salt per kind, the way FastHTML signs session cookies. A client id is the signed registration, so registration needs no cap. Access tokens last 30 days. There are no refresh tokens and no /oauth/revoke, and apps and disconnect raise NotImplementedError. A code lasts 60 seconds and isn't refused on a second redemption within that time, which RFC 6749 forbids. PKCE still refuses anyone without the verifier. Changing secret_key cancels every registration and token.

Passing a store turns on grants, one row for each app a user connects. A store has five methods: add_grant (insert if absent), grant, advance (move the refresh counter from n to n+1), revoke and grants(uid). FastliteStore is one fastlite table, and fastlite stays a dev-only dependency because the module never imports it. Redeeming a code creates a grant whose id comes from the code, so a second redemption is refused and revokes the grant. Refresh tokens carry the grant's counter and rotate through advance, so a replayed one revokes its grant (RFC 9700 requires rotation for public clients). With a store, access tokens last an hour, user checks the grant on each request so Disconnect takes effect at once, a new grant replaces the user's older grant to the same app, and /oauth/revoke implements RFC 7009. OAuth in 08_oauth now redirects to a same-site path saved in session['next'] after sign-in, in place of get_auth's response. The Allow page saves its own path there before sending a signed-out visitor to login_path.

The server is extracted from two copies that had drifted apart: seedlist's app/oauth.py, which runs the seedlist claude.ai connector, and the one in AnswerDotAI/solve-lp#538. Both used three tables and an 11-method store. The notebook walks a full sign-in with and without a store through Starlette's TestClient, plus sign-in through OAuth with a stub provider, and nbdev-test passes for the whole repo locally. Nothing has been tried with real claude.ai or Claude Code clients yet. The server has its own notebook because 08_oauth is skip_exec, so its examples never run in CI. Unrelated and unchanged: redir_url in fasthtml/oauth.py ignores its scheme argument and always recomputes it from the host.

🤖 Generated with Claude Code

https://claude.ai/code/session_012ywYrYMjp5MDq7g7oV5dmS

erikgaas and others added 3 commits November 17, 2026 18:46
`fasthtml.oauth_server`, from nbs/api/08a_oauth_server.ipynb, adds `OAuthServer`. It lets apps such as Claude act as a FastHTML app's signed-in users at a protected route such as `/mcp`, following the MCP authorization spec. It serves the discovery documents, app registration, an Allow page, the token endpoint and revocation. It keeps registered apps, codes and tokens in a store object, and `FastliteStore` is a store on a fastlite database the app passes in.

After sign-in, `OAuth` now redirects to a same-site path saved in `session['next']`. The Allow page saves its own path there before sending a signed-out visitor to sign in.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012ywYrYMjp5MDq7g7oV5dmS
With no store, OAuthServer signs client ids, one-time codes and access tokens with the app's `secret_key` and stores nothing. Registration then needs no daily cap. Access tokens last 30 days, there are no refresh tokens or `/oauth/revoke`, and `apps` and `disconnect` raise NotImplementedError. A code can be redeemed again within its 60 seconds, and PKCE still refuses anyone without the verifier.

Passing a store turns on grants, one row for each app a user connects. Redeeming a code creates a grant whose id comes from the code, so a second redemption is refused and revokes the grant. Refresh tokens carry the grant's counter, and `advance` rotates them, so a replayed one revokes its grant. The store needs five methods, and `FastliteStore` is one table, down from 11 methods and three tables.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012ywYrYMjp5MDq7g7oV5dmS
The 18 exported cells now carry {"nbdev": {"export": "true"}} instead of a `#| export` line, as the newest cells in 00_core and 01_components do. The exported module is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012ywYrYMjp5MDq7g7oV5dmS

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.

1 participant