mirror of
https://github.com/calibrain/shelfmark.git
synced 2026-11-23 18:12:34 +00:00
refactor: move the proxy-auth and API-key middleware into core/auth_middleware.py (#1641)
Row 8 of the plan in Discussion #1536 is the auth middleware. The API key `before_request` hook, the proxy auth hook, the hook that strips the session cookie from API key requests and `_proxy_default_is_admin` move from `main.py` to `core/auth_middleware.py`, registered with `register_auth_middleware(app, resolve_auth_mode=..., get_user_db=...)`. This is the code that decides who a request is, so the first commit adds 41 tests before anything moves. They cover the hook order, the admin group mapping (comma and pipe delimiters, a custom group header, the WSGI fallback), proxy header handling in each auth mode, provisioning once per session, the API key scope (exempt paths, a read only key only on GET `/api/stats`, a wrong key) and the cookie stripping. They pass on current `main`. The second commit is the move. The call is made right after `register_security_headers`, so the `before_request` and `after_request` order is the same as before. The existing tests called `proxy_auth_middleware()` and `api_key_auth_middleware()` on `shelfmark.main` directly, and those names are gone, so 18 call lines in three test files now find the registered hook by name through a small helper. No patch targets changed since the auth mode and user database are still read from `main` at request time. The URL map and hook order are identical, and the full suite, ruff and basedpyright are clean. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Authentication** * API-key authentication supports admin and read-only access, with read-only keys limited to statistics requests. * Reverse-proxy authentication recognizes configured identity and group information, and applies admin access according to the configured role settings. * Requests with invalid credentials, missing proxy identities, or authentication errors receive appropriate error responses. * API-key requests no longer retain session cookies. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
1 parent
384adc4c75
commit
2254e2bf93
6 files changed
+643
-247
No files matched your search
@@ -14,6 +14,11 @@ import pytest
|
||||
from shelfmark.core import api_key
|
||||
|
||||
|
||||
def _hook(main_module, name):
|
||||
"""The registered before_request hook called `name` (it lives in core/auth_middleware.py)."""
|
||||
return next(f for f in main_module.app.before_request_funcs[None] if f.__name__ == name)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def user_db():
|
||||
from shelfmark.core.user_db import UserDB
|
||||
@@ -111,7 +116,7 @@ def _identity_for_key(wired):
|
||||
the middleware in a request context sidesteps that entirely.
|
||||
"""
|
||||
with wired.app.test_request_context("/api/downloads/active", headers=_bearer("s3cret")):
|
||||
assert wired.api_key_auth_middleware() is None
|
||||
assert _hook(wired, "api_key_auth_middleware")() is None
|
||||
from flask import session
|
||||
|
||||
return dict(session)
|
||||
@@ -492,7 +497,7 @@ class TestReadOnlyKeyRequests:
|
||||
def test_never_gets_an_admin_session(self, ro, user_db):
|
||||
user_db.create_user(username="root", role="admin")
|
||||
with ro.app.test_request_context("/api/stats", headers=_bearer("ro-key")):
|
||||
assert ro.api_key_auth_middleware() is None
|
||||
assert _hook(ro, "api_key_auth_middleware")() is None
|
||||
from flask import session
|
||||
|
||||
assert "is_admin" not in session
|
||||
|
||||
Reference in new issue
Block a user