Model/backend/app/local/router.py
Jun-te Kim 4f45eeb3e9 save
2026-05-07 15:55:44 +00:00

52 lines
1.4 KiB
Python

from fastapi import APIRouter, HTTPException, status
from jose import jwt, jwe
import json
import datetime
from backend.app.config import get_settings
from backend.app.dependencies import get_derived_encryption_key
router = APIRouter(
prefix="/local",
tags=["local"],
)
def create_dummy_token(secret: str) -> str:
"""
Create a JWE token using NextAuth.js encryption method
Arguments:
sub -- The subject or identifier for who the token is for (usually a user id)
secret -- The secret key to encrypt the token. Should be the same as the key used in NextAuth.js
exp -- Optional expiry time for the token. If not provided, token does not expire
Returns:
A string containing the JWE token
"""
claims = {
"dbId": "known_id",
}
token = jwe.encrypt(
json.dumps(claims),
get_derived_encryption_key(secret),
algorithm="dir",
encryption="A256GCM",
)
return token
@router.get("/")
async def dummy_token():
return {"hello": "world"}
@router.get("/dummy-token")
async def dummy_token():
settings = get_settings()
if settings.ENVIRONMENT != "local":
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Dummy token can only be generated in local environment",
)
return {"dummy_token": create_dummy_token(settings.SECRET_KEY)}