3131from parts .logs import Action , LogService , Module
3232from parts .urls import api
3333from utils .util_database import db
34+ from utils .util_ecdh import decrypt_ecdh_encrypted_data
3435
3536from .sms import SmsChecker
3637
@@ -39,8 +40,8 @@ class RegisterApi(Resource):
3940 def post (self ):
4041 """注册新用户账号。
4142
42- 处理用户注册请求,验证用户输入信息并创建新账号 。
43- 包括验证短信验证码、检查密码一致性、创建账号和租户等。
43+ 接收 encrypted_data 和 session_id 参数,使用 ECDH 会话密钥解密 。
44+ 请求数据应包含:name, email, phone, password, confirm_password, verify_code
4445
4546 Returns:
4647 dict: 登录成功后的令牌信息
@@ -49,25 +50,37 @@ def post(self):
4950 ValueError: 当输入信息无效或密码不一致时抛出
5051 """
5152 parser = reqparse .RequestParser ()
52- parser .add_argument ("name" , type = str , required = True , location = "json" )
53- parser .add_argument ("email" , type = EmailType , required = True , location = "json" )
54- parser .add_argument ("phone" , type = str , required = True , location = "json" )
55- parser .add_argument ("password" , type = str , required = True , location = "json" )
56- parser .add_argument (
57- "confirm_password" , type = str , required = True , location = "json"
58- )
59- parser .add_argument ("verify_code" , type = str , required = True , location = "json" )
53+ parser .add_argument ("encrypted_data" , type = str , required = True , location = "json" )
54+ parser .add_argument ("session_id" , type = str , required = True , location = "json" )
6055 body = parser .parse_args ()
6156
62- AccountService .validate_name_email_phone (body .name , body .email , body .phone )
63- if body .password != body .confirm_password :
57+ # 解密请求数据
58+ try :
59+ decrypted_data = decrypt_ecdh_encrypted_data (body .encrypted_data , body .session_id )
60+ except ValueError as e :
61+ raise ValueError (f"请求数据解密失败: { str (e )} " )
62+
63+ # 从解密后的数据中提取参数
64+ name = decrypted_data .get ("name" )
65+ email = decrypted_data .get ("email" )
66+ phone = decrypted_data .get ("phone" )
67+ password = decrypted_data .get ("password" )
68+ confirm_password = decrypted_data .get ("confirm_password" )
69+ verify_code = decrypted_data .get ("verify_code" )
70+
71+ # 验证必需参数
72+ if not all ([name , email , phone , password , confirm_password , verify_code ]):
73+ raise ValueError ("缺少必需参数:name, email, phone, password, confirm_password, verify_code" )
74+
75+ AccountService .validate_name_email_phone (name , email , phone )
76+ if password != confirm_password :
6477 raise ValueError ("两次输入的密码不相同" )
6578
6679 # 校验验证码
67- SmsChecker ("register" ).check (body . phone , body . verify_code )
80+ SmsChecker ("register" ).check (phone , verify_code )
6881
6982 account = RegisterService .register (
70- body . email , body . phone , body . name , password = body . password
83+ email , phone , name , password = password
7184 )
7285 TenantService .create_private_tenant (account )
7386
@@ -199,6 +212,9 @@ class LoginApi(Resource):
199212 def post (self ):
200213 """用户密码登录。
201214
215+ 接收 encrypted_data 和 session_id 参数,使用 ECDH 会话密钥解密。
216+ 请求数据应包含:name 或 email 或 phone(至少一个),以及 password
217+
202218 使用用户名/邮箱和密码进行身份验证并登录系统。
203219 验证成功后记录登录日志并返回访问令牌。
204220
@@ -209,16 +225,31 @@ def post(self):
209225 ValueError: 当身份验证失败时抛出
210226 """
211227 parser = reqparse .RequestParser ()
212- parser .add_argument ("name" , type = str , required = False , location = "json" )
213- parser .add_argument ("email" , type = str , required = False , location = "json" )
214- parser .add_argument ("password" , type = str , required = True , location = "json" )
215- parser .add_argument (
216- "remember_me" , type = bool , required = False , default = False , location = "json"
217- )
228+ parser .add_argument ("encrypted_data" , type = str , required = True , location = "json" )
229+ parser .add_argument ("session_id" , type = str , required = True , location = "json" )
218230 body = parser .parse_args ()
219231
232+ # 解密请求数据
233+ try :
234+ decrypted_data = decrypt_ecdh_encrypted_data (body .encrypted_data , body .session_id )
235+ except ValueError as e :
236+ raise ValueError (f"请求数据解密失败: { str (e )} " )
237+
238+ # 从解密后的数据中提取参数
239+ name = decrypted_data .get ("name" )
240+ email = decrypted_data .get ("email" )
241+ phone = decrypted_data .get ("phone" , "" )
242+ password = decrypted_data .get ("password" )
243+ remember_me = decrypted_data .get ("remember_me" , False )
244+
245+ # 验证必需参数
246+ if not password :
247+ raise ValueError ("密码不能为空" )
248+ if not any ([name , email , phone ]):
249+ raise ValueError ("必须提供用户名/邮箱/手机号" )
250+
220251 account = AccountService .authenticate_by_password (
221- body . name , body . email , "" , body . password
252+ name , email , phone , password
222253 )
223254 LogService ().add (
224255 Module .USER_MANAGEMENT ,
@@ -233,6 +264,9 @@ class LoginSmsApi(Resource):
233264 def post (self ):
234265 """短信验证码登录。
235266
267+ 接收 encrypted_data 和 session_id 参数,使用 ECDH 会话密钥解密。
268+ 请求数据应包含:phone, verify_code
269+
236270 使用手机号和短信验证码进行身份验证并登录系统。
237271 如果用户账号不存在,会缓存验证码用于后续注册流程。
238272
@@ -243,20 +277,36 @@ def post(self):
243277 ValueError: 当验证码验证失败或用户认证失败时抛出
244278 """
245279 parser = reqparse .RequestParser ()
246- parser .add_argument ("phone " , type = str , required = True , location = "json" )
247- parser .add_argument ("verify_code " , type = str , required = True , location = "json" )
280+ parser .add_argument ("encrypted_data " , type = str , required = True , location = "json" )
281+ parser .add_argument ("session_id " , type = str , required = True , location = "json" )
248282 body = parser .parse_args ()
249283
284+ # 解密请求数据
285+ try :
286+ decrypted_data = decrypt_ecdh_encrypted_data (body .encrypted_data , body .session_id )
287+ except ValueError as e :
288+ raise ValueError (f"请求数据解密失败: { str (e )} " )
289+
290+ # 从解密后的数据中提取参数
291+ phone = decrypted_data .get ("phone" )
292+ verify_code = decrypted_data .get ("verify_code" )
293+
294+ # 验证必需参数
295+ if not phone :
296+ raise ValueError ("手机号不能为空" )
297+ if not verify_code :
298+ raise ValueError ("验证码不能为空" )
299+
250300 # 校验登录验证码
251- SmsChecker ("login" ).check (body . phone , body . verify_code )
301+ SmsChecker ("login" ).check (phone , verify_code )
252302
253- account = Account .query .filter_by (phone = body . phone ).first ()
303+ account = Account .query .filter_by (phone = phone ).first ()
254304 if not account :
255305 SmsChecker ("register" ).cached_phone_code_for_registration (
256- body . phone , body . verify_code
306+ phone , verify_code
257307 )
258308
259- account = AccountService .authenticate_by_sms (body . phone , body . verify_code )
309+ account = AccountService .authenticate_by_sms (phone , verify_code )
260310
261311 LogService ().add (
262312 Module .USER_MANAGEMENT ,
0 commit comments