Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion apps/chat/views/v3/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions apps/common/auth/handle/impl/chat_user_key.py
Original file line number Diff line number Diff line change
@@ -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-")
27 changes: 12 additions & 15 deletions apps/maxkb/settings/auth/web.py
Original file line number Diff line number Diff line change
@@ -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]
Loading