server/auth/
auth_setup.rs

1//! Authentication setup and global state management.
2//!
3//! This module provides functionality for setting up and managing global authentication
4//! state that can be shared between the UI and server components. It handles the
5//! coordination of authentication providers and state management across the application.
6
7use super::auth_provider::AuthProvider;
8use super::auth_state::AuthStateManager;
9use super::provider::AuthProvider as AuthProviderTrait;
10use crate::service_bus_manager::ServiceBusError;
11use std::sync::{Arc, Mutex};
12
13/// Global authentication state manager shared across the application.
14///
15/// This static variable holds the authentication state manager that can be
16/// set by the UI and used by server components for authentication operations.
17static GLOBAL_AUTH_STATE: Mutex<Option<Arc<AuthStateManager>>> = Mutex::new(None);
18
19/// Sets the global authentication state manager.
20///
21/// This function is typically called by the UI component during initialization
22/// to establish a shared authentication state that can be used by server
23/// components for authentication operations.
24///
25/// # Arguments
26///
27/// * `auth_state` - The authentication state manager to set as global
28///
29/// # Examples
30///
31/// ```no_run
32/// use quetty_server::auth::{AuthStateManager, set_global_auth_state};
33/// use std::sync::Arc;
34///
35/// let auth_state = Arc::new(AuthStateManager::new());
36/// set_global_auth_state(auth_state);
37/// ```
38pub fn set_global_auth_state(auth_state: Arc<AuthStateManager>) {
39    let mut global = GLOBAL_AUTH_STATE.lock().unwrap();
40    *global = Some(auth_state);
41}
42
43/// Creates an authentication provider that uses the global authentication state.
44///
45/// This function creates an [`AuthProvider`] that integrates with the global
46/// authentication state manager. It provides a bridge between the UI authentication
47/// state and server-side authentication operations.
48///
49/// # Arguments
50///
51/// * `fallback_provider` - Optional fallback provider to use if the global state fails
52///
53/// # Returns
54///
55/// An [`AuthProvider`] that can be used for authentication operations
56///
57/// # Errors
58///
59/// Returns [`ServiceBusError::ConfigurationError`] if:
60/// - The global authentication state has not been initialized
61/// - The global state is in an invalid state
62///
63/// # Examples
64///
65/// ```no_run
66/// use quetty_server::auth::{create_auth_provider, set_global_auth_state, AuthStateManager};
67/// use std::sync::Arc;
68///
69/// // First, initialize the global state
70/// let auth_state = Arc::new(AuthStateManager::new());
71/// set_global_auth_state(auth_state);
72///
73/// // Then create a provider that uses the global state
74/// let provider = create_auth_provider(None)?;
75/// let token = provider.authenticate().await?;
76/// ```
77pub fn create_auth_provider(
78    fallback_provider: Option<Arc<dyn AuthProviderTrait>>,
79) -> Result<Arc<dyn AuthProviderTrait>, ServiceBusError> {
80    let global = GLOBAL_AUTH_STATE.lock().unwrap();
81
82    if let Some(auth_state) = global.as_ref() {
83        Ok(Arc::new(AuthProvider::new(
84            auth_state.clone(),
85            fallback_provider,
86        )))
87    } else {
88        Err(ServiceBusError::ConfigurationError(
89            "UI auth state not initialized".to_string(),
90        ))
91    }
92}