feat(auth): principal-resolver chain for session-or-bearer auth (#163) - #168
Merged
Conversation
Design for an extension-point that lets downstream modules (e.g. smpy_gis) plug in a bearer-token / PAT resolver without upstreaming PAT storage.
Step-by-step TDD plan covering type alias, AuthState registry, AuthModule hook, middleware fall-through + 401-for-/api/* branch, integration test, and framework docs.
Deploying simple-module-python with |
| Latest commit: | a00cd18 |
| Status: | ✅ Deploy successful! |
| Preview URL: | https://40a83663.simple-module-python.pages.dev |
| Branch Preview URL: | https://worktree-issue-163-principal.simple-module-python.pages.dev |
- _middleware_support: drop hand-rolled _sign_session, use framework's simple_module_test.forge_session_cookie (eliminates duplication). - integration test: import UserContext from `auth` (the documented public re-export) so the symbol's public-surface contract is exercised by an actual test. - middleware: extract _API_PATH_PREFIX and _UNAUTH_DETAIL constants alongside _LOGIN_REDIRECT — single-source the wire format. - principal-resolvers doc: document that resolvers run on public paths too (telemetry use case), plus add per-request DB-cost caveat with LRU caching guidance for high-traffic deployments. - test docstring: drop drive-by reference to an unrelated fixture bug.
Uh oh!
There was an error while loading. Please reload this page.
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 freeto 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.
Summary
app.state.auth.principal_resolvers— an extension point for plugging in non-cookie auth (PAT bearer tokens, API keys, JWT, etc.). Closessimple_module_auth: expose a principal resolver that handles session cookie OR PAT bearer token #163.users.AuthMiddlewarenow falls throughsession cookie → registered resolvers → unauthenticated. Unauthenticated/api/*returns401 {"detail": "Not authenticated"}; view paths still 302 to/users/loginwith the original URL stashed insession["next"].docs/framework/principal-resolvers.mdwith the contract, ordering rules, and a worked Bearer-token example. Pointer added fromdocs/framework-conventions.md.Notable behaviour change
/api/*route now returns401 {"detail": "Not authenticated"}JSON instead of302 → /users/loginHTML. This is strictly better for scripted callers and frontend XHR handling. Three pre-existing tests inmodules/users/tests/test_api_admin.pythat asserted the old 302 behaviour have been updated to assert the new 401 contract.Test plan
uv run pytest modules/auth/tests/test_resolver_registry.py -vuv run pytest modules/users/tests/test_users_middleware.py modules/users/tests/test_users_middleware_resolvers.py -vuv run pytest tests/test_principal_resolver_integration.py -vmake lintmake doctormake test-py