Skip to content

Commit fd6aa4f

Browse files
authored
fix: 优化认证逻辑 (#724)
1 parent 868e037 commit fd6aa4f

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

apps/common/auth/authenticate.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
@desc: 认证类
88
"""
99
import traceback
10+
from importlib import import_module
1011

12+
from django.conf import settings
1113
from django.core import cache
1214
from django.core import signing
1315
from rest_framework.authentication import TokenAuthentication
1416

15-
from common.auth.handle.impl.application_key import ApplicationKey
16-
from common.auth.handle.impl.public_access_token import PublicAccessToken
17-
from common.auth.handle.impl.user_token import UserToken
1817
from common.exception.app_exception import AppAuthenticationFailed, AppEmbedIdentityFailed, AppChatNumOutOfBoundsFailed
1918

2019
token_cache = cache.caches['token_cache']
@@ -25,7 +24,16 @@ def authenticate(self, request):
2524
return None, None
2625

2726

28-
handles = [UserToken(), PublicAccessToken(), ApplicationKey()]
27+
def new_instance_by_class_path(class_path: str):
28+
parts = class_path.rpartition('.')
29+
package_path = parts[0]
30+
class_name = parts[2]
31+
module = import_module(package_path)
32+
HandlerClass = getattr(module, class_name)
33+
return HandlerClass()
34+
35+
36+
handles = [new_instance_by_class_path(class_path) for class_path in settings.AUTH_HANDLES]
2937

3038

3139
class TokenDetails:

apps/smartdoc/settings/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
"""
99
from .base import *
1010
from .logging import *
11+
from .auth import *

apps/smartdoc/settings/auth.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# coding=utf-8
2+
"""
3+
@project: MaxKB
4+
@Author:虎
5+
@file: auth.py
6+
@date:2024/7/9 18:47
7+
@desc:
8+
"""
9+
USER_TOKEN_AUTH = 'common.auth.handle.impl.user_token.UserToken'
10+
11+
PUBLIC_ACCESS_TOKEN_AUTH = 'common.auth.handle.impl.public_access_token.PublicAccessToken'
12+
13+
APPLICATION_KEY_AUTH = 'common.auth.handle.impl.application_key.ApplicationKey'
14+
15+
AUTH_HANDLES = [
16+
USER_TOKEN_AUTH,
17+
PUBLIC_ACCESS_TOKEN_AUTH,
18+
APPLICATION_KEY_AUTH
19+
]

0 commit comments

Comments
 (0)