mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-09-21 13:12:24 +00:00
Update user and login security configs (#922)
* Update user and login security configs * Optimize some code definitions * Update config comments * Update the captcha check * Update the config plugin sql scripts * Add user password history model to init * Fix some logic errors * Add last_password_changed_time to user sql * Fix user update password * Fix the dynamic config check * Update the user sql style
This commit is contained in:
@@ -20,7 +20,7 @@ async def login_swagger(
|
||||
db: CurrentSessionTransaction, obj: Annotated[HTTPBasicCredentials, Depends()]
|
||||
) -> GetSwaggerToken:
|
||||
token, user = await auth_service.swagger_login(db=db, obj=obj)
|
||||
return GetSwaggerToken(access_token=token, user=user)
|
||||
return GetSwaggerToken(access_token=token, user=user) # type: ignore
|
||||
|
||||
|
||||
@router.post(
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from uuid import uuid4
|
||||
import uuid
|
||||
|
||||
from fast_captcha import img_captcha
|
||||
from fastapi import APIRouter, Depends
|
||||
@@ -8,7 +8,9 @@ from starlette.concurrency import run_in_threadpool
|
||||
from backend.app.admin.schema.captcha import GetCaptchaDetail
|
||||
from backend.common.response.response_schema import ResponseSchemaModel, response_base
|
||||
from backend.core.conf import settings
|
||||
from backend.database.db import CurrentSession
|
||||
from backend.database.redis import redis_client
|
||||
from backend.utils.dynamic_config import load_login_config
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -18,17 +20,19 @@ router = APIRouter()
|
||||
summary='获取登录验证码',
|
||||
dependencies=[Depends(RateLimiter(times=5, seconds=10))],
|
||||
)
|
||||
async def get_captcha() -> ResponseSchemaModel[GetCaptchaDetail]:
|
||||
"""
|
||||
此接口可能存在性能损耗,尽管是异步接口,但是验证码生成是IO密集型任务,使用线程池尽量减少性能损耗
|
||||
"""
|
||||
img_type: str = 'base64'
|
||||
img, code = await run_in_threadpool(img_captcha, img_byte=img_type)
|
||||
uuid = str(uuid4())
|
||||
async def get_captcha(db: CurrentSession) -> ResponseSchemaModel[GetCaptchaDetail]:
|
||||
await load_login_config(db)
|
||||
img, code = await run_in_threadpool(img_captcha, img_byte='base64')
|
||||
captcha_uuid = str(uuid.uuid4())
|
||||
await redis_client.set(
|
||||
f'{settings.CAPTCHA_LOGIN_REDIS_PREFIX}:{uuid}',
|
||||
f'{settings.LOGIN_CAPTCHA_REDIS_PREFIX}:{captcha_uuid}',
|
||||
code,
|
||||
ex=settings.CAPTCHA_LOGIN_EXPIRE_SECONDS,
|
||||
ex=settings.LOGIN_CAPTCHA_EXPIRE_SECONDS,
|
||||
)
|
||||
data = GetCaptchaDetail(
|
||||
is_enabled=settings.LOGIN_CAPTCHA_ENABLED,
|
||||
expire_seconds=settings.LOGIN_CAPTCHA_EXPIRE_SECONDS,
|
||||
uuid=captcha_uuid,
|
||||
image=img,
|
||||
)
|
||||
data = GetCaptchaDetail(uuid=uuid, img_type=img_type, image=img)
|
||||
return response_base.success(data=data)
|
||||
|
||||
@@ -102,9 +102,7 @@ async def update_user_permission(
|
||||
async def update_user_password(
|
||||
db: CurrentSessionTransaction, request: Request, obj: ResetPasswordParam
|
||||
) -> ResponseModel:
|
||||
count = await user_service.update_password(
|
||||
db=db, user_id=request.user.id, hash_password=request.user.password, obj=obj
|
||||
)
|
||||
count = await user_service.update_password(db=db, user_id=request.user.id, obj=obj)
|
||||
if count > 0:
|
||||
return response_base.success()
|
||||
return response_base.fail()
|
||||
|
||||
Reference in New Issue
Block a user