|
1 | | -from typing import Literal |
| 1 | +import secrets |
| 2 | +from typing import Literal, Self |
2 | 3 |
|
| 4 | +from pydantic import model_validator |
3 | 5 | from pydantic_settings import BaseSettings, SettingsConfigDict |
4 | 6 |
|
5 | 7 |
|
@@ -33,7 +35,9 @@ def database_type(self) -> str: |
33 | 35 | RTE_BASE_URL: str = "https://digital.iservices.rte-france.com" |
34 | 36 |
|
35 | 37 | # API Security |
36 | | - SECRET_KEY: str = "dev-secret-key-change-in-production" |
| 38 | + # SECRET_KEY is required in production (no default value for security) |
| 39 | + # In DEBUG mode, a random key is generated if not provided |
| 40 | + SECRET_KEY: str = "" |
37 | 41 | ALGORITHM: str = "HS256" |
38 | 42 | ACCESS_TOKEN_EXPIRE_MINUTES: int = 43200 |
39 | 43 |
|
@@ -94,5 +98,46 @@ def enedis_authorize_url(self) -> str: |
94 | 98 | return "https://mon-compte-particulier.enedis.fr/dataconnect/v1/oauth2/authorize" |
95 | 99 | return f"{self.enedis_base_url}/dataconnect/v1/oauth2/authorize" |
96 | 100 |
|
| 101 | + @model_validator(mode="after") |
| 102 | + def validate_secret_key(self) -> Self: |
| 103 | + """Validate SECRET_KEY configuration. |
| 104 | +
|
| 105 | + - Production (DEBUG=False): SECRET_KEY is required and must be secure |
| 106 | + - Development (DEBUG=True): Generate a random key if not provided (with warning) |
| 107 | + """ |
| 108 | + insecure_patterns = ["dev-", "changeme", "secret", "password", "test", "example"] |
| 109 | + |
| 110 | + if not self.SECRET_KEY: |
| 111 | + if self.DEBUG: |
| 112 | + # Generate random key for development (will change on restart) |
| 113 | + object.__setattr__(self, "SECRET_KEY", secrets.token_urlsafe(32)) |
| 114 | + import warnings |
| 115 | + warnings.warn( |
| 116 | + "SECRET_KEY not configured - using random key (sessions will be invalidated on restart). " |
| 117 | + "Set SECRET_KEY environment variable for persistent sessions.", |
| 118 | + UserWarning, |
| 119 | + stacklevel=2, |
| 120 | + ) |
| 121 | + else: |
| 122 | + raise ValueError( |
| 123 | + "SECRET_KEY environment variable is required in production (DEBUG=False). " |
| 124 | + "Generate a secure key with: python -c \"import secrets; print(secrets.token_urlsafe(32))\"" |
| 125 | + ) |
| 126 | + elif any(pattern in self.SECRET_KEY.lower() for pattern in insecure_patterns): |
| 127 | + if not self.DEBUG: |
| 128 | + raise ValueError( |
| 129 | + "SECRET_KEY appears to be insecure (contains common patterns). " |
| 130 | + "Generate a secure key with: python -c \"import secrets; print(secrets.token_urlsafe(32))\"" |
| 131 | + ) |
| 132 | + else: |
| 133 | + import warnings |
| 134 | + warnings.warn( |
| 135 | + "SECRET_KEY appears to be insecure. Use a strong random key in production.", |
| 136 | + UserWarning, |
| 137 | + stacklevel=2, |
| 138 | + ) |
| 139 | + |
| 140 | + return self |
| 141 | + |
97 | 142 |
|
98 | 143 | settings = Settings() |
0 commit comments