29 lines
761 B
Python
29 lines
761 B
Python
import os
|
|
from functools import wraps
|
|
from flask import request, Response
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
ADMIN_USERNAME = os.getenv("ADMIN_USERNAME")
|
|
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD")
|
|
|
|
def check_auth(username, password):
|
|
return username == ADMIN_USERNAME and password == ADMIN_PASSWORD
|
|
|
|
def authenticate():
|
|
return Response(
|
|
"Du måste logga in för att se adminpanelen.\n",
|
|
401,
|
|
{"WWW-Authenticate": 'Basic realm="Admin Area"'}
|
|
)
|
|
|
|
def requires_auth(f):
|
|
@wraps(f)
|
|
def decorated(*args, **kwargs):
|
|
auth = request.authorization
|
|
if not auth or not check_auth(auth.username, auth.password):
|
|
return authenticate()
|
|
return f(*args, **kwargs)
|
|
return decorated
|