MemGPT/letta/server/rest_api/auth_token.py
Sarah Wooders 85faf5f474
chore: migrate package name to letta (#1775)
Co-authored-by: Charles Packer <packercharles@gmail.com>
Co-authored-by: Shubham Naik <shubham.naik10@gmail.com>
Co-authored-by: Shubham Naik <shub@memgpt.ai>
2024-09-23 09:15:18 -07:00

23 lines
774 B
Python

import uuid
from fastapi import Depends, HTTPException
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from letta.server.server import SyncServer
security = HTTPBearer()
def get_current_user(server: SyncServer, password: str, auth: HTTPAuthorizationCredentials = Depends(security)) -> uuid.UUID:
try:
api_key_or_password = auth.credentials
if api_key_or_password == password:
# user is admin so we just return the default uuid
return server.authenticate_user()
user_id = server.api_key_to_user(api_key=api_key_or_password)
return user_id
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=403, detail=f"Authentication error: {e}")