forked from polka_billy/porridger
begin implementing async
This commit is contained in:
@ -1,54 +1,55 @@
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Annotated, Union
|
||||
import os
|
||||
|
||||
from fastapi import Depends, HTTPException, status
|
||||
from fastapi.security import OAuth2PasswordBearer
|
||||
from jose import JWTError, jwt
|
||||
from passlib.context import CryptContext
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from .db import database
|
||||
from .db import SessionLocal
|
||||
from . import orm_models, pydantic_schemas
|
||||
|
||||
|
||||
SECRET_KEY = "651a52941cf5de14d48ef5d7af115709"
|
||||
ALGORITHM = "HS256"
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES = 1440
|
||||
load_dotenv("unimportant.env")
|
||||
SECRET_KEY = os.getenv("SECRET_KEY")
|
||||
ALGORITHM = os.getenv("ALGORITHM")
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES = os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES")
|
||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/token")
|
||||
|
||||
|
||||
def get_db():
|
||||
db = database
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
async def get_session() -> AsyncSession:
|
||||
async with SessionLocal() as session:
|
||||
yield session
|
||||
|
||||
|
||||
def verify_password(plain_password, hashed_password):
|
||||
async def verify_password(plain_password, hashed_password):
|
||||
return pwd_context.verify(plain_password, hashed_password)
|
||||
|
||||
|
||||
def get_password_hash(password):
|
||||
async def get_password_hash(password):
|
||||
return pwd_context.hash(password)
|
||||
|
||||
|
||||
def get_user_by_nickname(db: Annotated[Session, Depends(get_db)], nickname: str):
|
||||
user_with_required_id = db.query(orm_models.User).filter(orm_models.User.nickname == nickname).first()
|
||||
async def get_user_by_nickname(db: Annotated[AsyncSession, Depends(get_session)], nickname: str):
|
||||
user_with_required_id = db.select(orm_models.User).where(orm_models.User.nickname == nickname).first()
|
||||
if user_with_required_id:
|
||||
return user_with_required_id
|
||||
return None
|
||||
|
||||
|
||||
def get_user_by_id(db: Annotated[Session, Depends(get_db)], user_id: int):
|
||||
user_with_required_id = db.query(orm_models.User).filter(orm_models.User.id == user_id).first()
|
||||
async def get_user_by_id(db: Annotated[AsyncSession, Depends(get_session)], user_id: int):
|
||||
user_with_required_id = db.select(orm_models.User).where(orm_models.User.id == user_id).first()
|
||||
if user_with_required_id:
|
||||
return user_with_required_id
|
||||
return None
|
||||
|
||||
|
||||
def authenticate_user(db: Annotated[Session, Depends(get_db)], nickname: str, password: str):
|
||||
async def authenticate_user(db: Annotated[AsyncSession, Depends(get_session)], nickname: str, password: str):
|
||||
user = get_user_by_nickname(db=db, nickname=nickname)
|
||||
if not user:
|
||||
return False
|
||||
@ -57,7 +58,7 @@ def authenticate_user(db: Annotated[Session, Depends(get_db)], nickname: str, pa
|
||||
return user
|
||||
|
||||
|
||||
def create_access_token(data: dict, expires_delta: Union[timedelta, None] = None):
|
||||
async def create_access_token(data: dict, expires_delta: Union[timedelta, None] = None):
|
||||
to_encode = data.copy()
|
||||
if expires_delta:
|
||||
expire = datetime.utcnow() + expires_delta
|
||||
@ -68,7 +69,7 @@ def create_access_token(data: dict, expires_delta: Union[timedelta, None] = None
|
||||
return encoded_jwt
|
||||
|
||||
|
||||
async def get_current_user(db: Annotated[Session, Depends(get_db)], token: Annotated[str, Depends(oauth2_scheme)]):
|
||||
async def get_current_user(db: Annotated[AsyncSession, Depends(get_session)], token: Annotated[str, Depends(oauth2_scheme)]):
|
||||
credentials_exception = HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Could not validate credentials",
|
||||
|
Reference in New Issue
Block a user