server/auth/mod.rs
1//! # Authentication Module
2//!
3//! Comprehensive authentication system for Azure Service Bus operations supporting
4//! multiple authentication methods and providers. This module provides a flexible
5//! architecture that can handle various Azure authentication scenarios.
6//!
7//! ## Supported Authentication Methods
8//!
9//! ### Azure Active Directory (Azure AD)
10//! - **Device Code Flow** - Interactive authentication for CLI applications
11//! - **Client Credentials Flow** - Service principal authentication for automated scenarios
12//!
13//! ### Connection String Authentication
14//! - **Shared Access Signature (SAS)** - Token-based authentication using connection strings
15//! - **Automatic SAS Token Generation** - Time-limited tokens with configurable expiration
16//!
17//! ## Architecture Overview
18//!
19//! The authentication system is built around several key components:
20//!
21//! - **[`AuthProvider`]** - Core trait defining the authentication interface
22//! - **[`AuthStateManager`]** - Centralized state management for authentication
23//! - **[`TokenCache`]** - Efficient caching with automatic expiration handling
24//! - **[`TokenRefreshService`]** - Background token refresh for long-running operations
25//!
26//! ## Authentication Providers
27//!
28//! ### Azure AD Provider
29//! ```no_run
30//! use quetty_server::auth::{AzureAdProvider, AzureAdAuthConfig};
31//!
32//! let config = AzureAdAuthConfig {
33//! auth_method: "device_code".to_string(),
34//! tenant_id: Some("your-tenant-id".to_string()),
35//! client_id: Some("your-client-id".to_string()),
36//! ..Default::default()
37//! };
38//!
39//! let provider = AzureAdProvider::new(config, http_client)?;
40//! let token = provider.authenticate().await?;
41//! ```
42//!
43//! ### Connection String Provider
44//! ```no_run
45//! use quetty_server::auth::{ConnectionStringProvider, ConnectionStringConfig};
46//!
47//! let config = ConnectionStringConfig {
48//! value: "Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=...".to_string(),
49//! };
50//!
51//! let provider = ConnectionStringProvider::new(config)?;
52//! let token = provider.authenticate().await?;
53//! ```
54//!
55//! ## State Management
56//!
57//! The [`AuthStateManager`] provides centralized authentication state:
58//!
59//! ```no_run
60//! use quetty_server::auth::AuthStateManager;
61//! use std::sync::Arc;
62//!
63//! let auth_manager = Arc::new(AuthStateManager::new());
64//!
65//! // Check authentication status
66//! if auth_manager.is_authenticated().await {
67//! println!("Already authenticated");
68//! }
69//!
70//! // Start automatic token refresh
71//! auth_manager.clone().start_refresh_service().await;
72//! ```
73//!
74//! ## Token Caching
75//!
76//! Automatic token caching with expiration management:
77//!
78//! ```no_run
79//! use quetty_server::auth::TokenCache;
80//!
81//! let cache = TokenCache::new();
82//!
83//! // Check if token needs refresh
84//! if cache.needs_refresh("user_token").await {
85//! // Refresh token...
86//! }
87//! ```
88//!
89//! ## Integration with Service Bus
90//!
91//! The authentication system integrates seamlessly with Service Bus operations:
92//!
93//! ```no_run
94//! use quetty_server::auth::{create_service_bus_auth_provider, get_azure_ad_token_with_auth};
95//!
96//! // Create provider for Service Bus
97//! let provider = create_service_bus_auth_provider(
98//! "azure_ad",
99//! None,
100//! &azure_config,
101//! http_client
102//! )?;
103//!
104//! // Get token for operations
105//! let token = get_azure_ad_token_with_auth(&provider).await?;
106//! ```
107
108pub mod auth_provider;
109pub mod auth_setup;
110pub mod auth_state;
111pub mod azure_ad;
112pub mod connection_string;
113pub mod errors;
114pub mod provider;
115pub mod sas_token_generator;
116pub mod service_bus_auth;
117pub mod token_cache;
118pub mod token_refresh_service;
119pub mod types;
120
121pub use auth_setup::{create_auth_provider, set_global_auth_state};
122pub use auth_state::{AuthStateManager, AuthenticationState};
123pub use azure_ad::{AzureAdProvider, DeviceCodeFlowInfo};
124pub use connection_string::ConnectionStringProvider;
125pub use errors::TokenRefreshError;
126pub use provider::{AuthProvider, AuthToken};
127pub use sas_token_generator::SasTokenGenerator;
128pub use service_bus_auth::{
129 create_auth_provider as create_service_bus_auth_provider, get_azure_ad_token_with_auth,
130};
131pub use token_cache::TokenCache;
132pub use token_refresh_service::TokenRefreshService;
133pub use types::{AuthConfig, AuthType, DeviceCodeInfo};