Spaces:
Running
Running
Download core/auth/stepup.py from Aynkhn/WorkPilot-AI: direct link, hf CLI and curl.
- Browser
- Download file 1.05 kB
-
https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/core/auth/stepup.py
- Command line
-
hf download hf://spaces/Aynkhn/WorkPilot-AI/core/auth/stepup.py
-
curl -L -o stepup.py https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/core/auth/stepup.py
1.05 kB
| """Step-up authentication: a fresh TOTP check that yields a short-lived ``stepup`` token. | |
| High-value approvals require this token in addition to a normal session (Phase 3). | |
| """ | |
| from __future__ import annotations | |
| import uuid | |
| from datetime import timedelta | |
| import pyotp | |
| from core.auth.jwt import TokenError, decode_token, issue_token | |
| STEPUP_TTL = timedelta(minutes=5) | |
| def new_totp_secret() -> str: | |
| return pyotp.random_base32() | |
| def verify_totp(secret: str, code: str) -> bool: | |
| return pyotp.TOTP(secret).verify(code, valid_window=1) | |
| def issue_stepup_token(user_id: uuid.UUID, tenant_id: uuid.UUID) -> str: | |
| return issue_token(user_id, tenant_id, "stepup", ttl=STEPUP_TTL) | |
| def check_stepup_token(token: str | None, user_id: uuid.UUID, tenant_id: uuid.UUID) -> bool: | |
| if not token: | |
| return False | |
| try: | |
| payload = decode_token(token, "stepup") | |
| except TokenError: | |
| return False | |
| return payload.get("sub") == str(user_id) and payload.get("tid") == str(tenant_id) | |