-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathauth.py
More file actions
20 lines (18 loc) · 781 Bytes
/
auth.py
File metadata and controls
20 lines (18 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from fastapi import FastAPI, Depends, HTTPException, status
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from fastapi.staticfiles import StaticFiles
import secrets
from os import getenv
USERNAME = getenv('USERNAME')
PASSWORD = getenv('PASSWORD')
security = HTTPBasic()
def authenticate(credentials: HTTPBasicCredentials = Depends(security)):
correct_username = secrets.compare_digest(credentials.username, USERNAME)
correct_password = secrets.compare_digest(credentials.password, PASSWORD)
if not (correct_username and correct_password):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized :/",
headers={"WWW-Authenticate": "Basic"},
)
return credentials.username