From d3e6706483b64ec051790d02e4def08f0c59360b Mon Sep 17 00:00:00 2001 From: hexiaonan-800 Date: Thu, 29 Oct 2026 14:49:37 +0800 Subject: [PATCH] feat: chat user api key authenticator --- apps/chat/views/v3/chat.py | 5 ++- apps/common/auth/handle/impl/chat_user_key.py | 35 +++++++++++++++++++ apps/maxkb/settings/auth/web.py | 27 +++++++------- 3 files changed, 51 insertions(+), 16 deletions(-) create mode 100644 apps/common/auth/handle/impl/chat_user_key.py diff --git a/apps/chat/views/v3/chat.py b/apps/chat/views/v3/chat.py index 47f417c310d..3cee1d170ee 100644 --- a/apps/chat/views/v3/chat.py +++ b/apps/chat/views/v3/chat.py @@ -492,7 +492,10 @@ def create_token_and_cache(self, user, access_token, operate): application_id = application_access_token.application_id workspace_id = application_access_token.application.workspace_id token = ChatToken( - str(user.id), AuthenticationType.CHAT_USER, str(operate), application_id=str(application_id) + str(user.id), + AuthenticationType.CHAT_USER, + str(operate), + application_id=str(application_id) if application_id else None, ).to_token() version, get_key = Cache_Version.CHAT_USER_TOKEN.value cache.set(get_key(token), user, timeout=60 * 60 * 2, version=version) diff --git a/apps/common/auth/handle/impl/chat_user_key.py b/apps/common/auth/handle/impl/chat_user_key.py new file mode 100644 index 00000000000..d64ea71b68e --- /dev/null +++ b/apps/common/auth/handle/impl/chat_user_key.py @@ -0,0 +1,35 @@ +# coding=utf-8 +""" +@project: MaxKB +@Author:虎虎 +@file: chat_user_key.py +@date:2025/10/29 11:30 +@desc: 对话用户api key认证 +""" + +from django.db.models import QuerySet +from django.utils.translation import gettext_lazy as _ + +from application.models import ChatUserType +from common.auth.handle.auth_base_handle import AuthBaseHandle +from common.auth.handle.impl.chat_user_token import get_auth +from common.auth.struct.auth import Principal +from common.exception.app_exception import AppAuthenticationFailed +from system_manage.models import ChatUser, ChatUserApiKey + + +class ChatUserKey(AuthBaseHandle): + def handle(self, request, token: str, get_token_details): + chat_user_api_key = QuerySet(ChatUserApiKey).filter(secret_key=token).first() + if chat_user_api_key is None: + raise AppAuthenticationFailed(500, _("Secret key is invalid")) + if not chat_user_api_key.is_active: + raise AppAuthenticationFailed(500, _("Secret key is invalid")) + chat_user = QuerySet(ChatUser).filter(id=chat_user_api_key.user_id).first() + if chat_user is None or not chat_user.is_active: + raise AppAuthenticationFailed(500, _("Secret key is invalid")) + auth = get_auth(chat_user.source, chat_user.id, None) + return Principal(str(chat_user.id), ChatUserType.CHAT_USER, profile=chat_user), auth + + def support(self, request, token: str, get_token_details): + return str(token).startswith("chat-") diff --git a/apps/maxkb/settings/auth/web.py b/apps/maxkb/settings/auth/web.py index 497bd1bc979..29b72aff5b5 100644 --- a/apps/maxkb/settings/auth/web.py +++ b/apps/maxkb/settings/auth/web.py @@ -1,19 +1,16 @@ # coding=utf-8 """ - @project: MaxKB - @Author:虎虎 - @file: auth.py - @date:2024/7/9 18:47 - @desc: +@project: MaxKB +@Author:虎虎 +@file: auth.py +@date:2024/7/9 18:47 +@desc: """ -USER_TOKEN_AUTH = 'common.auth.handle.impl.user_token.UserToken' -CHAT_ANONYMOUS_USER_AURH = 'common.auth.handle.impl.chat_user_token.ChatUserToken' -APPLICATION_KEY_AUTH = 'common.auth.handle.impl.application_key.ApplicationKey' -AUTH_HANDLES = [ - USER_TOKEN_AUTH -] -CHAT_AUTH_HANDLES = [ - CHAT_ANONYMOUS_USER_AURH, - APPLICATION_KEY_AUTH -] +USER_TOKEN_AUTH = "common.auth.handle.impl.user_token.UserToken" +CHAT_ANONYMOUS_USER_AURH = "common.auth.handle.impl.chat_user_token.ChatUserToken" +APPLICATION_KEY_AUTH = "common.auth.handle.impl.application_key.ApplicationKey" +CHAT_USER_KEY_AUTH = "common.auth.handle.impl.chat_user_key.ChatUserKey" +AUTH_HANDLES = [USER_TOKEN_AUTH] + +CHAT_AUTH_HANDLES = [CHAT_ANONYMOUS_USER_AURH, APPLICATION_KEY_AUTH, CHAT_USER_KEY_AUTH]