1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use ;
use ;
use AuthType;
use crateServiceBusError;
use async_trait;
use Arc;
/// 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`](crate::auth::provider::AuthProvider) for the base trait.
///
/// # Architecture
///
/// The [`AuthProvider`] implements a hierarchical authentication strategy:
///
/// 1. **UI State Priority** - First checks for valid tokens from UI authentication
/// 2. **State-based Authentication** - Uses centralized authentication state management
/// 3. **Fallback Provider** - Falls back to alternative authentication methods if available
/// 4. **Error Propagation** - Provides detailed error feedback for authentication failures
///
/// # Authentication Flow
///
/// ```no_run
/// 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`].
///
/// ```no_run
/// 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`](crate::service_bus_manager::ServiceBusError) variants.
/// - **Token Refresh Failures** - Graceful handling of token expiration scenarios