Add optional permission scopes to API tokens
All checks were successful
CI / backend (push) Successful in 1m19s
CI / naming (push) Successful in 1s
CI / frontend (push) Successful in 7s

A token may carry a scopes list: it then grants only those permissions,
intersected with what the owner holds at use time, with the admin role
bypass suspended and role-gated routes denied - a scoped token from an
admin account is genuinely limited. Scope ceiling enforced at
create/update too (only permissions the owner holds; 400 lists
violations) and the picker only offers what you hold. Token management
itself now requires the new apitokens.create permission (admin by
default, grantable via roles). Unscoped tokens keep the exact prior
act-as-owner behavior; imports need an unscoped admin token.
Migration 7d22.

756 tests pass; live-verified scoped 201/403 matrix.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
cproudlock
2026-07-12 08:58:31 -04:00
parent 688ff6646d
commit 848a8fb34f
13 changed files with 728 additions and 45 deletions

View File

@@ -16,7 +16,7 @@ regular users. See docs/IMPORT-API.md for the operator manual.
from datetime import datetime, timezone
from flask import request
from flask_jwt_extended import verify_jwt_in_request, current_user
from flask_jwt_extended import verify_jwt_in_request, current_user, get_jwt
# Request header a migration client sets to opt a request into import mode.
@@ -48,7 +48,14 @@ def import_mode_active():
return False
verify_jwt_in_request(optional=True)
user = current_user
return bool(user is not None and user.hasrole('admin'))
if user is None or not user.hasrole('admin'):
return False
# A scoped PAT never gets import mode: import mode is an admin-role
# capability and a scoped token suspends the admin bypass. get_jwt is safe
# here - an admin user means a valid JWT was decoded above.
if get_jwt().get('patscopes') is not None:
return False
return True
def parse_import_datetime(value):