security: harden data auth and deployment
This commit is contained in:
+51
-8
@@ -1,4 +1,5 @@
|
||||
from datetime import datetime, timedelta, timezone
|
||||
import uuid
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from passlib.context import CryptContext
|
||||
@@ -7,9 +8,15 @@ from jwt import InvalidTokenError
|
||||
|
||||
from .config import settings
|
||||
|
||||
_pwd_context = CryptContext(schemes=["pbkdf2_sha256"], deprecated="auto")
|
||||
_pwd_context = CryptContext(
|
||||
schemes=["argon2", "pbkdf2_sha256"],
|
||||
deprecated=["pbkdf2_sha256"],
|
||||
argon2__memory_cost=65536,
|
||||
argon2__time_cost=3,
|
||||
argon2__parallelism=4,
|
||||
)
|
||||
_ALGORITHM = "HS256"
|
||||
MIN_PASSWORD_LENGTH = 8
|
||||
MIN_PASSWORD_LENGTH = 12
|
||||
PASSWORD_POLICY_MESSAGE = f"Password must be at least {MIN_PASSWORD_LENGTH} characters."
|
||||
|
||||
|
||||
@@ -18,7 +25,17 @@ def hash_password(password: str) -> str:
|
||||
|
||||
|
||||
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
||||
return _pwd_context.verify(plain_password, hashed_password)
|
||||
try:
|
||||
return _pwd_context.verify(plain_password, hashed_password)
|
||||
except (TypeError, ValueError):
|
||||
return False
|
||||
|
||||
|
||||
def verify_and_update_password(plain_password: str, hashed_password: str) -> tuple[bool, Optional[str]]:
|
||||
try:
|
||||
return _pwd_context.verify_and_update(plain_password, hashed_password)
|
||||
except (TypeError, ValueError):
|
||||
return False, None
|
||||
|
||||
|
||||
def validate_password_policy(password: str) -> str:
|
||||
@@ -34,32 +51,58 @@ def _create_token(
|
||||
*,
|
||||
expires_at: datetime,
|
||||
token_type: str = "access",
|
||||
auth_version: int = 1,
|
||||
) -> str:
|
||||
issued_at = datetime.now(timezone.utc)
|
||||
payload: Dict[str, Any] = {
|
||||
"sub": subject,
|
||||
"role": role,
|
||||
"typ": token_type,
|
||||
"exp": expires_at,
|
||||
"iat": issued_at,
|
||||
"jti": uuid.uuid4().hex,
|
||||
"iss": settings.jwt_issuer,
|
||||
"aud": settings.jwt_audience,
|
||||
"ver": max(1, int(auth_version or 1)),
|
||||
}
|
||||
return jwt.encode(payload, settings.jwt_secret, algorithm=_ALGORITHM)
|
||||
|
||||
def create_access_token(subject: str, role: str, expires_minutes: Optional[int] = None) -> str:
|
||||
def create_access_token(
|
||||
subject: str,
|
||||
role: str,
|
||||
expires_minutes: Optional[int] = None,
|
||||
*,
|
||||
auth_version: int = 1,
|
||||
) -> str:
|
||||
if not settings.jwt_secret:
|
||||
raise ValueError("JWT_SECRET is not configured")
|
||||
minutes = expires_minutes or settings.jwt_exp_minutes
|
||||
expires = datetime.now(timezone.utc) + timedelta(minutes=minutes)
|
||||
return _create_token(subject, role, expires_at=expires, token_type="access")
|
||||
return _create_token(subject, role, expires_at=expires, token_type="access", auth_version=auth_version)
|
||||
|
||||
|
||||
def create_stream_token(subject: str, role: str, expires_seconds: int = 120) -> str:
|
||||
def create_stream_token(
|
||||
subject: str,
|
||||
role: str,
|
||||
expires_seconds: int = 120,
|
||||
*,
|
||||
auth_version: int = 1,
|
||||
) -> str:
|
||||
expires = datetime.now(timezone.utc) + timedelta(seconds=max(30, int(expires_seconds or 120)))
|
||||
return _create_token(subject, role, expires_at=expires, token_type="sse")
|
||||
return _create_token(subject, role, expires_at=expires, token_type="sse", auth_version=auth_version)
|
||||
|
||||
|
||||
def decode_token(token: str) -> Dict[str, Any]:
|
||||
if not settings.jwt_secret:
|
||||
raise ValueError("JWT_SECRET is not configured")
|
||||
return jwt.decode(token, settings.jwt_secret, algorithms=[_ALGORITHM])
|
||||
return jwt.decode(
|
||||
token,
|
||||
settings.jwt_secret,
|
||||
algorithms=[_ALGORITHM],
|
||||
audience=settings.jwt_audience,
|
||||
issuer=settings.jwt_issuer,
|
||||
options={"require": ["exp", "iat", "jti", "iss", "aud", "sub", "typ", "ver"]},
|
||||
)
|
||||
|
||||
|
||||
class TokenError(Exception):
|
||||
|
||||
Reference in New Issue
Block a user