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:
splitsec2 authored and GitHub committed 2026-11-01 18:50:36 -07:00
1 parent 384adc4c75
commit 2254e2bf93
6 files changed
+643 -247

No files matched your search

+6 -1
View File
@@ -10,6 +10,11 @@ import pytest
from werkzeug.security import generate_password_hash
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(scope="module")
def main_module():
"""Import `shelfmark.main` with background startup disabled."""
@@ -309,7 +314,7 @@ class TestProxyProvisioningRole:
):
main_module.session["user_id"] = "bob"
main_module.session["db_user_id"] = bob["id"]
assert main_module.proxy_auth_middleware() is None
assert _hook(main_module, "proxy_auth_middleware")() is None
return main_module.session["is_admin"]
assert run_middleware() is False