|
@ -2,8 +2,8 @@ from datetime import datetime, timedelta, timezone |
|
|
from jose import JWTError, jwt |
|
|
from jose import JWTError, jwt |
|
|
from passlib.context import CryptContext |
|
|
from passlib.context import CryptContext |
|
|
from fastapi.security import OAuth2PasswordBearer |
|
|
from fastapi.security import OAuth2PasswordBearer |
|
|
from fastapi import Depends,HTTPException,status |
|
|
|
|
|
from internal.models import TokenData,UserInDB,User |
|
|
|
|
|
|
|
|
from fastapi import Depends, HTTPException, status |
|
|
|
|
|
from internal.models import TokenData, UserInDB, User |
|
|
from internal.database import execute_query |
|
|
from internal.database import execute_query |
|
|
|
|
|
|
|
|
# openssl rand -hex 32 |
|
|
# openssl rand -hex 32 |
|
@ -15,6 +15,8 @@ pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") |
|
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/users/token") |
|
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/users/token") |
|
|
|
|
|
|
|
|
# 创建访问令牌 |
|
|
# 创建访问令牌 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_access_token(data: dict, expires_delta: timedelta): |
|
|
def create_access_token(data: dict, expires_delta: timedelta): |
|
|
to_encode = data.copy() |
|
|
to_encode = data.copy() |
|
|
expire = datetime.now(timezone.utc) + expires_delta |
|
|
expire = datetime.now(timezone.utc) + expires_delta |
|
@ -23,12 +25,15 @@ def create_access_token(data: dict, expires_delta: timedelta): |
|
|
return encoded_jwt |
|
|
return encoded_jwt |
|
|
|
|
|
|
|
|
# 从数据库获取信息 |
|
|
# 从数据库获取信息 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_user(username: str): |
|
|
def get_user(username: str): |
|
|
query = "SELECT * FROM users WHERE username = %s" |
|
|
query = "SELECT * FROM users WHERE username = %s" |
|
|
result = execute_query(query, (username,), fetchall=False) |
|
|
result = execute_query(query, (username,), fetchall=False) |
|
|
if result: |
|
|
if result: |
|
|
return UserInDB(**result) |
|
|
return UserInDB(**result) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_current_user(token: str = Depends(oauth2_scheme)): |
|
|
async def get_current_user(token: str = Depends(oauth2_scheme)): |
|
|
credentials_exception = HTTPException( |
|
|
credentials_exception = HTTPException( |
|
|
status_code=status.HTTP_401_UNAUTHORIZED, |
|
|
status_code=status.HTTP_401_UNAUTHORIZED, |
|
@ -49,20 +54,28 @@ async def get_current_user(token: str = Depends(oauth2_scheme)): |
|
|
return user |
|
|
return user |
|
|
|
|
|
|
|
|
# 验证用户是否为活跃用户 |
|
|
# 验证用户是否为活跃用户 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_current_active_user(current_user: User = Depends(get_current_user)): |
|
|
async def get_current_active_user(current_user: User = Depends(get_current_user)): |
|
|
if current_user.disabled: |
|
|
if current_user.disabled: |
|
|
raise HTTPException(status_code=400, detail="Inactive user") |
|
|
raise HTTPException(status_code=400, detail="Inactive user") |
|
|
return current_user |
|
|
return current_user |
|
|
|
|
|
|
|
|
# 验证密码 |
|
|
# 验证密码 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def verify_password(plain_password, hashed_password): |
|
|
def verify_password(plain_password, hashed_password): |
|
|
return pwd_context.verify(plain_password, hashed_password) |
|
|
return pwd_context.verify(plain_password, hashed_password) |
|
|
|
|
|
|
|
|
# 获取密码哈希 |
|
|
# 获取密码哈希 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_password_hash(password): |
|
|
def get_password_hash(password): |
|
|
return pwd_context.hash(password) |
|
|
return pwd_context.hash(password) |
|
|
|
|
|
|
|
|
# 验证用户密码 |
|
|
# 验证用户密码 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def authenticate_user(username: str, password: str): |
|
|
def authenticate_user(username: str, password: str): |
|
|
user = get_user(username) |
|
|
user = get_user(username) |
|
|
if not user: |
|
|
if not user: |
|
|