24 lines
No EOL
878 B
Python
24 lines
No EOL
878 B
Python
import base64
|
|
import os
|
|
|
|
import falcon
|
|
from falcon import Request, Response
|
|
|
|
|
|
class AuthMiddleware:
|
|
def validate_credentials(self, auth_header_value: str) -> bool:
|
|
auth_token = auth_header_value.split(' ')[1]
|
|
username, password = base64.b64decode(auth_token).decode().split(':')
|
|
return username == (os.environ.get('AUTH_USERNAME') or 'admin') and password == (os.environ.get('AUTH_PASSWORD') or 'admin')
|
|
|
|
def set_response_to_auth(self, resp: Response):
|
|
resp.complete = True
|
|
resp.status = 401
|
|
resp.body = "Not authenticated."
|
|
resp.set_header("WWW-Authenticate", "Basic realm='Login required'")
|
|
|
|
def process_request(self, request: Request, resp: Response):
|
|
if request.auth is not None and self.validate_credentials(request.auth):
|
|
pass
|
|
else:
|
|
self.set_response_to_auth(resp) |