-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #224 from mobiusml/admin_only_endpoints
Enhance API Key Validation and Admin Access Control
- Loading branch information
Showing
9 changed files
with
137 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from typing import Annotated | ||
|
||
from fastapi import Depends, Request | ||
|
||
from aana.configs.settings import settings as aana_settings | ||
from aana.exceptions.api_service import AdminOnlyAccess | ||
|
||
|
||
def check_admin_permissions(request: Request): | ||
"""Check if the user is an admin. | ||
Args: | ||
request (Request): The request object | ||
Raises: | ||
AdminOnlyAccess: If the user is not an admin | ||
""" | ||
if aana_settings.api_service.enabled: | ||
api_key_info = request.state.api_key_info | ||
is_admin = api_key_info.get("is_admin", False) | ||
if not is_admin: | ||
raise AdminOnlyAccess() | ||
|
||
|
||
class AdminCheck: | ||
"""Dependency to check if the user is an admin.""" | ||
|
||
async def __call__(self, request: Request) -> bool: | ||
"""Check if the user is an admin.""" | ||
check_admin_permissions(request) | ||
return True | ||
|
||
|
||
AdminRequired = Annotated[bool, Depends(AdminCheck())] | ||
""" Annotation to check if the user is an admin. If not, it will raise an exception. """ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters