Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/auth/token.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
"""JWT issuance / verification."""
import jwt
import os
from datetime import datetime, timedelta, timezone

JWT_SECRET = "billing-api-jwt-supersecret-2024"
import jwt


def _get_jwt_secret() -> str:
"""Get JWT secret from environment. Raises if not configured."""
secret = os.environ.get("JWT_SECRET")
if not secret:
raise RuntimeError(
"JWT_SECRET environment variable is not set. "
"Set it to a secure random string in production."
)
return secret
JWT_ALGO = "HS256"


Expand All @@ -14,8 +25,8 @@ def issue_token(user_id: int, email: str, is_admin: bool = False) -> str:
"iat": datetime.now(timezone.utc),
"exp": datetime.now(timezone.utc) + timedelta(hours=12),
}
return jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGO)
return jwt.encode(payload, _get_jwt_secret(), algorithm=JWT_ALGO)


def verify_token(token: str) -> dict:
return jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGO])
return jwt.decode(token, _get_jwt_secret(), algorithms=[JWT_ALGO])