88"""
99import base64
1010import datetime
11+ import json
1112import os
1213import random
1314import re
3738from common .util .common import valid_license , get_random_chars
3839from common .util .field_message import ErrMessage
3940from common .util .lock import lock
41+ from common .util .rsa_util import decrypt , get_key_pair_by_sql
4042from dataset .models import DataSet , Document , Paragraph , Problem , ProblemParagraphMapping
4143from embedding .task import delete_embedding_by_dataset_id_list
4244from function_lib .models .function import FunctionLib
@@ -75,7 +77,8 @@ def get_profile():
7577 xpack_cache = DBModelManage .get_model ('xpack_cache' )
7678 return {'version' : version , 'IS_XPACK' : hasattr (settings , 'IS_XPACK' ),
7779 'XPACK_LICENSE_IS_VALID' : False if xpack_cache is None else xpack_cache .get ('XPACK_LICENSE_IS_VALID' ,
78- False )}
80+ False ),
81+ 'ras' : get_key_pair_by_sql ().get ('key' )}
7982
8083 @staticmethod
8184 def get_response_body_api ():
@@ -96,35 +99,13 @@ class LoginSerializer(ApiMixin, serializers.Serializer):
9699 password = serializers .CharField (required = True , error_messages = ErrMessage .char (_ ("Password" )))
97100
98101 captcha = serializers .CharField (required = True , error_messages = ErrMessage .char (_ ("captcha" )))
102+ encryptedData = serializers .CharField (required = False , label = _ ('encryptedData' ), allow_null = True ,
103+ allow_blank = True )
99104
100- def is_valid (self , * , raise_exception = False ):
101- """
102- 校验参数
103- :param raise_exception: Whether to throw an exception can only be True
104- :return: User information
105- """
106- super ().is_valid (raise_exception = True )
107- captcha = self .data .get ('captcha' )
108- captcha_value = captcha_cache .get (f"LOGIN:{ captcha .lower ()} " )
109- if captcha_value is None :
110- raise AppApiException (1005 , _ ("Captcha code error or expiration" ))
111- username = self .data .get ("username" )
112- password = password_encrypt (self .data .get ("password" ))
113- user = QuerySet (User ).filter (Q (username = username ,
114- password = password ) | Q (email = username ,
115- password = password )).first ()
116- if user is None :
117- raise ExceptionCodeConstants .INCORRECT_USERNAME_AND_PASSWORD .value .to_app_api_exception ()
118- if not user .is_active :
119- raise AppApiException (1005 , _ ("The user has been disabled, please contact the administrator!" ))
120- return user
121-
122- def get_user_token (self ):
105+ def get_user_token (self , user ):
123106 """
124- Get user token
125107 :return: User Token (authentication information)
126108 """
127- user = self .is_valid ()
128109 token = signing .dumps ({'username' : user .username , 'id' : str (user .id ), 'email' : user .email ,
129110 'type' : AuthenticationType .USER .value })
130111 return token
@@ -136,11 +117,13 @@ class Meta:
136117 def get_request_body_api (self ):
137118 return openapi .Schema (
138119 type = openapi .TYPE_OBJECT ,
139- required = ['username' , 'password ' ],
120+ required = ['username' , 'encryptedData ' ],
140121 properties = {
141122 'username' : openapi .Schema (type = openapi .TYPE_STRING , title = _ ("Username" ), description = _ ("Username" )),
142123 'password' : openapi .Schema (type = openapi .TYPE_STRING , title = _ ("Password" ), description = _ ("Password" )),
143- 'captcha' : openapi .Schema (type = openapi .TYPE_STRING , title = _ ("captcha" ), description = _ ("captcha" ))
124+ 'captcha' : openapi .Schema (type = openapi .TYPE_STRING , title = _ ("captcha" ), description = _ ("captcha" )),
125+ 'encryptedData' : openapi .Schema (type = openapi .TYPE_STRING , title = _ ("encryptedData" ),
126+ description = _ ("encryptedData" ))
144127 }
145128 )
146129
@@ -152,6 +135,29 @@ def get_response_body_api(self):
152135 description = "认证token"
153136 ))
154137
138+ @staticmethod
139+ def login (instance ):
140+ username = instance .get ("username" , "" )
141+ encryptedData = instance .get ("encryptedData" , "" )
142+ if encryptedData :
143+ json_data = json .loads (decrypt (encryptedData ))
144+ instance .update (json_data )
145+ LoginSerializer (data = instance ).is_valid (raise_exception = True )
146+ password = instance .get ("password" )
147+ captcha = instance .get ("captcha" , "" )
148+ captcha_value = captcha_cache .get (f"LOGIN:{ captcha .lower ()} " )
149+ if captcha_value is None :
150+ raise AppApiException (1005 , _ ("Captcha code error or expiration" ))
151+ user = QuerySet (User ).filter (Q (username = username ,
152+ password = password_encrypt (password )) | Q (email = username ,
153+ password = password_encrypt (
154+ password ))).first ()
155+ if user is None :
156+ raise ExceptionCodeConstants .INCORRECT_USERNAME_AND_PASSWORD .value .to_app_api_exception ()
157+ if not user .is_active :
158+ raise AppApiException (1005 , _ ("The user has been disabled, please contact the administrator!" ))
159+ return user
160+
155161
156162class RegisterSerializer (ApiMixin , serializers .Serializer ):
157163 """
0 commit comments