mirror of
https://github.com/QuivrHQ/quivr.git
synced 2024-12-16 01:55:15 +03:00
08e015af6c
# 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): --------- Co-authored-by: Antoine Dewez <44063631+Zewed@users.noreply.github.com>
61 lines
1.8 KiB
Python
61 lines
1.8 KiB
Python
from datetime import datetime
|
|
|
|
from fastapi import HTTPException
|
|
from logger import get_logger
|
|
from modules.api_key.repository.api_key_interface import ApiKeysInterface
|
|
from modules.api_key.repository.api_keys import ApiKeys
|
|
from modules.user.entity.user_identity import UserIdentity
|
|
from modules.user.service.user_service import UserService
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
user_service = UserService()
|
|
|
|
|
|
class ApiKeyService:
|
|
repository: ApiKeysInterface
|
|
|
|
def __init__(self):
|
|
self.repository = ApiKeys()
|
|
|
|
async def verify_api_key(
|
|
self,
|
|
api_key: str,
|
|
) -> bool:
|
|
try:
|
|
# Use UTC time to avoid timezone issues
|
|
current_date = datetime.utcnow().date()
|
|
result = self.repository.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.year == current_date.year:
|
|
return True
|
|
return False
|
|
except Exception as e:
|
|
logger.error(f"Error verifying API key: {e}")
|
|
return False
|
|
|
|
async def get_user_from_api_key(
|
|
self,
|
|
api_key: str,
|
|
) -> UserIdentity:
|
|
user_id_data = self.repository.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"]
|
|
|
|
# TODO: directly UserService instead
|
|
email = user_service.get_user_email_by_user_id(user_id)
|
|
|
|
if email is None:
|
|
raise HTTPException(status_code=400, detail="Invalid API key.")
|
|
|
|
return UserIdentity(email=email, id=user_id)
|