oauth2-passkey 0.6.1

OAuth2 and Passkey authentication library for Rust web applications
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
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
use super::*;
use crate::storage::GENERIC_CACHE_STORE;

/// Test store_aaguid_in_cache function with valid JSON
/// This test checks that the function stores the AAGUID mappings in the cache successfully.
/// It performs the following steps:
/// 1. Initializes a test environment
/// 2. Creates a test JSON string with valid AAGUID mappings
/// 3. Calls `store_aaguid_in_cache` to store the mappings in the cache
/// 4. Verifies that the mappings were successfully stored in the cache
///
#[tokio::test]
async fn test_store_aaguid_in_cache_success() {
    use crate::test_utils::init_test_environment;
    init_test_environment().await;

    let json = r#"
    {
        "00000000-0000-0000-0000-000000000000": {
            "name": "Test Authenticator",
            "icon_dark": "https://example.com/icon-dark.png",
            "icon_light": "https://example.com/icon-light.png"
        },
        "11111111-1111-1111-1111-111111111111": {
            "name": "Another Authenticator",
            "icon_dark": null,
            "icon_light": null
        }
    }
    "#;

    // Store the data
    let result = store_aaguid_in_cache(json.to_string()).await;
    assert!(result.is_ok(), "Failed to store valid JSON: {result:?}");

    // Verify both AAGUIDs were stored by retrieving them using the unified cache operations
    let info1 = get_authenticator_info("00000000-0000-0000-0000-000000000000").await;
    assert!(info1.is_ok(), "Failed to get first AAGUID from cache");
    let info1 = info1.unwrap();
    assert!(info1.is_some(), "First AAGUID should exist in cache");
    let info1 = info1.unwrap();
    assert_eq!(info1.name, "Test Authenticator");
    assert_eq!(
        info1.icon_dark,
        Some("https://example.com/icon-dark.png".to_string())
    );

    let info2 = get_authenticator_info("11111111-1111-1111-1111-111111111111").await;
    assert!(info2.is_ok(), "Failed to get second AAGUID from cache");
    let info2 = info2.unwrap();
    assert!(info2.is_some(), "Second AAGUID should exist in cache");
    let info2 = info2.unwrap();
    assert_eq!(info2.name, "Another Authenticator");
    assert_eq!(info2.icon_dark, None);
}

/// Test store_aaguid_in_cache function with invalid JSON
/// This test checks that the function returns an error when called with invalid JSON.
/// It performs the following steps:
/// 1. Initializes a test environment
/// 2. Creates a test JSON string with invalid AAGUID mappings
/// 3. Calls `store_aaguid_in_cache` to store the mappings in the cache
/// 4. Verifies that the function returns an error
#[tokio::test]
async fn test_store_aaguid_in_cache_invalid_json() {
    use crate::test_utils::init_test_environment;
    init_test_environment().await;

    let invalid_json = r#"
    {
        "00000000-0000-0000-0000-000000000000": {
            "name": "Test Authenticator",
            "icon_dark": "https://example.com/icon-dark.png",
            "icon_light": "https://example.com/icon-light.png",
        }
    }
    "#;

    let result = store_aaguid_in_cache(invalid_json.to_string()).await;
    assert!(result.is_err(), "Expected error for invalid JSON");

    if let Err(PasskeyError::Storage(msg)) = result {
        assert!(
            msg.contains("expected") || msg.contains("trailing comma"),
            "Error message should indicate JSON parsing issue: {msg}"
        );
    } else {
        panic!("Expected PasskeyError::Storage");
    }
}

/// Test AuthenticatorInfo parsing with valid data
///
/// This test verifies that `AuthenticatorInfo` can be correctly deserialized from
/// valid JSON containing all required fields. It tests the serde parsing of
/// authenticator metadata including name and icon URLs.
#[test]
fn test_authenticator_info_parsing() {
    let json = r#"
    {
        "name": "Test Authenticator",
        "icon_dark": "https://example.com/icon-dark.png",
        "icon_light": "https://example.com/icon-light.png"
    }
    "#;

    let info: Result<AuthenticatorInfo, _> = serde_json::from_str(json);
    assert!(info.is_ok());
    let info = info.unwrap();
    assert_eq!(info.name, "Test Authenticator");
    assert_eq!(
        info.icon_dark,
        Some("https://example.com/icon-dark.png".to_string())
    );
    assert_eq!(
        info.icon_light,
        Some("https://example.com/icon-light.png".to_string())
    );
}

/// Test AuthenticatorInfo parsing with null icons
///
/// This test verifies that `AuthenticatorInfo` correctly handles null values for
/// optional icon fields during JSON deserialization. It tests that null icon
/// values are properly converted to None in the parsed structure.
#[test]
fn test_authenticator_info_parsing_null_icons() {
    let json = r#"
    {
        "name": "Test Authenticator",
        "icon_dark": null,
        "icon_light": null
    }
    "#;

    let info: Result<AuthenticatorInfo, _> = serde_json::from_str(json);
    assert!(info.is_ok());
    let info = info.unwrap();
    assert_eq!(info.name, "Test Authenticator");
    assert_eq!(info.icon_dark, None);
    assert_eq!(info.icon_light, None);
}

/// Test AuthenticatorInfo parsing with missing fields
///
/// This test verifies that `AuthenticatorInfo` correctly handles JSON with missing
/// optional fields during deserialization. It tests that missing icon fields
/// default to None and that the name field is properly parsed.
#[test]
fn test_authenticator_info_parsing_missing_fields() {
    let json = r#"
    {
        "icon_dark": "https://example.com/icon-dark.png",
        "icon_light": "https://example.com/icon-light.png"
    }
    "#;

    let info: Result<AuthenticatorInfo, _> = serde_json::from_str(json);
    assert!(
        info.is_err(),
        "Should fail when required 'name' field is missing"
    );
}

/// Test AAGUID validation
///
/// This test verifies AAGUID format validation logic by testing various AAGUID
/// string formats. It validates that proper UUID format AAGUIDs are accepted
/// and that invalid formats are properly rejected with appropriate errors.
#[test]
fn test_aaguid_format_validation() {
    // Valid AAGUID format
    let valid_aaguid = "00000000-0000-0000-0000-000000000000";
    assert_eq!(valid_aaguid.len(), 36);
    assert!(valid_aaguid.chars().filter(|&c| c == '-').count() == 4);

    // Invalid AAGUID format
    let invalid_aaguid = "invalid-aaguid-format";
    assert_ne!(invalid_aaguid.len(), 36);
}

// Test get_authenticator_info with non-existent AAGUID
#[tokio::test]
async fn test_get_authenticator_info_not_found() {
    use crate::test_utils::init_test_environment;
    init_test_environment().await;

    let non_existent_aaguid = "99999999-9999-9999-9999-999999999999";
    let result = get_authenticator_info(non_existent_aaguid).await;

    // Should return Ok(None) for non-existent AAGUID
    assert!(
        result.is_ok(),
        "Should handle non-existent AAGUID gracefully"
    );
    assert!(
        result.unwrap().is_none(),
        "Should return None for non-existent AAGUID"
    );
}

/// Test batch retrieval with empty input
/// This test checks that the function can retrieve a batch of authenticator information with an empty input.
/// It performs the following steps:
/// 1. Initializes a test environment
/// 2. Creates an empty vector of AAGUIDs
/// 3. Calls `get_authenticator_info_batch` to retrieve the batch of authenticator information
/// 4. Verifies that the function returns an empty map
#[tokio::test]
async fn test_get_authenticator_info_batch_empty() {
    use crate::test_utils::init_test_environment;
    init_test_environment().await;

    let empty_aaguids: Vec<String> = vec![];
    let result = get_authenticator_info_batch(&empty_aaguids).await;

    assert!(result.is_ok());
    let info_map = result.unwrap();
    assert!(info_map.is_empty());
}

/// Test successful retrieval after storage
///
/// This test verifies that `get_authenticator_info_batch` can successfully retrieve
/// authenticator information after it has been stored in the cache. It stores AAGUID
/// data and then retrieves it to validate the complete storage and retrieval cycle.
#[tokio::test]
async fn test_get_authenticator_info_success() {
    use crate::test_utils::init_test_environment;
    init_test_environment().await;

    // First store some test data
    let json = r#"
    {
        "12345678-1234-1234-1234-123456789abc": {
            "name": "YubiKey 5",
            "icon_dark": "https://example.com/yubikey-dark.png",
            "icon_light": "https://example.com/yubikey-light.png"
        }
    }
    "#;

    let store_result = store_aaguid_in_cache(json.to_string()).await;
    assert!(store_result.is_ok(), "Failed to store test data");

    // Now retrieve it
    let aaguid = "12345678-1234-1234-1234-123456789abc";
    let result = get_authenticator_info(aaguid).await;

    assert!(result.is_ok(), "Failed to retrieve stored AAGUID");
    let info = result.unwrap();
    assert!(info.is_some(), "Should find the stored AAGUID");

    let info = info.unwrap();
    assert_eq!(info.name, "YubiKey 5");
    assert_eq!(
        info.icon_dark,
        Some("https://example.com/yubikey-dark.png".to_string())
    );
    assert_eq!(
        info.icon_light,
        Some("https://example.com/yubikey-light.png".to_string())
    );
}

/// Test batch retrieval with actual data
///
/// This test verifies that `get_authenticator_info_batch` correctly retrieves multiple
/// authenticator entries when queried with multiple AAGUIDs. It tests batch operations
/// with real data and validates that all requested entries are properly returned.
#[tokio::test]
async fn test_get_authenticator_info_batch_with_data() {
    use crate::test_utils::init_test_environment;
    init_test_environment().await;

    // Store multiple test AAGUIDs
    let json = r#"
    {
        "aaaa0000-bbbb-cccc-dddd-eeeeeeeeeeee": {
            "name": "Authenticator A",
            "icon_dark": "https://example.com/a-dark.png",
            "icon_light": null
        },
        "bbbb1111-cccc-dddd-eeee-ffffffffffff": {
            "name": "Authenticator B",
            "icon_dark": null,
            "icon_light": "https://example.com/b-light.png"
        },
        "cccc2222-dddd-eeee-ffff-000000000000": {
            "name": "Authenticator C",
            "icon_dark": null,
            "icon_light": null
        }
    }
    "#;

    let store_result = store_aaguid_in_cache(json.to_string()).await;
    assert!(store_result.is_ok(), "Failed to store test data");

    // Test batch retrieval with mix of existing and non-existing AAGUIDs
    let aaguids = vec![
        "aaaa0000-bbbb-cccc-dddd-eeeeeeeeeeee".to_string(),
        "bbbb1111-cccc-dddd-eeee-ffffffffffff".to_string(),
        "nonexistent-aaguid-here".to_string(), // This one doesn't exist
        "cccc2222-dddd-eeee-ffff-000000000000".to_string(),
    ];

    let result = get_authenticator_info_batch(&aaguids).await;
    assert!(result.is_ok(), "Batch retrieval should succeed");

    let info_map = result.unwrap();
    assert_eq!(
        info_map.len(),
        3,
        "Should return 3 existing AAGUIDs, ignore non-existent"
    );

    // Verify each retrieved item
    let info_a = info_map
        .get("aaaa0000-bbbb-cccc-dddd-eeeeeeeeeeee")
        .unwrap();
    assert_eq!(info_a.name, "Authenticator A");
    assert_eq!(
        info_a.icon_dark,
        Some("https://example.com/a-dark.png".to_string())
    );
    assert_eq!(info_a.icon_light, None);

    let info_b = info_map
        .get("bbbb1111-cccc-dddd-eeee-ffffffffffff")
        .unwrap();
    assert_eq!(info_b.name, "Authenticator B");
    assert_eq!(info_b.icon_dark, None);
    assert_eq!(
        info_b.icon_light,
        Some("https://example.com/b-light.png".to_string())
    );

    let info_c = info_map
        .get("cccc2222-dddd-eeee-ffff-000000000000")
        .unwrap();
    assert_eq!(info_c.name, "Authenticator C");
    assert_eq!(info_c.icon_dark, None);
    assert_eq!(info_c.icon_light, None);

    // Verify non-existent AAGUID is not in the result
    assert!(!info_map.contains_key("nonexistent-aaguid-here"));
}

/// Test cache corruption handling (invalid JSON stored in cache)
///
/// This test verifies that `get_authenticator_info_batch` gracefully handles cache
/// corruption by returning appropriate errors when invalid JSON is stored in the cache.
/// It tests error handling for corrupted cache data scenarios.
#[tokio::test]
async fn test_get_authenticator_info_corrupted_cache() {
    use crate::storage::CacheData;
    use crate::test_utils::init_test_environment;
    init_test_environment().await;

    let aaguid = "corrupt-data-test-aaguid";

    // Manually insert invalid JSON into cache
    let corrupted_data = CacheData {
        value: "invalid json data".to_string(),
    };

    let mut cache = GENERIC_CACHE_STORE.lock().await;
    let cache_prefix = CachePrefix::new("aaguid".to_string()).unwrap();
    let cache_key = CacheKey::new(aaguid.to_string()).unwrap();
    let put_result = cache.put(cache_prefix, cache_key, corrupted_data).await;
    assert!(put_result.is_ok(), "Should be able to put corrupted data");
    drop(cache); // Release the lock

    // Now try to retrieve it - should handle the corruption gracefully
    let result = get_authenticator_info(aaguid).await;
    assert!(
        result.is_err(),
        "Should return error for corrupted cache data"
    );

    if let Err(PasskeyError::Storage(msg)) = result {
        assert!(
            msg.contains("expected") || msg.contains("EOF") || msg.contains("invalid"),
            "Error should indicate JSON parsing issue: {msg}"
        );
    } else {
        panic!("Expected PasskeyError::Storage for corrupted data");
    }
}

/// Test edge case: empty JSON object
///
/// This test verifies that `store_aaguid_in_cache` correctly handles empty JSON objects
/// by successfully storing them and returning appropriate results. It tests the boundary
/// case of valid but empty AAGUID mapping data.
#[tokio::test]
async fn test_store_aaguid_in_cache_empty_object() {
    use crate::test_utils::init_test_environment;
    init_test_environment().await;

    let empty_json = "{}";
    let result = store_aaguid_in_cache(empty_json.to_string()).await;

    assert!(result.is_ok(), "Empty JSON object should be valid");

    // Verify no AAGUIDs were stored by trying to get a non-existent AAGUID
    let non_existent = get_authenticator_info("any-aaguid").await;
    assert!(non_existent.is_ok());
    assert!(
        non_existent.unwrap().is_none(),
        "No AAGUIDs should be stored from empty object"
    );
}

/// Test batch retrieval edge case: duplicate AAGUIDs in input
///
/// This test verifies that `get_authenticator_info_batch` correctly handles duplicate
/// AAGUIDs in the input vector by deduplicating them and returning each unique entry
/// only once. It tests the function's handling of redundant input data.
#[tokio::test]
async fn test_get_authenticator_info_batch_duplicates() {
    use crate::test_utils::init_test_environment;
    init_test_environment().await;

    // Store one test AAGUID
    let json = r#"
    {
        "duplicate-test-aaguid": {
            "name": "Duplicate Test",
            "icon_dark": null,
            "icon_light": null
        }
    }
    "#;

    let store_result = store_aaguid_in_cache(json.to_string()).await;
    assert!(store_result.is_ok(), "Failed to store test data");

    // Request with duplicates
    let aaguids = vec![
        "duplicate-test-aaguid".to_string(),
        "duplicate-test-aaguid".to_string(), // Duplicate
        "nonexistent".to_string(),
        "duplicate-test-aaguid".to_string(), // Another duplicate
    ];

    let result = get_authenticator_info_batch(&aaguids).await;
    assert!(
        result.is_ok(),
        "Batch retrieval with duplicates should succeed"
    );

    let info_map = result.unwrap();
    assert_eq!(
        info_map.len(),
        1,
        "Should have only one unique result despite duplicates"
    );

    let info = info_map.get("duplicate-test-aaguid").unwrap();
    assert_eq!(info.name, "Duplicate Test");
}