Coverage for downtify/admin_access.py: 100%
12 statements
« prev ^ index » next coverage.py v7.17.4, created at 2026-11-21 22:18 +0000
« prev ^ index » next coverage.py v7.17.4, created at 2026-11-21 22:18 +0000
1"""Revalidate administrator privileges for sensitive mutations."""
3from fastapi import HTTPException, Request
5from . import api
7#: Who may: a signed-in browser, a paired app, or anyone while sign-in
8#: is off (``DOWNTIFY_DISABLE_AUTH``: the request acts as the first
9#: admin). Never a signed URL or a WebSocket ticket.
10_KINDS = {'session', 'device', 'open'}
13def admin_user(request: Request) -> int:
14 principal = api.principal_of(request)
15 if (
16 principal is None
17 or principal.kind not in _KINDS
18 or not principal.is_admin
19 or not principal.user_id
20 ):
21 raise HTTPException(403, detail={'code': 'admin_required'})
22 auth = api.state.auth
23 user = auth.users.get(principal.user_id) if auth else None
24 if user is None or user.get('role') != 'admin':
25 raise HTTPException(403, detail={'code': 'admin_required'})
26 return principal.user_id