From 473cfa50451a7d4016c79c7f03421cf608a1cbe5 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Tue, 27 Oct 2026 10:36:50 +0800 Subject: [PATCH] fix: enhance authentication handling by ensuring 'LOCAL' is included in login values and adjusting access token response structure --- .../serializers/application_access_token.py | 134 +++++++++--------- apps/chat/serializers/chat_authentication.py | 3 +- apps/chat/serializers/chat_user_serializer.py | 2 +- 3 files changed, 70 insertions(+), 69 deletions(-) diff --git a/apps/application/serializers/application_access_token.py b/apps/application/serializers/application_access_token.py index 96f9ba621a7..12df5887390 100644 --- a/apps/application/serializers/application_access_token.py +++ b/apps/application/serializers/application_access_token.py @@ -1,11 +1,12 @@ # coding=utf-8 """ - @project: MaxKB - @Author:虎虎 - @file: application_access_token.py - @date:2025/6/9 17:49 - @desc: +@project: MaxKB +@Author:虎虎 +@file: application_access_token.py +@date:2025/6/9 17:49 +@desc: """ + import hashlib import uuid_utils.compat as uuid @@ -16,28 +17,22 @@ from application.models import ApplicationAccessToken, Application from common.constants.cache_version import Cache_Version -from common.database_model_manage.database_model_manage import DatabaseModelManage from common.exception.app_exception import AppApiException class AccessTokenEditSerializer(serializers.Serializer): - access_token_reset = serializers.BooleanField(required=False, - label=_("Reset Token")) + access_token_reset = serializers.BooleanField(required=False, label=_("Reset Token")) is_active = serializers.BooleanField(required=False, label=_("Is it enabled")) - access_num = serializers.IntegerField(required=False, max_value=10000000, - min_value=0, - label=_("Number of visits")) - white_active = serializers.BooleanField(required=False, - label=_("Whether to enable whitelist")) - white_list = serializers.ListSerializer(required=False, child=serializers.CharField(required=True, - label=_("Whitelist")), - label=_("Whitelist")), - show_source = serializers.BooleanField(required=False, - label=_("Whether to display knowledge sources")) - show_exec = serializers.BooleanField(required=False, - label=_("Display execution details")) - language = serializers.CharField(required=False, allow_blank=True, allow_null=True, - label=_("language")) + access_num = serializers.IntegerField(required=False, max_value=10000000, min_value=0, label=_("Number of visits")) + white_active = serializers.BooleanField(required=False, label=_("Whether to enable whitelist")) + white_list = ( + serializers.ListSerializer( + required=False, child=serializers.CharField(required=True, label=_("Whitelist")), label=_("Whitelist") + ), + ) + show_source = serializers.BooleanField(required=False, label=_("Whether to display knowledge sources")) + show_exec = serializers.BooleanField(required=False, label=_("Display execution details")) + language = serializers.CharField(required=False, allow_blank=True, allow_null=True, label=_("language")) authentication = serializers.BooleanField(default=False, label="Do you need authentication") authentication_value = serializers.JSONField(required=False, label="Certified value", default=dict) @@ -49,45 +44,44 @@ class AccessTokenSerializer(serializers.Serializer): def is_valid(self, *, raise_exception=False): super().is_valid(raise_exception=True) - workspace_id = self.data.get('workspace_id') - query_set = QuerySet(Application).filter(id=self.data.get('application_id')) + workspace_id = self.data.get("workspace_id") + query_set = QuerySet(Application).filter(id=self.data.get("application_id")) if workspace_id: query_set = query_set.filter(workspace_id=workspace_id) if not query_set.exists(): - raise AppApiException(500, _('Application id does not exist')) + raise AppApiException(500, _("Application id does not exist")) def edit(self, instance): self.is_valid(raise_exception=True) AccessTokenEditSerializer(data=instance).is_valid(raise_exception=True) - application_access_token = QuerySet(ApplicationAccessToken).get( - application_id=self.data.get('application_id')) - if 'is_active' in instance: + application_access_token = QuerySet(ApplicationAccessToken).get(application_id=self.data.get("application_id")) + if "is_active" in instance: application_access_token.is_active = instance.get("is_active") - if 'access_token_reset' in instance and instance.get('access_token_reset'): + if "access_token_reset" in instance and instance.get("access_token_reset"): application_access_token.access_token = hashlib.md5(str(uuid.uuid7()).encode()).hexdigest()[8:24] - if 'access_num' in instance and instance.get('access_num') is not None: + if "access_num" in instance and instance.get("access_num") is not None: application_access_token.access_num = instance.get("access_num") - if 'white_active' in instance and instance.get('white_active') is not None: + if "white_active" in instance and instance.get("white_active") is not None: application_access_token.white_active = instance.get("white_active") - if 'white_list' in instance and instance.get('white_list') is not None: - application_access_token.white_list = instance.get('white_list') - if 'show_source' in instance and instance.get('show_source') is not None: - application_access_token.show_source = instance.get('show_source') - if 'show_exec' in instance and instance.get('show_exec') is not None: - application_access_token.show_exec = instance.get('show_exec') - if 'language' in instance and instance.get('language') is not None: - application_access_token.language = instance.get('language') - if 'language' not in instance or instance.get('language') is None: + if "white_list" in instance and instance.get("white_list") is not None: + application_access_token.white_list = instance.get("white_list") + if "show_source" in instance and instance.get("show_source") is not None: + application_access_token.show_source = instance.get("show_source") + if "show_exec" in instance and instance.get("show_exec") is not None: + application_access_token.show_exec = instance.get("show_exec") + if "language" in instance and instance.get("language") is not None: + application_access_token.language = instance.get("language") + if "language" not in instance or instance.get("language") is None: application_access_token.language = None application_access_token.save() - license_is_valid = cache.get(Cache_Version.SYSTEM.get_key(key='license_is_valid'), - version=Cache_Version.SYSTEM.get_version()) + license_is_valid = cache.get( + Cache_Version.SYSTEM.get_key(key="license_is_valid"), version=Cache_Version.SYSTEM.get_version() + ) if license_is_valid: - if instance.get('authentication') is not None and instance.get( - 'authentication_value') is not None: - application_access_token.authentication = instance.get('authentication') - application_access_token.authentication_value = instance.get('authentication_value') + if instance.get("authentication") is not None and instance.get("authentication_value") is not None: + application_access_token.authentication = instance.get("authentication") + application_access_token.authentication_value = instance.get("authentication_value") application_access_token.save() return self.one(with_valid=False) @@ -95,29 +89,35 @@ def one(self, with_valid=True): if with_valid: self.is_valid(raise_exception=True) application_id = self.data.get("application_id") - application_access_token = QuerySet(ApplicationAccessToken).filter( - application_id=application_id).first() + application_access_token = QuerySet(ApplicationAccessToken).filter(application_id=application_id).first() if application_access_token is None: - application_access_token = ApplicationAccessToken(application_id=application_id, - access_token=hashlib.md5( - str(uuid.uuid7()).encode()).hexdigest()[ - 8:24], is_active=True) + application_access_token = ApplicationAccessToken( + application_id=application_id, + access_token=hashlib.md5(str(uuid.uuid7()).encode()).hexdigest()[8:24], + is_active=True, + ) application_access_token.save() other = {} - license_is_valid = cache.get(Cache_Version.SYSTEM.get_key(key='license_is_valid'), - version=Cache_Version.SYSTEM.get_version()) + license_is_valid = cache.get( + Cache_Version.SYSTEM.get_key(key="license_is_valid"), version=Cache_Version.SYSTEM.get_version() + ) if license_is_valid: - other = {'authentication': application_access_token.authentication, - 'authentication_value': application_access_token.authentication_value} + other = { + "authentication": application_access_token.authentication, + "authentication_value": application_access_token.authentication_value, + } + else: + other = {"authentication": application_access_token.authentication} - return {'application_id': application_access_token.application_id, - 'access_token': application_access_token.access_token, - "is_active": application_access_token.is_active, - 'access_num': application_access_token.access_num, - 'white_active': application_access_token.white_active, - 'white_list': application_access_token.white_list, - 'show_source': application_access_token.show_source, - 'show_exec': application_access_token.show_exec, - 'language': application_access_token.language, - **other, - } + return { + "application_id": application_access_token.application_id, + "access_token": application_access_token.access_token, + "is_active": application_access_token.is_active, + "access_num": application_access_token.access_num, + "white_active": application_access_token.white_active, + "white_list": application_access_token.white_list, + "show_source": application_access_token.show_source, + "show_exec": application_access_token.show_exec, + "language": application_access_token.language, + **other, + } diff --git a/apps/chat/serializers/chat_authentication.py b/apps/chat/serializers/chat_authentication.py index 528222a1b81..e3ef58d5ac9 100644 --- a/apps/chat/serializers/chat_authentication.py +++ b/apps/chat/serializers/chat_authentication.py @@ -69,7 +69,8 @@ def profile(self): if chat_platform is not None: types = QuerySet(chat_platform).filter(is_active=True, is_valid=True).values_list("auth_type", flat=True) login_value = list(set(login_value) & set(types)) - if "LOCAL" in application_access_token.authentication_value.get("login_value", []): + if application_access_token.authentication: + if "LOCAL" not in login_value: login_value.insert(0, "LOCAL") return { "max_attempts": application_access_token.authentication_value.get("max_attempts", 1), diff --git a/apps/chat/serializers/chat_user_serializer.py b/apps/chat/serializers/chat_user_serializer.py index 38b9afbfd15..3fde9f54015 100644 --- a/apps/chat/serializers/chat_user_serializer.py +++ b/apps/chat/serializers/chat_user_serializer.py @@ -45,7 +45,7 @@ def _get_login_policy(cls, access_token): if portal is None or not portal.enable_auth: raise AppApiException(500, _("Portal authentication is not enabled")) auth_config = portal.auth_config or {} - if "LOCAL" not in auth_config.get("login_value", []): + if "LOCAL" not in auth_config.get("login_value", ["LOCAL"]): raise AppApiException(500, _("Portal local login is not enabled")) return ( "portal",