|
|
|
@ -1,14 +1,13 @@
|
|
|
|
import json
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import contexts
|
|
|
|
import flask_login # type: ignore
|
|
|
|
import flask_login # type: ignore
|
|
|
|
|
|
|
|
from dify_app import DifyApp
|
|
|
|
from flask import Response, request
|
|
|
|
from flask import Response, request
|
|
|
|
from flask_login import user_loaded_from_request, user_logged_in
|
|
|
|
from flask_login import user_loaded_from_request, user_logged_in
|
|
|
|
from werkzeug.exceptions import Unauthorized
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import contexts
|
|
|
|
|
|
|
|
from dify_app import DifyApp
|
|
|
|
|
|
|
|
from libs.passport import PassportService
|
|
|
|
from libs.passport import PassportService
|
|
|
|
from services.account_service import AccountService
|
|
|
|
from services.account_service import AccountService
|
|
|
|
|
|
|
|
from werkzeug.exceptions import Unauthorized
|
|
|
|
|
|
|
|
|
|
|
|
login_manager = flask_login.LoginManager()
|
|
|
|
login_manager = flask_login.LoginManager()
|
|
|
|
|
|
|
|
|
|
|
|
@ -17,7 +16,7 @@ login_manager = flask_login.LoginManager()
|
|
|
|
@login_manager.request_loader
|
|
|
|
@login_manager.request_loader
|
|
|
|
def load_user_from_request(request_from_flask_login):
|
|
|
|
def load_user_from_request(request_from_flask_login):
|
|
|
|
"""Load user based on the request."""
|
|
|
|
"""Load user based on the request."""
|
|
|
|
if request.blueprint not in {"console", "inner_api"}:
|
|
|
|
if request.blueprint not in {"console", "inner_api", "service_api_with_auth"}:
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
# Check if the user_id contains a dot, indicating the old format
|
|
|
|
# Check if the user_id contains a dot, indicating the old format
|
|
|
|
auth_header = request.headers.get("Authorization", "")
|
|
|
|
auth_header = request.headers.get("Authorization", "")
|
|
|
|
@ -27,11 +26,15 @@ def load_user_from_request(request_from_flask_login):
|
|
|
|
raise Unauthorized("Invalid Authorization token.")
|
|
|
|
raise Unauthorized("Invalid Authorization token.")
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
if " " not in auth_header:
|
|
|
|
if " " not in auth_header:
|
|
|
|
raise Unauthorized("Invalid Authorization header format. Expected 'Bearer <api-key>' format.")
|
|
|
|
raise Unauthorized(
|
|
|
|
|
|
|
|
"Invalid Authorization header format. Expected 'Bearer <api-key>' format."
|
|
|
|
|
|
|
|
)
|
|
|
|
auth_scheme, auth_token = auth_header.split(None, 1)
|
|
|
|
auth_scheme, auth_token = auth_header.split(None, 1)
|
|
|
|
auth_scheme = auth_scheme.lower()
|
|
|
|
auth_scheme = auth_scheme.lower()
|
|
|
|
if auth_scheme != "bearer":
|
|
|
|
if auth_scheme != "bearer":
|
|
|
|
raise Unauthorized("Invalid Authorization header format. Expected 'Bearer <api-key>' format.")
|
|
|
|
raise Unauthorized(
|
|
|
|
|
|
|
|
"Invalid Authorization header format. Expected 'Bearer <api-key>' format."
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
decoded = PassportService().verify(auth_token)
|
|
|
|
decoded = PassportService().verify(auth_token)
|
|
|
|
user_id = decoded.get("user_id")
|
|
|
|
user_id = decoded.get("user_id")
|
|
|
|
|