60 lines
1.5 KiB
Python
Raw Permalink Normal View History

from functools import wraps
2025-07-15 15:45:17 +01:00
from flask import request, jsonify, Blueprint, Response
2025-07-15 15:48:38 +01:00
from typing import Callable, Any, Tuple, Dict
2025-07-15 15:45:17 +01:00
auth_blueprint = Blueprint("auth", __name__)
2025-07-15 15:45:17 +01:00
def check_auth(username: str, password: str) -> bool:
"""
This function checks if a username and password are valid.
Currently, it always returns True.
Args:
username: The username to check.
password: The password to check.
Returns:
True if the credentials are valid, False otherwise.
"""
2025-07-15 11:38:59 +01:00
return True
2025-07-15 15:45:17 +01:00
def requires_basic_auth(f: Callable) -> Callable:
"""
A decorator to protect routes with basic authentication.
Args:
f: The function to decorate.
Returns:
The decorated function.
"""
@wraps(f)
2025-07-15 15:45:17 +01:00
def decorated(*args: Any, **kwargs: Any) -> Tuple[Response, int, Dict[str, str]] | Response:
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password):
2025-07-15 15:45:17 +01:00
return (
jsonify({"message": "Could not verify"}),
401,
{"WWW-Authenticate": 'Basic realm="Login Required"'},
)
return f(auth.username, auth.password, *args, **kwargs)
return decorated
2025-07-15 09:28:47 +01:00
2025-07-15 15:45:17 +01:00
def check_login(username: str, password: str) -> Response:
"""
Checks a user's login credentials.
Args:
username: The username to check.
password: The password to check.
Returns:
A Flask JSON response indicating success.
"""
2025-07-15 11:38:59 +01:00
return jsonify({"auth": "Success"})