mod claims;
mod middleware;
mod password;
pub use claims::{Claims, JwtResult};
pub use middleware::jwt_middleware;
pub use password::{hash_password, verify_password, generate_temp_password};
use crate::config::Config;
use chrono::{Duration, Utc};
use jsonwebtoken::{encode, EncodingKey, Header};
pub fn issue_jwt(sub: &str, tenant: Option<&str>) -> Result<String, String> {
let config = Config::try_get().ok_or("Config not initialized")?;
let exp_timestamp = Utc::now() + Duration::days(config.jwt.exp_days);
let claims = Claims {
sub: sub.to_string(),
tenant: tenant.map(String::from),
exp: exp_timestamp.timestamp() as usize,
};
encode(
&Header::default(),
&claims,
&EncodingKey::from_secret(config.jwt.secret.as_ref()),
)
.map_err(|_| "Failed to issue token".to_string())
}
pub fn validate_jwt(token: &str) -> Result<Claims, String> {
let config = Config::try_get().ok_or("Config not initialized")?;
claims::validate_jwt_with_config(token, config)
}