2022-08-25 17:03:56 +00:00
|
|
|
from fastapi import Depends, HTTPException, status
|
|
|
|
from fastapi.security import APIKeyHeader
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
2022-12-26 15:20:58 +00:00
|
|
|
from selfprivacy_api.actions.api_tokens import is_token_valid
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TokenHeader(BaseModel):
|
|
|
|
token: str
|
|
|
|
|
|
|
|
|
|
|
|
async def get_token_header(
|
|
|
|
token: str = Depends(APIKeyHeader(name="Authorization", auto_error=False))
|
|
|
|
) -> TokenHeader:
|
|
|
|
if token is None:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED, detail="Token not provided"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
token = token.replace("Bearer ", "")
|
|
|
|
if not is_token_valid(token):
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token"
|
|
|
|
)
|
|
|
|
return TokenHeader(token=token)
|
|
|
|
|
|
|
|
|
|
|
|
def get_api_version() -> str:
|
|
|
|
"""Get API version"""
|
2023-10-09 09:45:22 +00:00
|
|
|
return "2.4.2"
|