Refactor: Update backend logic
Wabifocus committed
Sep 1, 2025 at 02:14 UTC
603aed49be3cc649462e6fbbca264e54ec731bea
1 file changed
+30
-17
run_ui.py
+30
-17
@@ -6,8 +6,7 @@ import socket
6
import struct
7
from functools import wraps
8
import threading
9
-from flask import Flask, request, Response, session
10
-from flask_basicauth import BasicAuth
9
+from flask import Flask, request, Response, session, redirect, url_for, render_template_string
10
import initialize
11
from python.helpers import files, git, mcp_server, fasta2a_server
12
from python.helpers.files import get_abs_path
@@ -43,7 +42,7 @@ webapp.config.update(
42
lock = threading.Lock()
43
44
# Set up basic authentication for UI and API but not MCP
46
-basic_auth = BasicAuth(webapp)
45
+# basic_auth = BasicAuth(webapp)
46
47
48
def is_loopback_address(address):
@@ -79,7 +78,6 @@ def is_loopback_address(address):
78
return False
79
return True
80
82
-
81
def requires_api_key(f):
82
@wraps(f)
83
async def decorated(*args, **kwargs):
@@ -121,21 +119,17 @@ def requires_auth(f):
119
@wraps(f)
120
async def decorated(*args, **kwargs):
121
user = dotenv.get_dotenv_value("AUTH_LOGIN")
124
- password = dotenv.get_dotenv_value("AUTH_PASSWORD")
125
- if user and password:
126
- auth = request.authorization
127
- if not auth or not (auth.username == user and auth.password == password):
128
- return Response(
129
- "Could not verify your access level for that URL.\n"
130
- "You have to login with proper credentials",
131
- 401,
132
- {"WWW-Authenticate": 'Basic realm="Login Required"'},
133
- )
122
+ # If no auth is configured, just proceed
123
+ if not user:
124
+ return await f(*args, **kwargs)
125
+
126
+ if not session.get('authenticated'):
127
+ return redirect(url_for('login'))
128
+
129
return await f(*args, **kwargs)
130
131
return decorated
132
138
-
133
def csrf_protect(f):
134
@wraps(f)
135
async def decorated(*args, **kwargs):
@@ -149,6 +143,26 @@ def csrf_protect(f):
143
144
return decorated
145
146
+@webapp.route("/login", methods=["GET", "POST"])
147
+async def login():
148
+ error = None
149
+ if request.method == 'POST':
150
+ user = dotenv.get_dotenv_value("AUTH_LOGIN")
151
+ password = dotenv.get_dotenv_value("AUTH_PASSWORD")
152
+
153
+ if request.form['username'] == user and request.form['password'] == password:
154
+ session['authenticated'] = True
155
+ return redirect(url_for('serve_index'))
156
+ else:
157
+ error = 'Invalid Credentials. Please try again.'
158
+
159
+ login_page_content = files.read_file("webui/login.html")
160
+ return render_template_string(login_page_content, error=error)
161
+
162
+@webapp.route("/logout")
163
+async def logout():
164
+ session.pop('authenticated', None)
165
+ return redirect(url_for('login'))
166
167
# handle default address, load index
168
@webapp.route("/", methods=["GET"])
@@ -170,7 +184,6 @@ async def serve_index():
184
)
185
return index
186
173
-
187
def run():
188
PrintStyle().print("Initializing framework...")
189
@@ -267,4 +280,4 @@ def init_a0():
280
if __name__ == "__main__":
281
runtime.initialize()
282
dotenv.load_dotenv()
270
- run()
283
+ run()
\ No newline at end of file