#!/usr/bin/env python # -*- coding: utf-8 -*- import datetime import json from typing import Any, Union import jwt from passlib.context import CryptContext from core.config import settings ALGORITHM: str = "HS256" pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto", bcrypt__default_rounds=8) async def create_token(subject: Union[str, Any], expires_delta: datetime.timedelta = None) -> str: if expires_delta: expire = datetime.datetime.now() + expires_delta else: expire = datetime.datetime.now() + datetime.timedelta( minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES) to_encoded = {"exp": expire, "sub": json.dumps(subject)} encoded_jwt = jwt.encode(to_encoded, settings.SECRET_KEY, algorithm=ALGORITHM) return encoded_jwt async def verify_password(plain_password: str, hashed_password: str): return pwd_context.verify(plain_password, hashed_password) def hashed_password(password: str) -> str: return pwd_context.hash(password)