mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-31 05:47:04 -05:00
feat: Implement MFA authentication, add ride statistics model, and update various services, APIs, and tests across the application.
This commit is contained in:
@@ -21,9 +21,8 @@ Why python-decouple?
|
||||
|
||||
import logging
|
||||
import warnings
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Optional
|
||||
from decouple import config, UndefinedValueError
|
||||
|
||||
from decouple import UndefinedValueError, config
|
||||
|
||||
logger = logging.getLogger("security")
|
||||
|
||||
@@ -171,10 +170,10 @@ def validate_secret_key(secret_key: str) -> bool:
|
||||
|
||||
def get_secret(
|
||||
name: str,
|
||||
default: Optional[str] = None,
|
||||
default: str | None = None,
|
||||
required: bool = True,
|
||||
min_length: int = 0,
|
||||
) -> Optional[str]:
|
||||
) -> str | None:
|
||||
"""
|
||||
Safely retrieve a secret with validation.
|
||||
|
||||
@@ -197,11 +196,10 @@ def get_secret(
|
||||
raise ValueError(f"Required secret '{name}' is not set")
|
||||
return default
|
||||
|
||||
if value and min_length > 0:
|
||||
if not validate_secret_strength(name, value, min_length):
|
||||
if required:
|
||||
raise ValueError(f"Secret '{name}' does not meet requirements")
|
||||
return default
|
||||
if value and min_length > 0 and not validate_secret_strength(name, value, min_length):
|
||||
if required:
|
||||
raise ValueError(f"Secret '{name}' does not meet requirements")
|
||||
return default
|
||||
|
||||
return value
|
||||
|
||||
@@ -284,7 +282,7 @@ class SecretProvider:
|
||||
- Azure Key Vault
|
||||
"""
|
||||
|
||||
def get_secret(self, name: str) -> Optional[str]:
|
||||
def get_secret(self, name: str) -> str | None:
|
||||
"""Retrieve a secret by name."""
|
||||
raise NotImplementedError
|
||||
|
||||
@@ -308,7 +306,7 @@ class EnvironmentSecretProvider(SecretProvider):
|
||||
This is the fallback provider for development and simple deployments.
|
||||
"""
|
||||
|
||||
def get_secret(self, name: str) -> Optional[str]:
|
||||
def get_secret(self, name: str) -> str | None:
|
||||
"""Retrieve a secret from environment variables."""
|
||||
try:
|
||||
return config(name)
|
||||
@@ -370,7 +368,7 @@ def run_startup_validation() -> None:
|
||||
if errors:
|
||||
for error in errors:
|
||||
if debug_mode:
|
||||
warnings.warn(f"Secret validation warning: {error}")
|
||||
warnings.warn(f"Secret validation warning: {error}", stacklevel=2)
|
||||
else:
|
||||
logger.error(f"Secret validation error: {error}")
|
||||
|
||||
@@ -383,9 +381,8 @@ def run_startup_validation() -> None:
|
||||
# Validate SECRET_KEY specifically
|
||||
try:
|
||||
secret_key = config("SECRET_KEY")
|
||||
if not validate_secret_key(secret_key):
|
||||
if not debug_mode:
|
||||
raise ValueError("SECRET_KEY does not meet security requirements")
|
||||
if not validate_secret_key(secret_key) and not debug_mode:
|
||||
raise ValueError("SECRET_KEY does not meet security requirements")
|
||||
except UndefinedValueError:
|
||||
if not debug_mode:
|
||||
raise ValueError("SECRET_KEY is required in production")
|
||||
|
||||
Reference in New Issue
Block a user