Skip to content

Commit 143a1cd

Browse files
committed
Added support for authentication in --share mode (via auth.json)
1 parent b900eee commit 143a1cd

File tree

6 files changed

+56
-1
lines changed

6 files changed

+56
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ notification.mp3
4141
/resolutions.json
4242
/notification.ogg
4343
/notification.mp3
44+
/auth.json

auth-example.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"user": "sitting-duck-1",
4+
"pass": "very-bad-publicly-known-password-change-it"
5+
}
6+
]

modules/auth.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import json
2+
import hashlib
3+
4+
from os.path import exists
5+
6+
7+
def auth_list_to_dict(auth_list):
8+
auth_dict = {}
9+
for auth_data in auth_list:
10+
if 'user' in auth_data:
11+
if 'hash' in auth_data:
12+
auth_dict |= {auth_data['user']: auth_data['hash']}
13+
elif 'pass' in auth_data:
14+
auth_dict |= {auth_data['user']: hashlib.sha256(bytes(auth_data['pass'], encoding='utf-8')).hexdigest()}
15+
return auth_dict
16+
17+
18+
def load_auth_data(filename=None):
19+
auth_dict = None
20+
if filename != None and exists(filename):
21+
with open(filename, encoding='utf-8') as auth_file:
22+
try:
23+
auth_obj = json.load(auth_file)
24+
if isinstance(auth_obj, list) and len(auth_obj) > 0:
25+
auth_dict = auth_list_to_dict(auth_obj)
26+
except Exception as e:
27+
print('load_auth_data, e: ' + str(e))
28+
finally:
29+
auth_file.close()
30+
return auth_dict
31+
32+
33+
auth_dict = load_auth_data('auth.json')
34+
35+
auth_enabled = auth_dict != None
36+
37+
38+
def check_auth(user, password):
39+
if user not in auth_dict:
40+
return False
41+
else:
42+
return hashlib.sha256(bytes(password, encoding='utf-8')).hexdigest() == auth_dict[user]

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ Below things are already inside the software, and **users do not need to do anyt
182182
24. Support for playing audio when generation is finished (ported from SD web UI - use notification.ogg or notification.mp3).
183183
25. Starting generation via Ctrl-ENTER hotkey (ported from SD web UI).
184184
26. Support for loading models from subfolders (ported from RuinedFooocus).
185+
27. Support for authentication in --share mode (credentials loaded from auth.json - use auth-examle.json as a template).
185186

186187
## Thanks
187188

update_log_mre.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 2.0.18 MRE
2+
3+
* Added support for authentication in --share mode (via auth.json).
4+
15
### 2.0.14 MRE
26

37
* Added support for loading models from subfolders (ported from RuinedFooocus).

webui.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from fastapi import FastAPI
2020
from modules.ui_gradio_extensions import reload_javascript
2121
from modules.util import get_current_log_path, get_previous_log_path
22+
from modules.auth import auth_enabled, check_auth
2223
from os.path import exists
2324

2425

@@ -485,4 +486,4 @@ def stop_clicked():
485486

486487

487488
app = gr.mount_gradio_app(app, shared.gradio_root, '/')
488-
shared.gradio_root.launch(inbrowser=True, server_name=args.listen, server_port=args.port, share=args.share)
489+
shared.gradio_root.launch(inbrowser=True, server_name=args.listen, server_port=args.port, share=args.share, auth=check_auth if args.share and auth_enabled else None)

0 commit comments

Comments
 (0)