passless-rs 0.17.0

FIDO2 security token emulator
Documentation
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
//! Desktop notification handling for user presence and verification
//!
//! This module provides desktop notification support with compatibility for
//! different notification servers (notify-osd, mako, Dunst, etc.).

use std::sync::{Arc, Mutex};

use log::{debug, info, warn};
use notify_rust::{Notification, Timeout, Urgency};

/// Result of user interaction via notification
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NotificationResult {
    /// User approved the operation
    Accepted,
    /// User denied the operation
    Denied,
}

/// Result of a yes/no question via notification
pub type YesNoResult = NotificationResult;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum PromptKind {
    UserPresence,
    UserVerification,
    YesNo,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum NotificationActionMode {
    Explicit,
    Default,
    DunstDefault,
}

impl NotificationActionMode {
    fn uses_default_action(self) -> bool {
        matches!(self, Self::Default | Self::DunstDefault)
    }
}

/// Determine how actions should be exposed for a notification server.
///
/// Legacy compatibility servers use a default action for all prompt types.
/// Dunst gets that behavior only for CTAP user presence: its actions are
/// supported but normally invoked through Dunst's interaction model instead
/// of visible buttons. User verification deliberately keeps explicit actions
/// so the UP compatibility path cannot silently weaken UV semantics.
fn action_mode_for_server(
    server_name: &str,
    server_version: &str,
    prompt_kind: PromptKind,
) -> NotificationActionMode {
    let server_name = server_name.to_lowercase();

    match (server_name.as_str(), server_version) {
        ("notify-osd", "1.0") | ("mako", "0.0.0") | ("quickshell", "") => {
            NotificationActionMode::Default
        }
        ("dunst", _) if prompt_kind == PromptKind::UserPresence => {
            NotificationActionMode::DunstDefault
        }
        _ => NotificationActionMode::Explicit,
    }
}

fn notification_action_mode(prompt_kind: PromptKind) -> NotificationActionMode {
    notify_rust::get_server_information()
        .map(|server| {
            let mode = action_mode_for_server(&server.name, &server.version, prompt_kind);
            debug!(
                "Notification server: {} (version: {})",
                server.name, server.version
            );

            match mode {
                NotificationActionMode::Default => {
                    info!("Detected {} - using default action mode", server.name);
                }
                NotificationActionMode::DunstDefault => {
                    info!("Detected Dunst - using UP-specific default action mode");
                }
                NotificationActionMode::Explicit => {}
            }

            mode
        })
        .unwrap_or_else(|e| {
            warn!("Failed to get notification server info: {}", e);
            NotificationActionMode::Explicit
        })
}

fn action_is_accepted(
    action: &str,
    affirmative_action: &str,
    action_mode: NotificationActionMode,
) -> bool {
    if action == affirmative_action {
        return true;
    }

    action == "default" && action_mode.uses_default_action()
}

fn show_confirmation_notification(
    operation: &str,
    relying_party: Option<&str>,
    user: Option<&str>,
    timeout_seconds: u32,
    prompt_kind: PromptKind,
) -> Result<NotificationResult, String> {
    debug_assert!(prompt_kind != PromptKind::YesNo);

    let action_mode = notification_action_mode(prompt_kind);

    let mut message = format!("Operation: {}", operation);
    if let Some(rp) = relying_party {
        message.push_str(&format!("\nRelying Party: {}", rp));
    }
    if let Some(user) = user {
        message.push_str(&format!("\nUser: {}", user));
    }

    if action_mode == NotificationActionMode::DunstDefault {
        message.push_str(
            "\n\nDunst: confirm by invoking this notification's action \
             (middle-click by default). Closing the notification denies the request.",
        );
    }

    let summary = match prompt_kind {
        PromptKind::UserPresence => "👆 User Presence Required",
        PromptKind::UserVerification => "🔒 User Verification Required",
        PromptKind::YesNo => unreachable!("yes/no prompts use show_yes_no_notification"),
    };

    info!("Showing {} notification", summary);

    let action_result = Arc::new(Mutex::new(None));
    let action_result_clone = action_result.clone();

    let mut notification = Notification::new();
    notification
        .summary(summary)
        .body(&message)
        .icon("security-high")
        .timeout(Timeout::Milliseconds(timeout_seconds * 1000))
        .urgency(Urgency::Critical);

    if action_mode.uses_default_action() {
        notification.action("default", "");
    } else {
        notification.action("approve", "Accept");
        notification.action("deny", "Deny");
    }

    let handle = notification
        .show()
        .map_err(|e| format!("Failed to show notification: {}", e))?;

    handle.wait_for_action(|action| {
        debug!("User action received: {}", action);
        let mut result = action_result_clone
            .lock()
            .expect("Failed to lock action result");
        *result = Some(action.to_string());
    });

    let action = action_result
        .lock()
        .expect("Failed to lock action result")
        .clone()
        .unwrap_or_else(|| "__closed".to_string());

    if action_is_accepted(&action, "approve", action_mode) {
        info!("Notification accepted");
        Ok(NotificationResult::Accepted)
    } else {
        if action != "deny" && action != "__closed" {
            debug!("Unknown action '{}' - treating as denied", action);
        }
        info!("Notification denied or closed");
        Ok(NotificationResult::Denied)
    }
}

/// Show a CTAP user-presence notification and wait for response.
///
/// User presence is a consent gesture (for example, touching a hardware key),
/// so notification activation may be used as the explicit gesture on daemons
/// such as Dunst that do not expose action buttons directly.
pub fn show_user_presence_notification(
    operation: &str,
    relying_party: Option<&str>,
    user: Option<&str>,
    timeout_seconds: u32,
) -> Result<NotificationResult, String> {
    show_confirmation_notification(
        operation,
        relying_party,
        user,
        timeout_seconds,
        PromptKind::UserPresence,
    )
}

/// Show a user-verification notification and wait for response.
///
/// Unlike user presence, Dunst activation is not treated as verification: the
/// user must invoke the explicit Accept action through Dunst's action UI.
pub fn show_verification_notification(
    operation: &str,
    relying_party: Option<&str>,
    user: Option<&str>,
    timeout_seconds: u32,
) -> Result<NotificationResult, String> {
    show_confirmation_notification(
        operation,
        relying_party,
        user,
        timeout_seconds,
        PromptKind::UserVerification,
    )
}

/// Show a yes/no question notification and wait for response
///
/// # Arguments
///
/// * `title` - Title of the notification
/// * `question` - The question to ask
///
/// # Returns
///
/// Result indicating whether the user answered yes (Accepted) or no (Denied)
pub fn show_yes_no_notification(title: &str, question: &str) -> Result<YesNoResult, String> {
    info!("Showing yes/no notification: {}", title);

    let action_mode = notification_action_mode(PromptKind::YesNo);

    let action_result = Arc::new(Mutex::new(None));
    let action_result_clone = action_result.clone();

    let mut notification = Notification::new();
    notification
        .summary(title)
        .body(question)
        .icon("dialog-question")
        .timeout(Timeout::Never)
        .urgency(Urgency::Critical);

    if action_mode.uses_default_action() {
        notification.action("default", "");
    } else {
        notification.action("yes", "Yes");
        notification.action("no", "No");
    }

    let handle = notification
        .show()
        .map_err(|e| format!("Failed to show notification: {}", e))?;

    handle.wait_for_action(|action| {
        debug!("User action received: {}", action);
        let mut result = action_result_clone
            .lock()
            .expect("Failed to lock action result");
        *result = Some(action.to_string());
    });

    let action = action_result
        .lock()
        .expect("Failed to lock action result")
        .clone()
        .unwrap_or_else(|| "__closed".to_string());

    if action_is_accepted(&action, "yes", action_mode) {
        info!("User answered yes");
        Ok(YesNoResult::Accepted)
    } else {
        if action != "no" && action != "__closed" {
            debug!("Unknown action '{}' - treating as no", action);
        }
        info!("User answered no or closed notification");
        Ok(YesNoResult::Denied)
    }
}

/// Show an informational notification (no user response needed)
///
/// # Arguments
///
/// * `title` - Title of the notification
/// * `message` - The message to display
///
/// # Returns
///
/// Ok if notification was shown successfully
pub fn show_info_notification(title: &str, message: &str) -> Result<(), String> {
    info!("Showing info notification: {}", title);

    Notification::new()
        .summary(title)
        .body(message)
        .icon("dialog-information")
        .timeout(Timeout::Milliseconds(5000))
        .show()
        .map_err(|e| format!("Failed to show notification: {}", e))?;

    Ok(())
}

/// Show an error notification
///
/// # Arguments
///
/// * `title` - Title of the notification
/// * `error_message` - The error message to display
///
/// # Returns
///
/// Ok if notification was shown successfully
pub fn show_error_notification(title: &str, error_message: &str) -> Result<(), String> {
    warn!("Showing error notification: {}", title);

    Notification::new()
        .summary(title)
        .body(error_message)
        .icon("dialog-error")
        .timeout(Timeout::Milliseconds(8000))
        .show()
        .map_err(|e| format!("Failed to show notification: {}", e))?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_notification_result_equality() {
        assert_eq!(NotificationResult::Accepted, NotificationResult::Accepted);
        assert_eq!(NotificationResult::Denied, NotificationResult::Denied);
        assert_ne!(NotificationResult::Accepted, NotificationResult::Denied);
    }

    #[test]
    fn test_dunst_default_action_is_up_only() {
        assert_eq!(
            action_mode_for_server("dunst", "1.13.0", PromptKind::UserPresence),
            NotificationActionMode::DunstDefault
        );
        assert_eq!(
            action_mode_for_server("Dunst", "1.13.0", PromptKind::UserPresence),
            NotificationActionMode::DunstDefault
        );
        assert_eq!(
            action_mode_for_server("dunst", "1.13.0", PromptKind::UserVerification),
            NotificationActionMode::Explicit
        );
        assert_eq!(
            action_mode_for_server("dunst", "1.13.0", PromptKind::YesNo),
            NotificationActionMode::Explicit
        );
    }

    #[test]
    fn test_legacy_default_action_servers_are_preserved() {
        for (name, version) in [("notify-osd", "1.0"), ("mako", "0.0.0"), ("quickshell", "")] {
            for prompt_kind in [
                PromptKind::UserPresence,
                PromptKind::UserVerification,
                PromptKind::YesNo,
            ] {
                assert_eq!(
                    action_mode_for_server(name, version, prompt_kind),
                    NotificationActionMode::Default
                );
            }
        }
    }

    #[test]
    fn test_unknown_servers_use_explicit_actions() {
        assert_eq!(
            action_mode_for_server("gnome-shell", "47", PromptKind::UserPresence),
            NotificationActionMode::Explicit
        );
    }

    #[test]
    fn test_default_action_acceptance_is_mode_specific() {
        assert!(action_is_accepted(
            "default",
            "approve",
            NotificationActionMode::DunstDefault
        ));
        assert!(action_is_accepted(
            "default",
            "approve",
            NotificationActionMode::Default
        ));
        assert!(!action_is_accepted(
            "default",
            "approve",
            NotificationActionMode::Explicit
        ));
        assert!(action_is_accepted(
            "approve",
            "approve",
            NotificationActionMode::Explicit
        ));
        assert!(!action_is_accepted(
            "deny",
            "approve",
            NotificationActionMode::DunstDefault
        ));
        assert!(!action_is_accepted(
            "__closed",
            "approve",
            NotificationActionMode::DunstDefault
        ));
    }

    #[test]
    fn test_notification_action_mode_doesnt_panic() {
        let _ = notification_action_mode(PromptKind::UserPresence);
    }
}