fix: 优化认证逻辑 (#724)
Some checks are pending
sync2gitee / repo-sync (push) Waiting to run
Typos Check / Spell Check with Typos (push) Waiting to run

This commit is contained in:
shaohuzhang1 2024-07-09 18:59:15 +08:00 committed by GitHub
parent 868e037522
commit fd6aa4fb39
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 4 deletions

View File

@ -7,14 +7,13 @@
@desc: 认证类
"""
import traceback
from importlib import import_module
from django.conf import settings
from django.core import cache
from django.core import signing
from rest_framework.authentication import TokenAuthentication
from common.auth.handle.impl.application_key import ApplicationKey
from common.auth.handle.impl.public_access_token import PublicAccessToken
from common.auth.handle.impl.user_token import UserToken
from common.exception.app_exception import AppAuthenticationFailed, AppEmbedIdentityFailed, AppChatNumOutOfBoundsFailed
token_cache = cache.caches['token_cache']
@ -25,7 +24,16 @@ class AnonymousAuthentication(TokenAuthentication):
return None, None
handles = [UserToken(), PublicAccessToken(), ApplicationKey()]
def new_instance_by_class_path(class_path: str):
parts = class_path.rpartition('.')
package_path = parts[0]
class_name = parts[2]
module = import_module(package_path)
HandlerClass = getattr(module, class_name)
return HandlerClass()
handles = [new_instance_by_class_path(class_path) for class_path in settings.AUTH_HANDLES]
class TokenDetails:

View File

@ -8,3 +8,4 @@
"""
from .base import *
from .logging import *
from .auth import *

View File

@ -0,0 +1,19 @@
# coding=utf-8
"""
@project: MaxKB
@Author
@file auth.py
@date2024/7/9 18:47
@desc:
"""
USER_TOKEN_AUTH = 'common.auth.handle.impl.user_token.UserToken'
PUBLIC_ACCESS_TOKEN_AUTH = 'common.auth.handle.impl.public_access_token.PublicAccessToken'
APPLICATION_KEY_AUTH = 'common.auth.handle.impl.application_key.ApplicationKey'
AUTH_HANDLES = [
USER_TOKEN_AUTH,
PUBLIC_ACCESS_TOKEN_AUTH,
APPLICATION_KEY_AUTH
]