CurrentSuperuser now requires a short-lived (5 min) elevated JWT
claim in addition to the is_superadmin flag, obtained via the new
POST /auth/elevate (no re-authentication — the user is already the
only superadmin in practice; this is a deliberate-confirmation guard
against accidentally triggering a destructive action, not a defense
against a stolen session). Regular login tokens keep working unchanged
for all non-superadmin routes; DELETE /participants/{id} from task 5
now needs a fresh /auth/elevate call, no other code changes required
since CurrentSuperuser's meaning changed underneath it.
create_access_token gained an extra_claims param (backward compatible)
to carry the elevated flag.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
from datetime import datetime, timedelta, timezone
|
|
|
|
import jwt
|
|
from pwdlib import PasswordHash
|
|
|
|
from app.core.config import get_settings
|
|
|
|
_password_hash = PasswordHash.recommended() # argon2id
|
|
ALGORITHM = "HS256"
|
|
|
|
|
|
def hash_password(password: str) -> str:
|
|
return _password_hash.hash(password)
|
|
|
|
|
|
def verify_password(plain: str, hashed: str) -> bool:
|
|
return _password_hash.verify(plain, hashed)
|
|
|
|
|
|
def create_access_token(
|
|
subject: str,
|
|
expires_delta: timedelta | None = None,
|
|
extra_claims: dict | None = None,
|
|
) -> str:
|
|
settings = get_settings()
|
|
expire = datetime.now(timezone.utc) + (
|
|
expires_delta or timedelta(minutes=settings.access_token_expire_minutes)
|
|
)
|
|
payload = {"sub": subject, "exp": expire}
|
|
if extra_claims:
|
|
payload.update(extra_claims)
|
|
return jwt.encode(payload, settings.secret_key, algorithm=ALGORITHM)
|
|
|
|
|
|
def decode_access_token(token: str) -> dict | None:
|
|
settings = get_settings()
|
|
try:
|
|
return jwt.decode(token, settings.secret_key, algorithms=[ALGORITHM])
|
|
except jwt.PyJWTError:
|
|
return None
|