coder160
feat: implement complete FastAPI backend with MongoDB, JWT auth, Facebook webhook
64b23a4
Raw
History Blame Contribute Delete
1.71 kB
from datetime import datetime, timedelta, timezone
from typing import Any
from jose import JWTError, jwt
from passlib.context import CryptContext
from app.core.config import settings
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: timedelta | None = None,
) -> str:
to_encode = data.copy()
expire = datetime.now(timezone.utc) + (
expires_delta
if expires_delta is not None
else timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
)
to_encode["exp"] = expire
return jwt.encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
def create_llave_token(user_id: str, token_string: str) -> str:
expire = datetime.now(timezone.utc) + timedelta(
minutes=settings.LLAVE_EXPIRE_MINUTES
)
to_encode = {
"sub": user_id,
"token_string": token_string,
"type": "llave",
"exp": expire,
}
return jwt.encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
def decode_token(token: str) -> dict[str, Any]:
return jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM])
def decode_token_unverified(token: str) -> dict[str, Any]:
"""Decode without raising on expiry – use only for diagnostics."""
return jwt.decode(
token,
settings.SECRET_KEY,
algorithms=[settings.ALGORITHM],
options={"verify_exp": False},
)