File tree Expand file tree Collapse file tree 4 files changed +10
-6
lines changed Expand file tree Collapse file tree 4 files changed +10
-6
lines changed Original file line number Diff line number Diff line change 11#!/usr/bin/env python3
22# -*- coding: utf-8 -*-
3+ from uuid import uuid4
4+
35from fast_captcha import img_captcha
46from fastapi import APIRouter , Depends , Request
57from fastapi_limiter .depends import RateLimiter
@@ -24,11 +26,11 @@ async def get_captcha(request: Request) -> ResponseSchemaModel[GetCaptchaDetail]
2426 """
2527 img_type : str = 'base64'
2628 img , code = await run_in_threadpool (img_captcha , img_byte = img_type )
27- ip = request . state . ip
29+ uuid = str ( uuid4 ())
2830 await redis_client .set (
29- f'{ settings .CAPTCHA_LOGIN_REDIS_PREFIX } :{ ip } ' ,
31+ f'{ settings .CAPTCHA_LOGIN_REDIS_PREFIX } :{ uuid } ' ,
3032 code ,
3133 ex = settings .CAPTCHA_LOGIN_EXPIRE_SECONDS ,
3234 )
33- data = GetCaptchaDetail (image_type = img_type , image = img )
35+ data = GetCaptchaDetail (uuid = uuid , img_type = img_type , image = img )
3436 return response_base .success (data = data )
Original file line number Diff line number Diff line change 88class GetCaptchaDetail (SchemaBase ):
99 """验证码详情"""
1010
11- image_type : str = Field (description = '图片类型' )
11+ uuid : str = Field (description = '图片唯一标识' )
12+ img_type : str = Field (description = '图片类型' )
1213 image : str = Field (description = '图片内容' )
Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ class AuthSchemaBase(SchemaBase):
2222class AuthLoginParam (AuthSchemaBase ):
2323 """用户登录参数"""
2424
25+ uuid : str = Field (description = '验证码 UUID' )
2526 captcha : str = Field (description = '验证码' )
2627
2728
Original file line number Diff line number Diff line change @@ -92,12 +92,12 @@ async def login(
9292 user = None
9393 try :
9494 user = await self .user_verify (db , obj .username , obj .password )
95- captcha_code = await redis_client .get (f'{ settings .CAPTCHA_LOGIN_REDIS_PREFIX } :{ request . state . ip } ' )
95+ captcha_code = await redis_client .get (f'{ settings .CAPTCHA_LOGIN_REDIS_PREFIX } :{ obj . uuid } ' )
9696 if not captcha_code :
9797 raise errors .RequestError (msg = t ('error.captcha.expired' ))
9898 if captcha_code .lower () != obj .captcha .lower ():
9999 raise errors .CustomError (error = CustomErrorCode .CAPTCHA_ERROR )
100- await redis_client .delete (f'{ settings .CAPTCHA_LOGIN_REDIS_PREFIX } :{ request . state . ip } ' )
100+ await redis_client .delete (f'{ settings .CAPTCHA_LOGIN_REDIS_PREFIX } :{ obj . uuid } ' )
101101 await user_dao .update_login_time (db , obj .username )
102102 await db .refresh (user )
103103 access_token = await create_access_token (
You can’t perform that action at this time.
0 commit comments