38 lines
1.3 KiB
Python
Raw Normal View History

from functools import wraps
2025-07-15 09:54:01 +01:00
from flask import request, jsonify, Blueprint, make_response
from ktvmanager.lib.get_users import get_users
auth_blueprint = Blueprint("auth", __name__)
def check_auth(username, password):
users = get_users()
2025-07-15 09:54:01 +01:00
for user in users:
if user['userName'] == username and user['password'] == password:
return True
return False
def requires_basic_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
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"'}
2025-07-15 09:28:47 +01:00
return f(auth.username,auth.password, *args, **kwargs)
return decorated
2025-07-15 09:28:47 +01:00
def check_login(username, password):
users = get_users()
2025-07-15 09:54:01 +01:00
for user in users:
if user['userName'] == username and user['password'] == password:
response = make_response(jsonify({"auth": "Success"}))
user_id = str(user.get('id'))
if user_id:
response.set_cookie(
'user',
user_id,
domain='tv-ui.k-world.me.uk',
path='/',
samesite=None
)
return response
return jsonify({"auth": "Fail"})