mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-09-21 21:15:13 +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:
@@ -1,16 +1,14 @@
|
||||
import json
|
||||
import uuid
|
||||
|
||||
from datetime import timedelta
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
from fastapi import Depends, HTTPException, Request
|
||||
from fastapi.security import HTTPBearer
|
||||
from fastapi.security.http import HTTPAuthorizationCredentials
|
||||
from fastapi.security.utils import get_authorization_scheme_param
|
||||
from jose import ExpiredSignatureError, JWTError, jwt
|
||||
from pwdlib import PasswordHash
|
||||
from pwdlib.hashers.bcrypt import BcryptHasher
|
||||
from pydantic_core import from_json
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
@@ -44,30 +42,6 @@ class CustomHTTPBearer(HTTPBearer):
|
||||
# JWT authorizes dependency injection
|
||||
DependsJwtAuth = Depends(CustomHTTPBearer())
|
||||
|
||||
password_hash = PasswordHash((BcryptHasher(),))
|
||||
|
||||
|
||||
def get_hash_password(password: str, salt: bytes | None) -> str:
|
||||
"""
|
||||
使用哈希算法加密密码
|
||||
|
||||
:param password: 密码
|
||||
:param salt: 盐值
|
||||
:return:
|
||||
"""
|
||||
return password_hash.hash(password, salt=salt)
|
||||
|
||||
|
||||
def password_verify(plain_password: str, hashed_password: str) -> bool:
|
||||
"""
|
||||
密码验证
|
||||
|
||||
:param plain_password: 待验证的密码
|
||||
:param hashed_password: 哈希密码
|
||||
:return:
|
||||
"""
|
||||
return password_hash.verify(plain_password, hashed_password)
|
||||
|
||||
|
||||
def jwt_encode(payload: dict[str, Any]) -> str:
|
||||
"""
|
||||
@@ -119,7 +93,7 @@ async def create_access_token(user_id: int, *, multi_login: bool, **kwargs) -> A
|
||||
:return:
|
||||
"""
|
||||
expire = timezone.now() + timedelta(seconds=settings.TOKEN_EXPIRE_SECONDS)
|
||||
session_uuid = str(uuid4())
|
||||
session_uuid = str(uuid.uuid4())
|
||||
access_token = jwt_encode({
|
||||
'session_uuid': session_uuid,
|
||||
'exp': timezone.to_utc(expire).timestamp(),
|
||||
|
||||
Reference in New Issue
Block a user