netadm/auth/static_env.py

32 lines
1.0 KiB
Python
Raw Normal View History

2024-10-20 09:45:39 +00:00
from fastapi import Depends, HTTPException, status
from fastapi.security import APIKeyHeader
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from config import conf
import logging as log
security = HTTPBasic()
api_key_header = APIKeyHeader(name="X-API-KEY", auto_error=False)
# def verify_token(token: str = Depends(api_key_header)):
# if token != conf.zbx.token:
# log.warning("Invalid token")
# raise HTTPException(
# status_code=status.HTTP_401_UNAUTHORIZED,
# detail="Invalid authentication credentials",
# headers={"WWW-Authenticate": "Bearer"},
# )
def verify_user_pwd(credentials: HTTPBasicCredentials = Depends(security)):
if (
credentials.username != conf.swagger.login
or credentials.password != conf.swagger.pwd
):
log.warning("Invalid credentials")
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid credentials",
headers={"WWW-Authenticate": "Basic"},
)