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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
use crate::app::model::Model;
use crate::components::auth_popup::{AuthPopup, AuthPopupState};
use crate::components::common::{AuthActivityMsg, AzureDiscoveryMsg, ComponentId, Msg};
use crate::components::state::ComponentStateMount;
use crate::error::AppResult;
use crate::utils::auth::AuthUtils;
use tuirealm::terminal::TerminalAdapter;
impl<T> Model<T>
where
T: TerminalAdapter,
{
/// Handle authentication-related messages from the UI
///
/// Processes login, logout, and token management operations.
/// This includes triggering authentication flows, handling authentication
/// state changes, and managing authentication service lifecycle.
///
/// # Arguments
/// * `msg` - The authentication activity message to process
///
/// # Returns
/// * `Ok(Some(Msg))` - Next UI action to take
/// * `Ok(None)` - No further action needed
/// * `Err(AppError)` - Authentication operation failed
pub fn update_auth(&mut self, msg: AuthActivityMsg) -> AppResult<Option<Msg>> {
match msg {
AuthActivityMsg::Login => {
// Initiate login process
if let Some(auth_service) = &self.auth_service {
let auth_service = auth_service.clone();
self.task_manager.execute_background(async move {
auth_service.initiate_authentication().await
});
}
Ok(None)
}
AuthActivityMsg::ShowDeviceCode {
user_code,
verification_url,
message,
expires_in,
} => {
// Remove loading indicator if shown
if self.app.mounted(&ComponentId::LoadingIndicator) {
self.app
.umount(&ComponentId::LoadingIndicator)
.map_err(|e| crate::error::AppError::Component(e.to_string()))?;
}
// Calculate expiration time
let expires_at =
Some(std::time::Instant::now() + std::time::Duration::from_secs(expires_in));
// Show device code popup
let popup = AuthPopup::new(AuthPopupState::ShowingDeviceCode {
user_code,
verification_url,
message,
expires_at,
});
if self.app.mounted(&ComponentId::AuthPopup) {
self.app
.umount(&ComponentId::AuthPopup)
.map_err(|e| crate::error::AppError::Component(e.to_string()))?;
}
self.app
.mount_with_state(ComponentId::AuthPopup, popup, Vec::default())?;
self.app
.active(&ComponentId::AuthPopup)
.map_err(|e| crate::error::AppError::Component(e.to_string()))?;
// Start a timer to refresh the auth popup every second
let tx = self.state_manager.tx_to_main.clone();
tokio::spawn(async move {
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(1));
loop {
interval.tick().await;
if tx.send(Msg::Tick).is_err() {
break;
}
}
});
Ok(None)
}
AuthActivityMsg::AuthenticationSuccess => {
// Show success and close popup after a delay
if self.app.mounted(&ComponentId::AuthPopup) {
// Remount with success state
self.app
.umount(&ComponentId::AuthPopup)
.map_err(|e| crate::error::AppError::Component(e.to_string()))?;
let popup = AuthPopup::new(AuthPopupState::Success);
self.app
.mount_with_state(ComponentId::AuthPopup, popup, Vec::default())?;
}
// Schedule popup removal after 2 seconds
let tx = self.state_manager.tx_to_main.clone();
tokio::spawn(async move {
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
let _ = tx.send(Msg::AuthActivity(AuthActivityMsg::CancelAuthentication));
});
// Clear authentication flag
self.state_manager.is_authenticating = false;
// Check if we need to start Azure discovery
let config = crate::config::get_config_or_panic();
let auth_method = &config.azure_ad().auth_method;
let has_connection_string = config.servicebus().has_connection_string();
log::info!("Authentication successful! Checking next steps...");
log::info!(
"Auth method: {} ({})",
auth_method,
AuthUtils::auth_method_description(config)
);
log::info!("Has connection string: {has_connection_string}");
log::info!(
"Service bus manager initialized: {}",
self.service_bus_manager.is_some()
);
// For device code flow, check if we have all required configuration first
if AuthUtils::is_device_code_auth(config) {
let azure_ad_config = config.azure_ad();
if azure_ad_config.has_subscription_id()
&& azure_ad_config.has_resource_group()
&& azure_ad_config.has_namespace()
{
log::info!(
"Device code authentication - all Azure configuration already available, skipping discovery"
);
// All required Azure config is present, fetch connection string directly
let subscription_id = azure_ad_config
.subscription_id()
.expect("subscription_id should be present");
let resource_group = azure_ad_config
.resource_group()
.expect("resource_group should be present");
let namespace = azure_ad_config
.namespace()
.expect("namespace should be present");
// Set the selected values in state manager so discovery finalization works properly
self.state_manager.update_azure_selection(
Some(subscription_id.to_string()),
Some(resource_group.to_string()),
Some(namespace.to_string()),
);
Ok(Some(Msg::AzureDiscovery(
AzureDiscoveryMsg::FetchingConnectionString {
subscription_id: subscription_id.to_string(),
resource_group: resource_group.to_string(),
namespace: namespace.to_string(),
},
)))
} else {
log::info!("Device code authentication - starting Azure discovery flow");
Ok(Some(Msg::AzureDiscovery(AzureDiscoveryMsg::StartDiscovery)))
}
} else if AuthUtils::is_client_secret_auth(config) {
let azure_ad_config = config.azure_ad();
if azure_ad_config.has_subscription_id()
&& azure_ad_config.has_resource_group()
&& azure_ad_config.has_namespace()
{
log::info!(
"Client secret authentication - all Azure configuration already available, skipping discovery"
);
// All required Azure config is present, fetch connection string directly
let subscription_id = azure_ad_config
.subscription_id()
.expect("subscription_id should be present");
let resource_group = azure_ad_config
.resource_group()
.expect("resource_group should be present");
let namespace = azure_ad_config
.namespace()
.expect("namespace should be present");
// Set the selected values in state manager so discovery finalization works properly
self.state_manager.update_azure_selection(
Some(subscription_id.to_string()),
Some(resource_group.to_string()),
Some(namespace.to_string()),
);
Ok(Some(Msg::AzureDiscovery(
AzureDiscoveryMsg::FetchingConnectionString {
subscription_id: subscription_id.to_string(),
resource_group: resource_group.to_string(),
namespace: namespace.to_string(),
},
)))
} else {
log::info!("Client secret authentication - starting Azure discovery flow");
Ok(Some(Msg::AzureDiscovery(AzureDiscoveryMsg::StartDiscovery)))
}
} else if AuthUtils::is_connection_string_auth(config) {
// Connection string auth - check for saved queue name and load it
log::info!(
"Connection string authentication successful - checking for saved queue"
);
// Check if we have a saved queue name to auto-load
match std::env::var("SERVICEBUS__QUEUE_NAME") {
Ok(saved_queue) => {
if !saved_queue.trim().is_empty() {
log::info!("Found saved queue name '{saved_queue}' - auto-loading");
// Send queue selection message to trigger proper queue loading with statistics
return Ok(Some(Msg::QueueActivity(
crate::components::common::QueueActivityMsg::QueueSelected(
saved_queue,
),
)));
} else {
log::debug!("Saved queue name is empty");
}
}
Err(_) => {
log::debug!("No saved queue name found");
}
}
// No saved queue - show queue picker for manual entry
log::info!("No saved queue found - showing queue picker for manual entry");
return Ok(Some(Msg::QueueActivity(
crate::components::common::QueueActivityMsg::QueuesLoaded(vec![]),
)));
} else if config.servicebus().has_connection_string() {
// Other auth methods with connection string available
log::info!("Connection string available, loading namespaces directly");
self.queue_manager.load_namespaces(
crate::app::managers::state_manager::NavigationContext::Startup,
);
Ok(None)
} else if AuthUtils::supports_discovery(config) {
// Other auth methods without connection string - start discovery
log::info!(
"No connection string found, starting Azure discovery flow for {}",
AuthUtils::auth_method_description(config)
);
Ok(Some(Msg::AzureDiscovery(AzureDiscoveryMsg::StartDiscovery)))
} else {
log::warn!(
"Auth method {} does not support automatic discovery",
AuthUtils::auth_method_description(config)
);
self.queue_manager.load_namespaces(
crate::app::managers::state_manager::NavigationContext::Startup,
);
Ok(None)
}
}
AuthActivityMsg::AuthenticationFailed(error) => {
// Clear authentication flag
self.state_manager.is_authenticating = false;
// Check if the error is due to incomplete configuration
if error.contains("client ID")
|| error.contains("tenant ID")
|| error.contains("Invalid authentication request")
{
// Configuration issue - open config screen instead of showing error popup
log::info!(
"Authentication failed due to configuration issue, opening config screen"
);
return Ok(Some(Msg::ToggleConfigScreen));
}
// Show error in popup for other types of authentication failures
let popup = AuthPopup::new(AuthPopupState::Failed(error));
if self.app.mounted(&ComponentId::AuthPopup) {
self.app
.umount(&ComponentId::AuthPopup)
.map_err(|e| crate::error::AppError::Component(e.to_string()))?;
}
self.app
.mount_with_state(ComponentId::AuthPopup, popup, Vec::default())?;
self.app
.active(&ComponentId::AuthPopup)
.map_err(|e| crate::error::AppError::Component(e.to_string()))?;
Ok(None)
}
AuthActivityMsg::CancelAuthentication => {
// Close auth popup
if self.app.mounted(&ComponentId::AuthPopup) {
self.app
.umount(&ComponentId::AuthPopup)
.map_err(|e| crate::error::AppError::Component(e.to_string()))?;
// Force redraw after unmounting auth popup
self.state_manager.set_redraw(true);
}
// If we're still in the authentication phase (not logged in anywhere), close the app
if self.state_manager.is_authenticating {
log::info!("Authentication cancelled - closing application");
self.state_manager.is_authenticating = false;
return Ok(Some(Msg::AppClose));
}
// Otherwise, return focus to previous component
self.app
.active(&ComponentId::Messages)
.map_err(|e| crate::error::AppError::Component(e.to_string()))?;
Ok(None)
}
AuthActivityMsg::CopyDeviceCode => {
// Debounce rapid copy requests
let now = std::time::Instant::now();
if let Some(last_copy) = self.state_manager.last_device_code_copy {
if now.duration_since(last_copy).as_millis() < 500 {
// Ignore if less than 500ms since last copy
return Ok(None);
}
}
self.state_manager.last_device_code_copy = Some(now);
// Copy device code to clipboard
if let Some(auth_service) = &self.auth_service {
let auth_service = auth_service.clone();
let tx = self.state_manager.tx_to_main.clone();
self.task_manager.execute_background(async move {
match auth_service.get_device_code_info().await {
Some(device_info) => {
// Use copypasta to copy to clipboard
use copypasta::{ClipboardContext, ClipboardProvider};
match ClipboardContext::new() {
Ok(mut ctx) => {
if let Err(e) =
ctx.set_contents(device_info.user_code.clone())
{
let _ = tx.send(Msg::ShowError(format!(
"Failed to copy device code: {e}"
)));
} else {
log::info!("Device code copied to clipboard");
let _ = tx
.send(Msg::ShowSuccess("Code copied!".to_string()));
}
}
Err(e) => {
let _ = tx.send(Msg::ShowError(format!(
"Failed to access clipboard: {e}"
)));
}
}
}
None => {
let _ =
tx.send(Msg::ShowError("No device code available".to_string()));
}
}
Ok::<(), crate::error::AppError>(())
});
}
Ok(None)
}
AuthActivityMsg::OpenVerificationUrl => {
// Open verification URL in browser
if let Some(auth_service) = &self.auth_service {
let auth_service = auth_service.clone();
let tx = self.state_manager.tx_to_main.clone();
self.task_manager.execute_background(async move {
match auth_service.get_device_code_info().await {
Some(device_info) => {
// Use open crate to open URL in default browser
if let Err(e) = open::that(&device_info.verification_uri) {
let _ = tx.send(Msg::ShowError(format!("Failed to open URL: {e}")));
} else {
log::info!("Opened verification URL in browser");
let _ = tx.send(Msg::ShowSuccess("Browser opened! Please enter the device code to authenticate.".to_string()));
}
}
None => {
let _ = tx.send(Msg::ShowError("No verification URL available".to_string()));
}
}
Ok::<(), crate::error::AppError>(())
});
}
Ok(None)
}
AuthActivityMsg::TokenRefreshFailed(error) => {
log::error!("Token refresh failed: {error}");
// Check if it's a critical error that requires re-authentication
if error.contains("expired") || error.contains("Invalid refresh token") {
// Clear authentication state
if let Some(auth_service) = &self.auth_service {
let auth_state = auth_service.auth_state_manager();
tokio::spawn(async move {
auth_state.logout().await;
});
}
// Show error popup with re-authentication prompt
let error_msg = format!(
"Authentication token refresh failed: {error}. Please log in again."
);
// Mount error popup
self.mount_error_popup(&crate::error::AppError::Auth(error_msg.clone()))?;
// Trigger re-authentication after a short delay
let tx = self.state_manager.tx_to_main.clone();
tokio::spawn(async move {
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
let _ = tx.send(Msg::AuthActivity(AuthActivityMsg::Login));
});
} else {
// Non-critical error, just log it
log::warn!("Non-critical token refresh error: {error}");
}
Ok(None)
}
AuthActivityMsg::CreateServiceBusManager => {
log::info!("Creating Service Bus manager with connection string");
// For connection string auth, we need to create the Service Bus manager
// and proceed directly to namespace/queue selection
let config = crate::config::get_config_or_panic();
match config.servicebus().connection_string() {
Ok(Some(connection_string)) => {
// Spawn task to create Service Bus manager
let tx = self.state_manager.tx_to_main.clone();
let http_client = self.http_client.clone();
let connection_string = connection_string.to_string();
self.task_manager.execute_background(async move {
use azservicebus::{ServiceBusClient as AzureServiceBusClient, ServiceBusClientOptions};
use quetty_server::service_bus_manager::ServiceBusManager;
use std::sync::Arc;
use tokio::sync::Mutex;
match AzureServiceBusClient::new_from_connection_string(
&connection_string,
ServiceBusClientOptions::default(),
).await {
Ok(azure_service_bus_client) => {
log::info!("Service Bus client created successfully");
// Create ServiceBusManager
let config = crate::config::get_config_or_panic();
let azure_ad_config = config.azure_ad();
let statistics_config = quetty_server::service_bus_manager::azure_management_client::StatisticsConfig::new(
config.queue_stats_display_enabled(),
config.queue_stats_cache_ttl_seconds(),
config.queue_stats_use_management_api(),
);
let batch_config = config.batch();
let service_bus_manager = Arc::new(Mutex::new(ServiceBusManager::new(
Arc::new(Mutex::new(azure_service_bus_client)),
http_client,
azure_ad_config.clone(),
statistics_config,
batch_config.clone(),
connection_string,
)));
// Send the service bus manager to the model
let _ = tx.send(Msg::SetServiceBusManager(service_bus_manager));
// Send success message after manager is set
let _ = tx.send(Msg::AuthActivity(AuthActivityMsg::AuthenticationSuccess));
Ok(())
}
Err(e) => {
log::error!("Failed to create Service Bus client: {e}");
let _ = tx.send(Msg::AuthActivity(AuthActivityMsg::AuthenticationFailed(
format!("Failed to connect to Service Bus: {e}")
)));
Err(crate::error::AppError::ServiceBus(e.to_string()))
}
}
});
}
Ok(None) => {
log::error!(
"No connection string available for Service Bus manager creation"
);
return Ok(Some(Msg::AuthActivity(
AuthActivityMsg::AuthenticationFailed(
"No connection string configured".to_string(),
),
)));
}
Err(e) => {
log::error!("Failed to decrypt connection string: {e}");
return Ok(Some(Msg::AuthActivity(
AuthActivityMsg::AuthenticationFailed(format!(
"Connection string decryption failed: {e}"
)),
)));
}
}
Ok(None)
}
}
}
}