2023-05-24 23:21:22 +03:00
|
|
|
import os
|
2023-06-17 00:36:53 +03:00
|
|
|
from typing import Optional
|
2023-05-24 23:21:22 +03:00
|
|
|
|
2023-06-17 00:36:53 +03:00
|
|
|
from fastapi import Depends, HTTPException, Request
|
|
|
|
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
2023-11-14 16:31:02 +03:00
|
|
|
from middlewares.auth.jwt_token_handler import decode_access_token, verify_token
|
2023-12-01 00:29:28 +03:00
|
|
|
from modules.api_key.service.api_key_service import ApiKeyService
|
2023-11-15 15:17:51 +03:00
|
|
|
from modules.user.entity.user_identity import UserIdentity
|
2023-06-17 00:36:53 +03:00
|
|
|
|
2023-12-01 00:29:28 +03:00
|
|
|
api_key_service = ApiKeyService()
|
|
|
|
|
2023-05-24 23:21:22 +03:00
|
|
|
|
2023-06-14 22:21:13 +03:00
|
|
|
class AuthBearer(HTTPBearer):
|
2023-05-24 23:21:22 +03:00
|
|
|
def __init__(self, auto_error: bool = True):
|
|
|
|
super().__init__(auto_error=auto_error)
|
|
|
|
|
2023-06-23 11:36:55 +03:00
|
|
|
async def __call__(
|
|
|
|
self,
|
|
|
|
request: Request,
|
|
|
|
):
|
2023-06-20 10:54:23 +03:00
|
|
|
credentials: Optional[HTTPAuthorizationCredentials] = await super().__call__(
|
|
|
|
request
|
|
|
|
)
|
2023-06-14 22:21:13 +03:00
|
|
|
self.check_scheme(credentials)
|
2023-07-10 15:27:49 +03:00
|
|
|
token = credentials.credentials # pyright: ignore reportPrivateUsage=none
|
2023-06-23 11:36:55 +03:00
|
|
|
return await self.authenticate(
|
|
|
|
token,
|
|
|
|
)
|
2023-06-14 22:21:13 +03:00
|
|
|
|
|
|
|
def check_scheme(self, credentials):
|
2023-06-23 11:36:55 +03:00
|
|
|
if credentials and credentials.scheme != "Bearer":
|
|
|
|
raise HTTPException(status_code=401, detail="Token must be Bearer")
|
2023-06-14 22:21:13 +03:00
|
|
|
elif not credentials:
|
2023-06-23 11:36:55 +03:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=403, detail="Authentication credentials missing"
|
|
|
|
)
|
2023-05-24 23:21:22 +03:00
|
|
|
|
2023-06-23 11:36:55 +03:00
|
|
|
async def authenticate(
|
|
|
|
self,
|
|
|
|
token: str,
|
2023-08-21 15:05:13 +03:00
|
|
|
) -> UserIdentity:
|
2023-06-14 22:21:13 +03:00
|
|
|
if os.environ.get("AUTHENTICATE") == "false":
|
|
|
|
return self.get_test_user()
|
|
|
|
elif verify_token(token):
|
|
|
|
return decode_access_token(token)
|
2023-12-01 00:29:28 +03:00
|
|
|
elif await api_key_service.verify_api_key(
|
2023-06-23 11:36:55 +03:00
|
|
|
token,
|
|
|
|
):
|
2023-12-01 00:29:28 +03:00
|
|
|
return await api_key_service.get_user_from_api_key(
|
2023-06-23 11:36:55 +03:00
|
|
|
token,
|
2023-06-20 10:54:23 +03:00
|
|
|
)
|
2023-06-23 11:36:55 +03:00
|
|
|
else:
|
|
|
|
raise HTTPException(status_code=401, detail="Invalid token or api key.")
|
2023-06-12 18:58:05 +03:00
|
|
|
|
2023-08-21 15:05:13 +03:00
|
|
|
def get_test_user(self) -> UserIdentity:
|
|
|
|
return UserIdentity(
|
2023-07-10 15:27:49 +03:00
|
|
|
email="test@example.com", id="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" # type: ignore
|
2023-07-04 18:56:54 +03:00
|
|
|
) # replace with test user information
|
2023-06-20 10:54:23 +03:00
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
|
2023-08-21 15:05:13 +03:00
|
|
|
def get_current_user(user: UserIdentity = Depends(AuthBearer())) -> UserIdentity:
|
2023-07-04 18:56:54 +03:00
|
|
|
return user
|