|
| 1 | +# coding=utf-8 |
| 2 | +""" |
| 3 | + @project: qabot |
| 4 | + @Author:虎虎 |
| 5 | + @file: authenticate.py |
| 6 | + @date:2023/9/4 11:16 |
| 7 | + @desc: 认证类 |
| 8 | +""" |
| 9 | +import traceback |
| 10 | +from importlib import import_module |
| 11 | + |
| 12 | +from django.conf import settings |
| 13 | +from django.core import cache |
| 14 | +from django.core import signing |
| 15 | +from rest_framework.authentication import TokenAuthentication |
| 16 | + |
| 17 | +from common.exception.app_exception import AppAuthenticationFailed, AppEmbedIdentityFailed, AppChatNumOutOfBoundsFailed, \ |
| 18 | + ChatException, AppApiException |
| 19 | +from django.utils.translation import gettext_lazy as _ |
| 20 | + |
| 21 | +token_cache = cache.caches['default'] |
| 22 | + |
| 23 | + |
| 24 | +class AnonymousAuthentication(TokenAuthentication): |
| 25 | + def authenticate(self, request): |
| 26 | + return None, None |
| 27 | + |
| 28 | + |
| 29 | +def new_instance_by_class_path(class_path: str): |
| 30 | + parts = class_path.rpartition('.') |
| 31 | + package_path = parts[0] |
| 32 | + class_name = parts[2] |
| 33 | + module = import_module(package_path) |
| 34 | + HandlerClass = getattr(module, class_name) |
| 35 | + return HandlerClass() |
| 36 | + |
| 37 | + |
| 38 | +handles = [new_instance_by_class_path(class_path) for class_path in settings.AUTH_HANDLES] |
| 39 | + |
| 40 | + |
| 41 | +class TokenDetails: |
| 42 | + token_details = None |
| 43 | + is_load = False |
| 44 | + |
| 45 | + def __init__(self, token: str): |
| 46 | + self.token = token |
| 47 | + |
| 48 | + def get_token_details(self): |
| 49 | + if self.token_details is None and not self.is_load: |
| 50 | + try: |
| 51 | + self.token_details = signing.loads(self.token) |
| 52 | + except Exception as e: |
| 53 | + self.is_load = True |
| 54 | + return self.token_details |
| 55 | + |
| 56 | + |
| 57 | +class OpenAIKeyAuth(TokenAuthentication): |
| 58 | + def authenticate(self, request): |
| 59 | + auth = request.META.get('HTTP_AUTHORIZATION') |
| 60 | + auth = auth.replace('Bearer ', '') |
| 61 | + # 未认证 |
| 62 | + if auth is None: |
| 63 | + raise AppAuthenticationFailed(1003, _('Not logged in, please log in first')) |
| 64 | + try: |
| 65 | + token_details = TokenDetails(auth) |
| 66 | + for handle in handles: |
| 67 | + if handle.support(request, auth, token_details.get_token_details): |
| 68 | + return handle.handle(request, auth, token_details.get_token_details) |
| 69 | + raise AppAuthenticationFailed(1002, _('Authentication information is incorrect! illegal user')) |
| 70 | + except Exception as e: |
| 71 | + traceback.format_exc() |
| 72 | + if isinstance(e, AppEmbedIdentityFailed) or isinstance(e, AppChatNumOutOfBoundsFailed) or isinstance(e, |
| 73 | + AppApiException): |
| 74 | + raise e |
| 75 | + raise AppAuthenticationFailed(1002, _('Authentication information is incorrect! illegal user')) |
| 76 | + |
| 77 | + |
| 78 | +class TokenAuth(TokenAuthentication): |
| 79 | + # 重新 authenticate 方法,自定义认证规则 |
| 80 | + def authenticate(self, request): |
| 81 | + auth = request.META.get('HTTP_AUTHORIZATION') |
| 82 | + # 未认证 |
| 83 | + if auth is None: |
| 84 | + raise AppAuthenticationFailed(1003, _('Not logged in, please log in first')) |
| 85 | + try: |
| 86 | + token_details = TokenDetails(auth) |
| 87 | + for handle in handles: |
| 88 | + if handle.support(request, auth, token_details.get_token_details): |
| 89 | + return handle.handle(request, auth, token_details.get_token_details) |
| 90 | + raise AppAuthenticationFailed(1002, _('Authentication information is incorrect! illegal user')) |
| 91 | + except Exception as e: |
| 92 | + traceback.format_exc() |
| 93 | + if isinstance(e, AppEmbedIdentityFailed) or isinstance(e, AppChatNumOutOfBoundsFailed) or isinstance(e, |
| 94 | + AppApiException): |
| 95 | + raise e |
| 96 | + raise AppAuthenticationFailed(1002, _('Authentication information is incorrect! illegal user')) |
0 commit comments