Skip to content

Commit f21e6e0

Browse files
authored
refactor: Consolidate Flask-Login Authentication Logic (langgenius#20235)
Signed-off-by: -LAN- <laipz8200@outlook.com>
1 parent 6f982eb commit f21e6e0

File tree

2 files changed

+23
-35
lines changed

2 files changed

+23
-35
lines changed

api/extensions/ext_login.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
from werkzeug.exceptions import NotFound, Unauthorized
77

88
import contexts
9+
from configs import dify_config
910
from dify_app import DifyApp
1011
from extensions.ext_database import db
1112
from libs.passport import PassportService
12-
from models.account import Account
13+
from models.account import Account, Tenant, TenantAccountJoin
1314
from models.model import EndUser
1415
from services.account_service import AccountService
1516

@@ -32,6 +33,26 @@ def load_user_from_request(request_from_flask_login):
3233
else:
3334
auth_token = request.args.get("_token")
3435

36+
# Check for admin API key authentication first
37+
if dify_config.ADMIN_API_KEY_ENABLE and auth_header:
38+
admin_api_key = dify_config.ADMIN_API_KEY
39+
if admin_api_key and admin_api_key == auth_token:
40+
workspace_id = request.headers.get("X-WORKSPACE-ID")
41+
if workspace_id:
42+
tenant_account_join = (
43+
db.session.query(Tenant, TenantAccountJoin)
44+
.filter(Tenant.id == workspace_id)
45+
.filter(TenantAccountJoin.tenant_id == Tenant.id)
46+
.filter(TenantAccountJoin.role == "owner")
47+
.one_or_none()
48+
)
49+
if tenant_account_join:
50+
tenant, ta = tenant_account_join
51+
account = db.session.query(Account).filter_by(id=ta.account_id).first()
52+
if account:
53+
account.current_tenant = tenant
54+
return account
55+
3556
if request.blueprint in {"console", "inner_api"}:
3657
if not auth_token:
3758
raise Unauthorized("Invalid Authorization token.")

api/libs/login.py

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
from typing import Any
33

44
from flask import current_app, g, has_request_context, request
5-
from flask_login import user_logged_in # type: ignore
65
from flask_login.config import EXEMPT_METHODS # type: ignore
7-
from werkzeug.exceptions import Unauthorized
86
from werkzeug.local import LocalProxy
97

108
from configs import dify_config
11-
from extensions.ext_database import db
12-
from models.account import Account, Tenant, TenantAccountJoin
9+
from models.account import Account
1310
from models.model import EndUser
1411

1512
#: A proxy for the current user. If no user is logged in, this will be an
@@ -53,36 +50,6 @@ def post():
5350

5451
@wraps(func)
5552
def decorated_view(*args, **kwargs):
56-
auth_header = request.headers.get("Authorization")
57-
if dify_config.ADMIN_API_KEY_ENABLE:
58-
if auth_header:
59-
if " " not in auth_header:
60-
raise Unauthorized("Invalid Authorization header format. Expected 'Bearer <api-key>' format.")
61-
auth_scheme, auth_token = auth_header.split(None, 1)
62-
auth_scheme = auth_scheme.lower()
63-
if auth_scheme != "bearer":
64-
raise Unauthorized("Invalid Authorization header format. Expected 'Bearer <api-key>' format.")
65-
66-
admin_api_key = dify_config.ADMIN_API_KEY
67-
if admin_api_key:
68-
if admin_api_key == auth_token:
69-
workspace_id = request.headers.get("X-WORKSPACE-ID")
70-
if workspace_id:
71-
tenant_account_join = (
72-
db.session.query(Tenant, TenantAccountJoin)
73-
.filter(Tenant.id == workspace_id)
74-
.filter(TenantAccountJoin.tenant_id == Tenant.id)
75-
.filter(TenantAccountJoin.role == "owner")
76-
.one_or_none()
77-
)
78-
if tenant_account_join:
79-
tenant, ta = tenant_account_join
80-
account = db.session.query(Account).filter_by(id=ta.account_id).first()
81-
# Login admin
82-
if account:
83-
account.current_tenant = tenant
84-
current_app.login_manager._update_request_context_with_user(account) # type: ignore
85-
user_logged_in.send(current_app._get_current_object(), user=_get_user()) # type: ignore
8653
if request.method in EXEMPT_METHODS or dify_config.LOGIN_DISABLED:
8754
pass
8855
elif not current_user.is_authenticated:

0 commit comments

Comments
 (0)