Skip to content

Commit b8da4a8

Browse files
committed
refac
1 parent b8912aa commit b8da4a8

File tree

4 files changed

+85
-18
lines changed

4 files changed

+85
-18
lines changed

backend/open_webui/config.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -779,12 +779,6 @@ def oidc_oauth_register(client: OAuth):
779779
pass
780780

781781

782-
####################################
783-
# LICENSE_KEY
784-
####################################
785-
786-
LICENSE_KEY = os.environ.get("LICENSE_KEY", "")
787-
788782
####################################
789783
# STORAGE PROVIDER
790784
####################################

backend/open_webui/env.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import shutil
88
from uuid import uuid4
99
from pathlib import Path
10+
from cryptography.hazmat.primitives import serialization
1011

1112
import markdown
1213
from bs4 import BeautifulSoup
@@ -430,6 +431,34 @@ def parse_section(section):
430431
os.environ.get("ENABLE_COMPRESSION_MIDDLEWARE", "True").lower() == "true"
431432
)
432433

434+
435+
####################################
436+
# LICENSE_KEY
437+
####################################
438+
439+
LICENSE_KEY = os.environ.get("LICENSE_KEY", "")
440+
441+
LICENSE_BLOB = None
442+
LICENSE_BLOB_PATH = os.environ.get("LICENSE_BLOB_PATH", DATA_DIR / "l.data")
443+
if LICENSE_BLOB_PATH and os.path.exists(LICENSE_BLOB_PATH):
444+
with open(LICENSE_BLOB_PATH, "rb") as f:
445+
LICENSE_BLOB = f.read()
446+
447+
LICENSE_PUBLIC_KEY = os.environ.get("LICENSE_PUBLIC_KEY", "")
448+
449+
pk = None
450+
if LICENSE_PUBLIC_KEY:
451+
pk = serialization.load_pem_public_key(
452+
f"""
453+
-----BEGIN PUBLIC KEY-----
454+
{LICENSE_PUBLIC_KEY}
455+
-----END PUBLIC KEY-----
456+
""".encode(
457+
"utf-8"
458+
)
459+
)
460+
461+
433462
####################################
434463
# MODELS
435464
####################################

backend/open_webui/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@
102102
from open_webui.models.chats import Chats
103103

104104
from open_webui.config import (
105-
LICENSE_KEY,
106105
# Ollama
107106
ENABLE_OLLAMA_API,
108107
OLLAMA_BASE_URLS,
@@ -395,6 +394,7 @@
395394
reset_config,
396395
)
397396
from open_webui.env import (
397+
LICENSE_KEY,
398398
AUDIT_EXCLUDED_PATHS,
399399
AUDIT_LOG_LEVEL,
400400
CHANGELOG,

backend/open_webui/utils/auth.py

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
import os
99

1010

11+
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
12+
from cryptography.hazmat.primitives.asymmetric import ed25519
13+
from cryptography.hazmat.primitives import serialization
14+
import json
15+
16+
1117
from datetime import datetime, timedelta
1218
import pytz
1319
from pytz import UTC
@@ -18,7 +24,11 @@
1824
from open_webui.models.users import Users
1925

2026
from open_webui.constants import ERROR_MESSAGES
27+
2128
from open_webui.env import (
29+
OFFLINE_MODE,
30+
LICENSE_BLOB,
31+
pk,
2232
WEBUI_SECRET_KEY,
2333
TRUSTED_SIGNATURE_KEY,
2434
STATIC_DIR,
@@ -74,6 +84,18 @@ def override_static(path: str, content: str):
7484

7585

7686
def get_license_data(app, key):
87+
def data_handler(data):
88+
for k, v in data.items():
89+
if k == "resources":
90+
for p, c in v.items():
91+
globals().get("override_static", lambda a, b: None)(p, c)
92+
elif k == "count":
93+
setattr(app.state, "USER_COUNT", v)
94+
elif k == "name":
95+
setattr(app.state, "WEBUI_NAME", v)
96+
elif k == "metadata":
97+
setattr(app.state, "LICENSE_METADATA", v)
98+
7799
def handler(u):
78100
res = requests.post(
79101
f"{u}/api/v1/license/",
@@ -83,30 +105,52 @@ def handler(u):
83105

84106
if getattr(res, "ok", False):
85107
payload = getattr(res, "json", lambda: {})()
86-
for k, v in payload.items():
87-
if k == "resources":
88-
for p, c in v.items():
89-
globals().get("override_static", lambda a, b: None)(p, c)
90-
elif k == "count":
91-
setattr(app.state, "USER_COUNT", v)
92-
elif k == "name":
93-
setattr(app.state, "WEBUI_NAME", v)
94-
elif k == "metadata":
95-
setattr(app.state, "LICENSE_METADATA", v)
108+
data_handler(payload)
96109
return True
97110
else:
98111
log.error(
99112
f"License: retrieval issue: {getattr(res, 'text', 'unknown error')}"
100113
)
101114

102115
if key:
103-
us = ["https://api.openwebui.com", "https://licenses.api.openwebui.com"]
116+
us = [
117+
"https://api.openwebui.com",
118+
"https://licenses.api.openwebui.com",
119+
]
104120
try:
105121
for u in us:
106122
if handler(u):
107123
return True
108124
except Exception as ex:
109125
log.exception(f"License: Uncaught Exception: {ex}")
126+
127+
try:
128+
if LICENSE_BLOB:
129+
nl = 12
130+
kb = hashlib.sha256((key.replace("-", "").upper()).encode()).digest()
131+
132+
def nt(b):
133+
return b[:nl], b[nl:]
134+
135+
lb = base64.b64decode(LICENSE_BLOB)
136+
ln, lt = nt(lb)
137+
138+
aesgcm = AESGCM(kb)
139+
p = json.loads(aesgcm.decrypt(ln, lt, None))
140+
pk.verify(base64.b64decode(p["s"]), p["p"].encode())
141+
142+
pb = base64.b64decode(p["p"])
143+
pn, pt = nt(pb)
144+
145+
data = json.loads(aesgcm.decrypt(pn, pt, None).decode())
146+
if not data.get("exp") and data.get("exp") < datetime.now().date():
147+
return False
148+
149+
data_handler(data)
150+
return True
151+
except Exception as e:
152+
log.error(f"License: {e}")
153+
110154
return False
111155

112156

0 commit comments

Comments
 (0)