mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-11-17 17:25:13 +00:00
refactor: Replace fallback defaults with required environment variable validation in charging-engine auth
- Change AuthConfig::from_env() return type from Self to Result<Self, String> - Remove default JWT_SECRET fallback, require environment variable to be set - Add validation requiring JWT_SECRET to be at least 32 characters for security - Replace expect() with ok_or_else in create_token timestamp calculation - Replace expect() with map_err in DATABASE_URL environment variable loading
This commit is contained in:
@@ -21,8 +21,13 @@ pub struct AuthConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl AuthConfig {
|
impl AuthConfig {
|
||||||
pub fn from_env() -> Self {
|
pub fn from_env() -> Result<Self, String> {
|
||||||
let secret = env::var("JWT_SECRET").unwrap_or_else(|_| "default_secret_key_change_in_production".to_string());
|
let secret = env::var("JWT_SECRET")
|
||||||
|
.map_err(|_| "JWT_SECRET environment variable is required".to_string())?;
|
||||||
|
|
||||||
|
if secret.len() < 32 {
|
||||||
|
return Err("JWT_SECRET must be at least 32 characters for security".to_string());
|
||||||
|
}
|
||||||
|
|
||||||
// Parse API keys from comma-separated environment variable
|
// Parse API keys from comma-separated environment variable
|
||||||
let api_keys_env = env::var("API_KEYS").unwrap_or_else(|_| String::new());
|
let api_keys_env = env::var("API_KEYS").unwrap_or_else(|_| String::new());
|
||||||
@@ -32,7 +37,7 @@ impl AuthConfig {
|
|||||||
.filter(|s| !s.is_empty())
|
.filter(|s| !s.is_empty())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
Self { secret, api_keys }
|
Ok(Self { secret, api_keys })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_api_key(&self, api_key: &str) -> bool {
|
pub fn validate_api_key(&self, api_key: &str) -> bool {
|
||||||
@@ -43,7 +48,7 @@ impl AuthConfig {
|
|||||||
pub fn create_token(user_id: &str, config: &AuthConfig) -> Result<String, StatusCode> {
|
pub fn create_token(user_id: &str, config: &AuthConfig) -> Result<String, StatusCode> {
|
||||||
let expiration = chrono::Utc::now()
|
let expiration = chrono::Utc::now()
|
||||||
.checked_add_signed(chrono::Duration::hours(24))
|
.checked_add_signed(chrono::Duration::hours(24))
|
||||||
.expect("valid timestamp")
|
.ok_or_else(|| StatusCode::INTERNAL_SERVER_ERROR)?
|
||||||
.timestamp() as usize;
|
.timestamp() as usize;
|
||||||
|
|
||||||
let claims = Claims {
|
let claims = Claims {
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ async fn main() -> Result<()> {
|
|||||||
// Initialize charging engine
|
// Initialize charging engine
|
||||||
let redis_url = std::env::var("REDIS_URI").unwrap_or_else(|_| "redis://127.0.0.1/".to_string());
|
let redis_url = std::env::var("REDIS_URI").unwrap_or_else(|_| "redis://127.0.0.1/".to_string());
|
||||||
let database_url = std::env::var("DATABASE_URL")
|
let database_url = std::env::var("DATABASE_URL")
|
||||||
.expect("DATABASE_URL is required (for rating plans)");
|
.map_err(|_| anyhow::anyhow!("DATABASE_URL environment variable is required (for rating plans)"))?;
|
||||||
let sync_interval = std::env::var("SYNC_INTERVAL")
|
let sync_interval = std::env::var("SYNC_INTERVAL")
|
||||||
.unwrap_or_else(|_| "1".to_string())
|
.unwrap_or_else(|_| "1".to_string())
|
||||||
.parse::<u64>()
|
.parse::<u64>()
|
||||||
@@ -47,7 +47,8 @@ async fn main() -> Result<()> {
|
|||||||
info!("Connected to Redis successfully");
|
info!("Connected to Redis successfully");
|
||||||
|
|
||||||
// Create application state
|
// Create application state
|
||||||
let auth_config = AuthConfig::from_env();
|
let auth_config = AuthConfig::from_env()
|
||||||
|
.map_err(|e| anyhow::anyhow!("Failed to initialize auth config: {}", e))?;
|
||||||
let state = crate::models::AppState {
|
let state = crate::models::AppState {
|
||||||
charging_engine,
|
charging_engine,
|
||||||
auth_config,
|
auth_config,
|
||||||
|
|||||||
Reference in New Issue
Block a user