60 lines
1.5 KiB
Python
60 lines
1.5 KiB
Python
from functools import wraps
|
|
from flask import request, jsonify, Blueprint, Response
|
|
from typing import Callable, Any, Tuple, Dict
|
|
|
|
|
|
auth_blueprint = Blueprint("auth", __name__)
|
|
|
|
|
|
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.
|
|
"""
|
|
return True
|
|
|
|
|
|
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)
|
|
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):
|
|
return (
|
|
jsonify({"message": "Could not verify"}),
|
|
401,
|
|
{"WWW-Authenticate": 'Basic realm="Login Required"'},
|
|
)
|
|
return f(auth.username, auth.password, *args, **kwargs)
|
|
|
|
return decorated
|
|
|
|
|
|
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.
|
|
"""
|
|
return jsonify({"auth": "Success"}) |