Astra: security review, left-on-pod ledger, chosen scenarios all run, GL navigation and Create Order popup on 26D, health check (ed9256cd4029)
348eedc verified Download coworker/api/auth.py from EnergyVenom/patchwise-agent-studio: direct link, hf CLI and curl.
- Browser
- Download file 2.61 kB
-
https://huggingface.co/spaces/EnergyVenom/patchwise-agent-studio/resolve/main/coworker/api/auth.py
- Command line
-
hf download hf://spaces/EnergyVenom/patchwise-agent-studio/coworker/api/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/EnergyVenom/patchwise-agent-studio/resolve/main/coworker/api/auth.py
2.61 kB
| """Authentication + RBAC dependencies. | |
| - Web app: httpOnly session cookie `fpt_session` + CSRF double-submit (`fpt_csrf` cookie, X-CSRF-Token header) on | |
| every unsafe method. | |
| - Desktop/API clients: `Authorization: Bearer <token>` (POST /api/auth/token). Bearer requests are CSRF-exempt. | |
| Roles: viewer, consultant, qa_approver, admin (coworker/platform/users.PERMISSIONS). Enforced here, not in the UI.""" | |
| from __future__ import annotations | |
| from dataclasses import dataclass | |
| from fastapi import Depends, Request | |
| from ..platform.users import PERMISSIONS, consteq | |
| from .errors import ApiError | |
| SESSION_COOKIE = "fpt_session" | |
| CSRF_COOKIE = "fpt_csrf" | |
| CSRF_HEADER = "x-csrf-token" | |
| UNSAFE = {"POST", "PUT", "PATCH", "DELETE"} | |
| class Principal: | |
| id: int | |
| username: str | |
| role: str | |
| via: str # cookie | bearer | |
| def can(self, perm: str) -> bool: | |
| return self.role in PERMISSIONS.get(perm, ()) | |
| def to_dict(self) -> dict: | |
| return {"id": self.id, "username": self.username, "role": self.role, "via": self.via, | |
| "permissions": sorted(p for p in PERMISSIONS if self.can(p))} | |
| def current_user(request: Request) -> Principal: | |
| svc = request.app.state.svc | |
| authz = request.headers.get("authorization", "") | |
| if authz.lower().startswith("bearer "): | |
| if authz[7:].strip().startswith("pwt_"): # personal access tokens are for the MCP and A2A connectors only | |
| raise ApiError(401, "UNAUTHENTICATED", "personal access tokens work only on /mcp and /a2a (the MCP and A2A connectors)") | |
| got = svc.users.session_user(authz[7:].strip()) | |
| if not got: | |
| raise ApiError(401, "UNAUTHENTICATED", "invalid or expired bearer token") | |
| u, _ = got | |
| p = Principal(u["id"], u["username"], u["role"], "bearer") | |
| else: | |
| tok = request.cookies.get(SESSION_COOKIE) | |
| got = svc.users.session_user(tok) if tok else None | |
| if not got: | |
| raise ApiError(401, "UNAUTHENTICATED", "not signed in") | |
| u, s = got | |
| if request.method in UNSAFE: | |
| hdr = request.headers.get(CSRF_HEADER, "") | |
| if not hdr or not consteq(hdr, s["csrf"]): | |
| raise ApiError(403, "CSRF", "missing or invalid CSRF token") | |
| p = Principal(u["id"], u["username"], u["role"], "cookie") | |
| request.state.user = p | |
| return p | |
| def require(perm: str): | |
| def dep(user: Principal = Depends(current_user)) -> Principal: | |
| if not user.can(perm): | |
| raise ApiError(403, "FORBIDDEN", f"role '{user.role}' lacks permission '{perm}'") | |
| return user | |
| return dep | |