Webseitensuche verbessert UI aufgeräumt
This commit is contained in:
+30
-1
@@ -1,8 +1,10 @@
|
||||
import base64
|
||||
import secrets
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
|
||||
from fastapi import Depends, FastAPI, HTTPException
|
||||
from fastapi.responses import FileResponse
|
||||
from fastapi.responses import FileResponse, Response
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
@@ -44,6 +46,33 @@ static_dir = frontend_dir / "static"
|
||||
app.mount("/static", StaticFiles(directory=static_dir), name="static")
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def require_basic_auth(request, call_next):
|
||||
if not settings.auth_username or not settings.auth_password:
|
||||
return await call_next(request)
|
||||
|
||||
authorization = request.headers.get("authorization", "")
|
||||
scheme, _, credentials = authorization.partition(" ")
|
||||
if scheme.lower() == "basic" and credentials:
|
||||
try:
|
||||
decoded = base64.b64decode(credentials).decode("utf-8")
|
||||
except (ValueError, UnicodeDecodeError):
|
||||
decoded = ""
|
||||
|
||||
username, separator, password = decoded.partition(":")
|
||||
if (
|
||||
separator
|
||||
and secrets.compare_digest(username, settings.auth_username)
|
||||
and secrets.compare_digest(password, settings.auth_password)
|
||||
):
|
||||
return await call_next(request)
|
||||
|
||||
return Response(
|
||||
status_code=401,
|
||||
headers={"WWW-Authenticate": 'Basic realm="eventlens"'},
|
||||
)
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
def startup():
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
Reference in New Issue
Block a user