supervisord
This commit is contained in:
@@ -1,33 +1,53 @@
|
|||||||
from functools import wraps
|
import os
|
||||||
|
import functools
|
||||||
from flask import request, Response
|
from flask import request, Response
|
||||||
import configparser
|
import configparser
|
||||||
|
|
||||||
# Load basic auth credentials
|
# Docker paths
|
||||||
|
CONFIG_DIR = '/app/config'
|
||||||
|
AUTH_CFG = os.path.join(CONFIG_DIR, 'auth', 'auth.cfg')
|
||||||
|
|
||||||
|
# Ensure config directory exists
|
||||||
|
os.makedirs(os.path.dirname(AUTH_CFG), exist_ok=True)
|
||||||
|
|
||||||
|
# Load auth credentials with fallback defaults
|
||||||
|
BASIC_AUTH_USERNAME = "admin"
|
||||||
|
BASIC_AUTH_PASSWORD = "admin"
|
||||||
|
|
||||||
|
try:
|
||||||
|
if os.path.exists(AUTH_CFG):
|
||||||
auth_config = configparser.ConfigParser()
|
auth_config = configparser.ConfigParser()
|
||||||
auth_config.read('auth/auth.cfg')
|
auth_config.read(AUTH_CFG)
|
||||||
BASIC_AUTH_USERNAME = auth_config.get('auth', 'username')
|
if auth_config.has_section('auth'):
|
||||||
BASIC_AUTH_PASSWORD = auth_config.get('auth', 'password')
|
BASIC_AUTH_USERNAME = auth_config.get('auth', 'username', fallback='admin')
|
||||||
|
BASIC_AUTH_PASSWORD = auth_config.get('auth', 'password', fallback='admin')
|
||||||
|
print(f"[AUTH] Loaded credentials from {AUTH_CFG}", flush=True)
|
||||||
|
else:
|
||||||
|
print(f"[AUTH] No [auth] section in {AUTH_CFG}, using defaults", flush=True)
|
||||||
|
else:
|
||||||
|
print(f"[AUTH] {AUTH_CFG} not found, using defaults", flush=True)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[AUTH] Error loading config: {e}, using defaults", flush=True)
|
||||||
|
|
||||||
def check_auth(username, password):
|
def check_auth(username, password):
|
||||||
return username == BASIC_AUTH_USERNAME and password == BASIC_AUTH_PASSWORD
|
return username == BASIC_AUTH_USERNAME and password == BASIC_AUTH_PASSWORD
|
||||||
|
|
||||||
def authenticate():
|
def authenticate():
|
||||||
return Response(
|
return Response(
|
||||||
'Authentication required.', 401,
|
'Could not verify your access level for that URL.\n'
|
||||||
|
'You have to login with proper credentials',
|
||||||
|
401,
|
||||||
{'WWW-Authenticate': 'Basic realm="Login Required"'}
|
{'WWW-Authenticate': 'Basic realm="Login Required"'}
|
||||||
)
|
)
|
||||||
|
|
||||||
def requires_auth(f):
|
def requires_auth(f):
|
||||||
@wraps(f)
|
@functools.wraps(f)
|
||||||
def decorated(*args, **kwargs):
|
def decorated_function(*args, **kwargs):
|
||||||
auth = request.authorization
|
auth = request.authorization
|
||||||
if not auth or not check_auth(auth.username, auth.password):
|
if not auth or not check_auth(auth.username, auth.password):
|
||||||
return authenticate()
|
return authenticate()
|
||||||
return f(*args, **kwargs)
|
return f(*args, **kwargs)
|
||||||
return decorated
|
return decorated_function
|
||||||
|
|
||||||
def setup_auth(app):
|
def setup_auth(app):
|
||||||
"""Setup authentication for the Flask app"""
|
|
||||||
# This function can be used to register auth-related configurations
|
|
||||||
# if needed in the future
|
|
||||||
pass
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user