Download backend/core/security.py from gavinder/ai-decision-intelligence-platform: direct link, hf CLI and curl.
- Browser
- Download file 1.15 kB
-
https://huggingface.co/spaces/gavinder/ai-decision-intelligence-platform/resolve/main/backend/core/security.py
- Command line
-
hf download hf://spaces/gavinder/ai-decision-intelligence-platform/backend/core/security.py
-
curl -L -o security.py https://huggingface.co/spaces/gavinder/ai-decision-intelligence-platform/resolve/main/backend/core/security.py
1.15 kB
| """Authentication and password utilities.""" | |
| from datetime import datetime, timedelta, timezone | |
| from typing import Any | |
| from jose import jwt | |
| from passlib.context import CryptContext | |
| from backend.core.config import get_settings | |
| # PBKDF2 avoids bcrypt's 72-byte password limit and external backend quirks. | |
| pwd_context = CryptContext(schemes=["pbkdf2_sha256"], deprecated="auto") | |
| ALGORITHM = "HS256" | |
| def verify_password(plain_password: str, hashed_password: str) -> bool: | |
| """Verify a plaintext password against a hash.""" | |
| return pwd_context.verify(plain_password, hashed_password) | |
| def get_password_hash(password: str) -> str: | |
| """Hash a password for storage.""" | |
| return pwd_context.hash(password) | |
| def create_access_token(subject: str, expires_delta: timedelta | None = None) -> str: | |
| """Create a signed JWT access token.""" | |
| settings = get_settings() | |
| expire = datetime.now(timezone.utc) + ( | |
| expires_delta or timedelta(minutes=settings.access_token_expire_minutes) | |
| ) | |
| to_encode: dict[str, Any] = {"sub": subject, "exp": expire} | |
| return jwt.encode(to_encode, settings.secret_key, algorithm=ALGORITHM) | |