pub mod jwt_server {
use crate::errors::Result;
use crate::storage::AuthStorage;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct JwtServerConfig {
pub issuer: String,
pub key_id: String,
}
impl Default for JwtServerConfig {
fn default() -> Self {
Self {
issuer: "https://auth.example.com".to_string(),
key_id: "default".to_string(),
}
}
}
pub struct JwtServer {
config: JwtServerConfig,
storage: Arc<dyn AuthStorage>,
}
impl JwtServer {
pub async fn new(config: JwtServerConfig, storage: Arc<dyn AuthStorage>) -> Result<Self> {
Ok(Self { config, storage })
}
pub async fn initialize(&self) -> Result<()> {
Ok(())
}
pub async fn get_well_known_jwt_configuration(&self) -> Result<JwtWellKnownConfiguration> {
Ok(JwtWellKnownConfiguration {
issuer: self.config.issuer.clone(),
jwks_uri: format!("{}/jwks", self.config.issuer),
})
}
pub async fn store_jwt_metadata(&self, metadata: &JwtWellKnownConfiguration) -> Result<()> {
let key = format!("jwt_metadata:{}", self.config.issuer);
let value = serde_json::to_string(metadata).map_err(|e| {
crate::errors::AuthError::internal(format!("Serialization error: {}", e))
})?;
self.storage.store_kv(&key, value.as_bytes(), None).await?;
log::info!("Stored JWT metadata for issuer: {}", self.config.issuer);
Ok(())
}
pub async fn get_stored_metadata(&self) -> Result<Option<JwtWellKnownConfiguration>> {
let key = format!("jwt_metadata:{}", self.config.issuer);
if let Some(value_bytes) = self.storage.get_kv(&key).await? {
let value = String::from_utf8(value_bytes).map_err(|e| {
crate::errors::AuthError::internal(format!("UTF-8 conversion error: {}", e))
})?;
let metadata: JwtWellKnownConfiguration =
serde_json::from_str(&value).map_err(|e| {
crate::errors::AuthError::internal(format!("Deserialization error: {}", e))
})?;
Ok(Some(metadata))
} else {
Ok(None)
}
}
pub async fn store_signing_key(&self, key_data: &str) -> Result<()> {
let key = format!("jwt_key:{}", self.config.key_id);
self.storage
.store_kv(&key, key_data.as_bytes(), None)
.await?;
log::info!("Stored JWT signing key: {}", self.config.key_id);
Ok(())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JwtWellKnownConfiguration {
pub issuer: String,
pub jwks_uri: String,
}
}
pub mod api_gateway {
use crate::errors::Result;
use crate::storage::AuthStorage;
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct ApiGatewayConfig {
pub name: String,
}
impl Default for ApiGatewayConfig {
fn default() -> Self {
Self {
name: "API Gateway".to_string(),
}
}
}
pub struct ApiGateway {
config: ApiGatewayConfig,
storage: Arc<dyn AuthStorage>,
}
impl ApiGateway {
pub async fn new(config: ApiGatewayConfig, storage: Arc<dyn AuthStorage>) -> Result<Self> {
Ok(Self { config, storage })
}
pub async fn initialize(&self) -> Result<()> {
Ok(())
}
pub async fn store_gateway_metadata(&self) -> Result<()> {
let key = format!("api_gateway_config:{}", self.config.name);
let metadata = serde_json::json!({
"name": self.config.name,
"initialized_at": chrono::Utc::now().to_rfc3339()
});
let value = serde_json::to_string(&metadata).map_err(|e| {
crate::errors::AuthError::internal(format!("Serialization error: {}", e))
})?;
self.storage.store_kv(&key, value.as_bytes(), None).await?;
log::info!("Stored API Gateway metadata for: {}", self.config.name);
Ok(())
}
pub async fn store_route_config(&self, route_path: &str, config_data: &str) -> Result<()> {
let key = format!("api_gateway_route:{}:{}", self.config.name, route_path);
self.storage
.store_kv(&key, config_data.as_bytes(), None)
.await?;
log::info!(
"Stored route config for {} on gateway: {}",
route_path,
self.config.name
);
Ok(())
}
pub async fn get_route_config(&self, route_path: &str) -> Result<Option<String>> {
let key = format!("api_gateway_route:{}:{}", self.config.name, route_path);
if let Some(config_bytes) = self.storage.get_kv(&key).await? {
let config = String::from_utf8(config_bytes).map_err(|e| {
crate::errors::AuthError::internal(format!("UTF-8 conversion error: {}", e))
})?;
Ok(Some(config))
} else {
Ok(None)
}
}
pub fn get_gateway_name(&self) -> &str {
&self.config.name
}
}
}
pub mod saml_idp {
use crate::errors::Result;
use crate::storage::AuthStorage;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct SamlIdpConfig {
pub entity_id: String,
}
impl Default for SamlIdpConfig {
fn default() -> Self {
Self {
entity_id: "https://auth.example.com".to_string(),
}
}
}
pub struct SamlIdentityProvider {
config: SamlIdpConfig,
storage: Arc<dyn AuthStorage>,
}
impl SamlIdentityProvider {
pub async fn new(config: SamlIdpConfig, storage: Arc<dyn AuthStorage>) -> Result<Self> {
Ok(Self { config, storage })
}
pub async fn initialize(&self) -> Result<()> {
Ok(())
}
pub async fn get_metadata(&self) -> Result<SamlMetadata> {
Ok(SamlMetadata {
entity_id: self.config.entity_id.clone(),
})
}
pub async fn store_saml_metadata(&self, metadata: &SamlMetadata) -> Result<()> {
let key = format!("saml_metadata:{}", self.config.entity_id);
let value = serde_json::to_string(metadata).map_err(|e| {
crate::errors::AuthError::internal(format!("Serialization error: {}", e))
})?;
self.storage.store_kv(&key, value.as_bytes(), None).await?;
log::info!("Stored SAML metadata for entity: {}", self.config.entity_id);
Ok(())
}
pub async fn store_assertion(
&self,
assertion_id: &str,
assertion_data: &str,
) -> Result<()> {
let key = format!("saml_assertion:{}:{}", self.config.entity_id, assertion_id);
self.storage
.store_kv(
&key,
assertion_data.as_bytes(),
Some(std::time::Duration::from_secs(3600)),
)
.await?;
log::info!(
"Stored SAML assertion {} for entity: {}",
assertion_id,
self.config.entity_id
);
Ok(())
}
pub async fn get_assertion(&self, assertion_id: &str) -> Result<Option<String>> {
let key = format!("saml_assertion:{}:{}", self.config.entity_id, assertion_id);
if let Some(assertion_bytes) = self.storage.get_kv(&key).await? {
let assertion = String::from_utf8(assertion_bytes).map_err(|e| {
crate::errors::AuthError::internal(format!("UTF-8 conversion error: {}", e))
})?;
Ok(Some(assertion))
} else {
Ok(None)
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SamlMetadata {
pub entity_id: String,
}
}
pub mod consent {
}
pub mod introspection {
}
pub mod device_flow_server {
}