pub struct AuthProvider { /* private fields */ }Expand description
Authentication provider that integrates with UI authentication state.
Provides a bridge between the server-side authentication system and the UI authentication state, enabling seamless authentication flow coordination between the terminal interface and Azure Service Bus operations.
This provider prioritizes UI-managed authentication tokens and provides fallback mechanisms for automated scenarios where UI authentication is not available.
See also: AuthStateManager for state management, AuthProvider for the base trait.
§Architecture
The AuthProvider implements a hierarchical authentication strategy:
- UI State Priority - First checks for valid tokens from UI authentication
- State-based Authentication - Uses centralized authentication state management
- Fallback Provider - Falls back to alternative authentication methods if available
- Error Propagation - Provides detailed error feedback for authentication failures
§Authentication Flow
use quetty_server::auth::{AuthProvider, AuthStateManager};
use std::sync::Arc;
// Create with UI authentication state integration
let auth_state = Arc::new(AuthStateManager::new());
let provider = AuthProvider::new(auth_state, None);
// Authenticate using UI state or fallback methods
match provider.authenticate().await {
Ok(token) => {
println!("Authentication successful: {}", token.token);
// Use token for Service Bus operations
}
Err(e) => eprintln!("Authentication failed: {}", e),
}§Integration with UI Authentication
This provider seamlessly integrates with UI authentication flows:
- Device Code Flow - Coordinates with UI device code authentication
- Token Management - Uses UI-managed token cache and refresh logic
- State Synchronization - Maintains consistency between UI and server authentication
- Error Handling - Provides user-friendly error messages for UI display
§Fallback Authentication
When UI authentication is not available, the provider can use fallback methods:
For more details on fallback providers, see [ConnectionStringProvider].
use quetty_server::auth::{AuthProvider, AuthStateManager, ConnectionStringProvider};
use std::sync::Arc;
// Create fallback provider for automated scenarios
let connection_provider = Arc::new(ConnectionStringProvider::new(config)?);
let auth_state = Arc::new(AuthStateManager::new());
let provider = AuthProvider::new(
auth_state,
Some(connection_provider as Arc<dyn AuthProviderTrait>)
);
// Will use UI state if available, otherwise fall back to connection string
let token = provider.authenticate().await?;§Thread Safety
The provider is designed for concurrent access and can be safely shared across multiple threads and async tasks. All internal state is protected by appropriate synchronization mechanisms.
§Error Handling
Provides comprehensive error handling for various authentication scenarios:
- Not Authenticated - Clear guidance for users to authenticate through UI
- Authentication in Progress - Informative messages during device code flow
- Authentication Failed - Detailed error information from underlying providers
All errors are returned as ServiceBusError variants.
- Token Refresh Failures - Graceful handling of token expiration scenarios
Implementations§
Source§impl AuthProvider
impl AuthProvider
Sourcepub fn new(
auth_state: Arc<AuthStateManager>,
fallback_provider: Option<Arc<dyn AuthProviderTrait>>,
) -> Self
pub fn new( auth_state: Arc<AuthStateManager>, fallback_provider: Option<Arc<dyn AuthProviderTrait>>, ) -> Self
Creates a new authentication provider with UI state integration.
§Arguments
auth_state- Shared authentication state manager for UI coordinationfallback_provider- Optional fallback provider for automated scenarios
§Returns
A new AuthProvider instance ready for authentication operations
§Examples
use quetty_server::auth::{AuthProvider, AuthStateManager};
use std::sync::Arc;
// Basic provider with UI state only
let auth_state = Arc::new(AuthStateManager::new());
let provider = AuthProvider::new(auth_state, None);
// Provider with fallback for automated scenarios
let auth_state = Arc::new(AuthStateManager::new());
let fallback = Arc::new(connection_provider);
let provider = AuthProvider::new(auth_state, Some(fallback));