33 lines
879 B
Python
33 lines
879 B
Python
|
|
from datetime import datetime, timedelta
|
||
|
|
from typing import Any, Dict, Optional
|
||
|
|
|
||
|
|
from jose import jwt
|
||
|
|
from passlib.context import CryptContext
|
||
|
|
|
||
|
|
from .. import config
|
||
|
|
|
||
|
|
|
||
|
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||
|
|
|
||
|
|
|
||
|
|
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
||
|
|
return pwd_context.verify(plain_password, hashed_password)
|
||
|
|
|
||
|
|
|
||
|
|
def get_password_hash(password: str) -> str:
|
||
|
|
return pwd_context.hash(password)
|
||
|
|
|
||
|
|
|
||
|
|
def create_access_token(
|
||
|
|
data: Dict[str, Any], expires_delta: Optional[timedelta] = None
|
||
|
|
) -> str:
|
||
|
|
to_encode = data.copy()
|
||
|
|
expire = datetime.utcnow() + (
|
||
|
|
expires_delta or timedelta(minutes=config.ACCESS_TOKEN_EXPIRE_MINUTES)
|
||
|
|
)
|
||
|
|
to_encode.update({"exp": expire})
|
||
|
|
encoded_jwt = jwt.encode(
|
||
|
|
to_encode, config.JWT_SECRET_KEY, algorithm=config.JWT_ALGORITHM
|
||
|
|
)
|
||
|
|
return encoded_jwt
|