Files
periscope/backend/services/local_jwt.py
T
micheleandCursor 5a69b380da Add local Pinscope multi-user auth for shared projects.
Self-host email/password accounts enable the existing collaborator
invite flow without Clerk; first admin inherits users/local projects.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-11 14:31:37 +02:00

46 lines
1.1 KiB
Python

"""Pinscope local JWT helpers (HS256)."""
from __future__ import annotations
from datetime import datetime, timedelta, timezone
from typing import Any
import jwt
from backend.config import settings
ALGORITHM = "HS256"
TOKEN_TTL_DAYS = 30
def issue_token(user_id: str, email: str) -> str:
secret = settings.auth_jwt_secret
if not secret:
raise RuntimeError("AUTH_JWT_SECRET is not configured")
now = datetime.now(timezone.utc)
payload = {
"sub": user_id,
"email": email,
"iss": "pinscope-local",
"iat": now,
"exp": now + timedelta(days=TOKEN_TTL_DAYS),
}
return jwt.encode(payload, secret, algorithm=ALGORITHM)
def decode_token(token: str) -> dict[str, Any] | None:
secret = settings.auth_jwt_secret
if not secret:
return None
try:
return jwt.decode(
token,
secret,
algorithms=[ALGORITHM],
issuer="pinscope-local",
options={"verify_aud": False},
leeway=10,
)
except jwt.PyJWTError:
return None