Skip to content

Commit

Permalink
Add utilities module to support basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
br3ndonland committed Aug 20, 2020
1 parent 8e99b98 commit 81d5a90
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions inboard/app/utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
from secrets import compare_digest

from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBasic, HTTPBasicCredentials


def basic_auth(credentials: HTTPBasicCredentials = Depends(HTTPBasic())) -> str:
correct_username = compare_digest(
credentials.username, str(os.getenv("BASIC_AUTH_USERNAME"))
)
correct_password = compare_digest(
credentials.password, str(os.getenv("BASIC_AUTH_PASSWORD"))
)
if not (correct_username and correct_password):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Basic"},
)
return credentials.username

0 comments on commit 81d5a90

Please sign in to comment.