forked from ah-kun/ComfyUI-OTP-Auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
330 lines (285 loc) · 11.7 KB
/
Copy path__init__.py
File metadata and controls
330 lines (285 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import os
import pyotp
import configparser
import string
import random
import ipaddress
import time
from collections import defaultdict
from aiohttp import web
from server import PromptServer
# ==========================================
# Config Handling
# ==========================================
CONFIG_FILE = os.path.join(os.path.dirname(__file__), 'config.ini')
def load_config():
config = configparser.ConfigParser()
if os.path.exists(CONFIG_FILE):
config.read(CONFIG_FILE)
if 'AUTH' not in config:
config['AUTH'] = {}
auth_config = config['AUTH']
save_needed = False
# 1. SECRET_KEY
if not auth_config.get('SECRET_KEY'):
print("Auth: Generating new random SECRET_KEY...")
auth_config['SECRET_KEY'] = pyotp.random_base32()
save_needed = True
# 2. COOKIE_NAME
if not auth_config.get('COOKIE_NAME'):
print("Auth: Generating new random COOKIE_NAME...")
random_suffix = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
auth_config['COOKIE_NAME'] = f"ComfyUI_Auth_{random_suffix}"
save_needed = True
# 3. New Config Keys Defaults
if 'IS_SETUP_COMPLETED' not in auth_config:
auth_config['IS_SETUP_COMPLETED'] = 'False'
save_needed = True
if 'SKIP_AUTH_ON_LOCALHOST' not in auth_config:
auth_config['SKIP_AUTH_ON_LOCALHOST'] = 'False'
save_needed = True
if 'IP_WHITELIST' not in auth_config:
auth_config['IP_WHITELIST'] = ''
save_needed = True
if save_needed:
with open(CONFIG_FILE, 'w') as f:
config.write(f)
return config
def save_setup_complete(allow_localhost):
config = load_config()
config['AUTH']['IS_SETUP_COMPLETED'] = 'True'
config['AUTH']['SKIP_AUTH_ON_LOCALHOST'] = str(allow_localhost)
with open(CONFIG_FILE, 'w') as f:
config.write(f)
# Global Config Object
current_config = load_config()
SECRET_KEY = current_config['AUTH']['SECRET_KEY']
COOKIE_NAME = current_config['AUTH']['COOKIE_NAME']
# ==========================================
# Security / Rate Limiting
# ==========================================
# Simple in-memory rate limiter per IP
# (Not persistent across restarts, but sufficient for runtime brute-force protection)
LOGIN_ATTEMPTS = defaultdict(list)
MAX_ATTEMPTS = 5 # Max failed attempts
WINDOW_SECONDS = 600 # 10 minutes
def check_rate_limit(ip):
now = time.time()
attempts = LOGIN_ATTEMPTS[ip]
# Clean up old attempts
attempts = [t for t in attempts if now - t < WINDOW_SECONDS]
LOGIN_ATTEMPTS[ip] = attempts
if len(attempts) >= MAX_ATTEMPTS:
return False # Locked
return True
def register_failed_attempt(ip):
LOGIN_ATTEMPTS[ip].append(time.time())
# ==========================================
# HTML Templates
# ==========================================
SETUP_HTML = f"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ComfyUI Auth Setup</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
<style>
body {{ font-family: sans-serif; background: #1e1e1e; color: #fff; display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; }}
.box {{ background: #2c2c2c; padding: 2rem; border-radius: 10px; max-width: 400px; width: 100%; text-align: center; box-shadow: 0 4px 15px rgba(0,0,0,0.5); }}
h2 {{ margin-top: 0; color: #4dabf7; }}
.step {{ margin: 20px 0; border-top: 1px solid #444; padding-top: 20px; }}
#qrcode {{ display: flex; justify-content: center; margin: 20px 0; background: #fff; padding: 10px; border-radius: 5px; }}
input[type="text"] {{ font-size: 1.5rem; padding: 10px; width: 150px; text-align: center; margin: 10px 0; border-radius: 5px; border: none; }}
label {{ cursor: pointer; display: block; margin: 15px 0; font-size: 0.9rem; color: #ccc; }}
button {{ font-size: 1.1rem; padding: 12px 30px; background: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer; width: 100%; }}
button:hover {{ background: #0056b3; }}
.error {{ color: #ff6b6b; margin-top: 10px; min-height: 1.2em; }}
.secret-text {{ font-family: monospace; background: #333; padding: 5px; border-radius: 3px; font-size: 0.9em; word-break: break-all; }}
</style>
</head>
<body>
<div class="box">
<h2>🛡️ Initial Setup</h2>
<div class="step">
<p>1. Scan this QR code with Google Authenticator:</p>
<div id="qrcode"></div>
<p>Or manually enter key:</p>
<div class="secret-text">{SECRET_KEY}</div>
</div>
<div class="step">
<p>2. Configuration</p>
<label>
<input type="checkbox" id="allow_local"> Allow Localhost (127.0.0.1) without Auth
</label>
</div>
<div class="step">
<p>3. Verify & Complete</p>
<input type="text" id="otp" placeholder="123456" maxlength="6" inputmode="numeric">
<div id="msg" class="error"></div>
<button onclick="finishSetup()">Complete Setup</button>
</div>
</div>
<script>
// Generate QR Code
const secret = "{SECRET_KEY}";
const label = "ComfyUI-User-Auth";
const otpauth = `otpauth://totp/${{label}}?secret=${{secret}}&issuer=ComfyUI`;
new QRCode(document.getElementById("qrcode"), {{
text: otpauth,
width: 128,
height: 128
}});
async function finishSetup() {{
const code = document.getElementById('otp').value;
const allowLocal = document.getElementById('allow_local').checked;
const msg = document.getElementById('msg');
msg.innerText = "Verifying...";
try {{
const res = await fetch('/custom_auth/setup_complete', {{
method: 'POST',
headers: {{'Content-Type': 'application/json'}},
body: JSON.stringify({{code: code, allow_localhost: allowLocal}})
}});
if (res.ok) {{
alert("Setup Complete! Reloading...");
location.reload();
}} else {{
const data = await res.text();
msg.innerText = "Error: " + data;
}}
}} catch (e) {{
msg.innerText = "Connection Error";
}}
}}
</script>
</body>
</html>
"""
LOGIN_HTML = """
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ComfyUI Login</title>
<style>
body { font-family: sans-serif; background: #1e1e1e; color: #fff; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }
.box { background: #2c2c2c; padding: 2rem; border-radius: 10px; text-align: center; box-shadow: 0 4px 15px rgba(0,0,0,0.5); }
input { font-size: 1.5rem; padding: 10px; width: 150px; text-align: center; margin-bottom: 20px; border-radius: 5px; border: none; }
button { font-size: 1.2rem; padding: 10px 30px; background: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer; }
button:hover { background: #0056b3; }
.error { color: #ff6b6b; margin-bottom: 15px; }
</style>
</head>
<body>
<div class="box">
<h2>🔒 Security Check</h2>
<div id="msg" class="error"></div>
<input type="text" id="otp" placeholder="123456" maxlength="6" inputmode="numeric">
<br>
<button onclick="login()">Login</button>
</div>
<script>
async function login() {
const code = document.getElementById('otp').value;
const res = await fetch('/custom_auth/login', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({code: code})
});
if (res.ok) {
location.reload();
} else {
const txt = await res.text();
document.getElementById('msg').innerText = txt || "Invalid Code";
}
}
</script>
</body>
</html>
"""
# ==========================================
# Helper Functions
# ==========================================
def is_ip_whitelisted(request):
remote_ip = request.remote
config = load_config()['AUTH']
if config.getboolean('SKIP_AUTH_ON_LOCALHOST'):
if remote_ip in ['127.0.0.1', '::1', 'localhost']:
return True
whitelist_str = config.get('IP_WHITELIST', '')
if not whitelist_str:
return False
whitelist = [ip.strip() for ip in whitelist_str.split(',') if ip.strip()]
try:
user_ip = ipaddress.ip_address(remote_ip)
for entry in whitelist:
if '/' in entry: # CIDR
if user_ip in ipaddress.ip_network(entry, strict=False):
return True
else: # Single IP
if user_ip == ipaddress.ip_address(entry):
return True
except ValueError:
pass
return False
# ==========================================
# Middleware
# ==========================================
@web.middleware
async def auth_middleware(request, handler):
if request.path.startswith("/custom_auth/"):
return await handler(request)
current_conf = load_config()['AUTH']
is_setup = current_conf.getboolean('IS_SETUP_COMPLETED')
if not is_setup:
return web.Response(text=SETUP_HTML, content_type='text/html')
if is_ip_whitelisted(request):
return await handler(request)
auth_cookie = request.cookies.get(COOKIE_NAME)
if auth_cookie == "authenticated":
return await handler(request)
return web.Response(text=LOGIN_HTML, content_type='text/html')
# ==========================================
# Route Handlers
# ==========================================
async def login_handler(request):
data = await request.json()
code = data.get("code")
ip = request.remote
# 1. Check Rate Limit
if not check_rate_limit(ip):
return web.Response(status=429, text="Too many failed attempts. Try again later.")
# 2. Verify Code
totp = pyotp.TOTP(SECRET_KEY)
if totp.verify(code):
# Clear failures on success? Optional. Keeping them prevents brute force over time.
# But for UX, we could clear. Let's keep it strict for now.
resp = web.Response(text="OK")
resp.set_cookie(COOKIE_NAME, "authenticated", httponly=True, max_age=3600*24*30)
return resp
else:
register_failed_attempt(ip)
return web.Response(status=401, text="Invalid Code")
async def setup_handler(request):
data = await request.json()
code = data.get("code")
allow_localhost = data.get("allow_localhost", False)
totp = pyotp.TOTP(SECRET_KEY)
if totp.verify(code):
save_setup_complete(allow_localhost)
resp = web.Response(text="Setup Completed")
resp.set_cookie(COOKIE_NAME, "authenticated", httponly=True, max_age=3600*24*30)
return resp
else:
return web.Response(status=400, text="Invalid Code. Please scan the QR correctly.")
# ==========================================
# Server Registration
# ==========================================
server = PromptServer.instance
server.app.middlewares.append(auth_middleware)
server.app.router.add_post("/custom_auth/login", login_handler)
server.app.router.add_post("/custom_auth/setup_complete", setup_handler)
NODE_CLASS_MAPPINGS = {}