Skip to content

Commit

Permalink
fix: 优化认证逻辑 (1Panel-dev#724)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaohuzhang1 authored Jul 9, 2024
1 parent 868e037 commit fd6aa4f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
16 changes: 12 additions & 4 deletions apps/common/auth/authenticate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -25,7 +24,16 @@ def authenticate(self, request):
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:
Expand Down
1 change: 1 addition & 0 deletions apps/smartdoc/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
"""
from .base import *
from .logging import *
from .auth import *
19 changes: 19 additions & 0 deletions apps/smartdoc/settings/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# coding=utf-8
"""
@project: MaxKB
@Author:虎
@file: auth.py
@date:2024/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
]

0 comments on commit fd6aa4f

Please sign in to comment.