-
Notifications
You must be signed in to change notification settings - Fork 484
Open
Labels
good first issueGood for newcomersGood for newcomers
Description
Issue Title: Improve Error Handling for API Key Validation in api_key_service.py
Description:
The validate_api_key function in api_key_service.py currently does not provide clear error messages when API key validation fails. To enhance user experience and debugging, we need to implement more informative error handling that specifies the nature of the validation failure.
Proposed Changes:
-
Enhance Error Messaging:
- Update the
validate_api_keyfunction to return specific error messages for different failure scenarios, such as:- When the API key format is incorrect (e.g., if it does not start with the expected prefix).
- When the hashed API key does not match any records in the database.
- Update the
-
Code Implementation:
- Modify the return statements in the function to provide structured error messages instead of simply returning
None.
- Modify the return statements in the function to provide structured error messages instead of simply returning
-
Testing:
- Add unit tests to ensure that various invalid inputs trigger the appropriate error messages.
Current Code Snippet:
@staticmethod async def validate_api_key(api_key: str, db: Session) -> Optional[dict]:
"""Validate an API key and return user info if valid."""
if not api_key.startswith(APIKeyService.SECRET_PREFIX): return None # <-- Update this line to provide an error message
hashed_key = APIKeyService.hash_api_key(api_key)
result = (
db.query(UserPreferences, User.email)
.join(User, UserPreferences.user_id == User.uid)
.filter(text("preferences->>'api_key_hash' = :hashed_key"))
.params(hashed_key=hashed_key)
.first()
)
if not result:
return None # <-- Update this line to provide an error message
Metadata
Metadata
Assignees
Labels
good first issueGood for newcomersGood for newcomers