raspap-webgui-mirror/api/auth.py

25 lines
666 B
Python
Raw Permalink Normal View History

2024-02-08 22:24:30 +00:00
import os
from fastapi.security.api_key import APIKeyHeader
from fastapi import Security, HTTPException
from starlette.status import HTTP_403_FORBIDDEN
2024-03-07 17:53:17 +00:00
from dotenv import load_dotenv
load_dotenv()
2024-02-08 22:24:30 +00:00
apikey=os.getenv('RASPAP_API_KEY')
#if env not set, set the api key to "insecure"
if apikey == None:
apikey = "insecure"
print(apikey)
api_key_header = APIKeyHeader(name="access_token", auto_error=False)
async def get_api_key(api_key_header: str = Security(api_key_header)):
if api_key_header ==apikey:
return api_key_header
else:
raise HTTPException(
status_code=HTTP_403_FORBIDDEN, detail="403: Unauthorized"
2024-03-07 17:53:17 +00:00
)