quivr/backend/auth/api_key_handler.py
Zineb El Bachiri 82bcf38b16
refactor: fix bad smells (#1399)
# Description

Please include a summary of the changes and the related issue. Please
also include relevant motivation and context.

## Checklist before requesting a review

Please delete options that are not relevant.

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented hard-to-understand areas
- [ ] I have ideally added tests that prove my fix is effective or that
my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged

## Screenshots (if appropriate):
2023-10-30 10:18:04 +01:00

47 lines
1.3 KiB
Python

from datetime import datetime
from fastapi import HTTPException
from models.settings import get_supabase_db
from models.user_identity import UserIdentity
from pydantic import DateError
async def verify_api_key(
api_key: str,
) -> bool:
try:
# Use UTC time to avoid timezone issues
current_date = datetime.utcnow().date()
supabase_db = get_supabase_db()
result = supabase_db.get_active_api_key(api_key)
if result.data is not None and len(result.data) > 0:
api_key_creation_date = datetime.strptime(
result.data[0]["creation_time"], "%Y-%m-%dT%H:%M:%S"
).date()
if (api_key_creation_date.month == current_date.month) and (
api_key_creation_date.year == current_date.year
):
return True
return False
except DateError:
return False
async def get_user_from_api_key(
api_key: str,
) -> UserIdentity:
supabase_db = get_supabase_db()
user_id_data = supabase_db.get_user_id_by_api_key(api_key)
if not user_id_data.data:
raise HTTPException(status_code=400, detail="Invalid API key.")
user_id = user_id_data.data[0]["user_id"]
email = supabase_db.get_user_email(user_id)
return UserIdentity(email=email, id=user_id)