create AuthManager
This commit is contained in:
8
src/repository/base.py
Normal file
8
src/repository/base.py
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
from src.core.database import Base
|
||||||
|
|
||||||
|
|
||||||
|
class BaseRepo:
|
||||||
|
model: type[Base] = None
|
||||||
|
|
||||||
|
def __init__(self, session):
|
||||||
|
self.session = session
|
||||||
@@ -1,9 +1,29 @@
|
|||||||
|
from datetime import timedelta, datetime, timezone
|
||||||
|
|
||||||
|
import jwt
|
||||||
from passlib.context import CryptContext
|
from passlib.context import CryptContext
|
||||||
|
|
||||||
|
from src.core.settings import settings
|
||||||
|
|
||||||
|
|
||||||
class AuthManger:
|
class AuthManger:
|
||||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def verify_password(cls, plain_password, hashed_password):
|
def verify_password(cls, plain_password, hashed_password):
|
||||||
return cls.pwd_context.verify(plain_password, hashed_password)
|
return cls.pwd_context.verify(plain_password, hashed_password)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_password_hash(cls, password):
|
||||||
|
return cls.pwd_context.hash(password)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def create_access_token(cls, data: dict, expires_delta: timedelta | None = None):
|
||||||
|
to_encode = data.copy()
|
||||||
|
if expires_delta:
|
||||||
|
expire = datetime.now(timezone.utc) + expires_delta
|
||||||
|
else:
|
||||||
|
expire = datetime.now(timezone.utc) + timedelta(minutes=settings.access_token.expire_minutes)
|
||||||
|
to_encode.update({"exp": expire})
|
||||||
|
encoded_jwt = jwt.encode(to_encode, settings.access_token.secret_key, algorithm=settings.access_token.algorithm)
|
||||||
|
return encoded_jwt
|
||||||
|
|||||||
Reference in New Issue
Block a user