aria2-core 0.2.3

High-performance download engine core: multi-protocol segmented downloads, rate limiting, config management, session persistence, and BitTorrent seeding
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
464
465
466
467
468
469
470
471
472
473
//! Credential Store for HTTP Authentication
//!
//! Provides a thread-safe storage mechanism for domain-specific credentials.
//! All passwords are automatically zeroized from memory when entries are removed
//! or the store is dropped, preventing sensitive data from lingering in memory.
//!
//! # Features
//! - Thread-safe: Uses `RwLock` for concurrent read access
//! - Memory safe: Passwords are zeroed on drop using `zeroize`
//! - Domain-based: Credentials are organized by domain/hostname
//! - CRUD operations: Store, retrieve, remove, and clear credentials
//!
//! # Example
//! ```rust
//! use aria2_core::auth::credential_store::CredentialStore;
//!
//! let store = CredentialStore::new();
//! store.store("example.com", "alice", b"secret123");
//!
//! if let Some(creds) = store.get("example.com") {
//!     println!("Found credentials for user: {}", creds.username);
//! }
//! ```

use std::collections::HashMap;
use std::fmt;
use std::sync::RwLock;
use zeroize::Zeroize;

/// A credential entry containing username and password for a specific domain.
///
/// The password field uses `Zeroize` to ensure secure memory erasure when the
/// entry is dropped. This prevents sensitive data from remaining in memory
/// after credentials are no longer needed.
#[derive(Clone)]
pub struct PasswordEntry {
    /// Username for authentication
    pub username: String,
    /// Password bytes (zeroized on drop)
    pub password: Vec<u8>,
}

impl fmt::Debug for PasswordEntry {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PasswordEntry")
            .field("username", &self.username)
            .field("password", &"***") // Mask password in debug output
            .finish()
    }
}

impl Drop for PasswordEntry {
    fn drop(&mut self) {
        // Zeroize the password before dropping
        self.password.zeroize();
    }
}

/// Thread-safe credential storage for HTTP authentication.
///
/// Stores username/password pairs indexed by domain (hostname or URL pattern).
/// Provides concurrent access through `RwLock`, allowing multiple readers
/// while ensuring exclusive access during writes.
///
/// # Security Guarantees
/// - All passwords are stored as `Vec<u8>` with automatic zeroization on drop
/// - When entries are removed or the store is cleared, passwords are immediately zeroed
/// - Debug output masks passwords to prevent accidental logging of secrets
pub struct CredentialStore {
    /// Internal storage: domain -> PasswordEntry mapping
    credentials: RwLock<HashMap<String, PasswordEntry>>,
}

impl CredentialStore {
    /// Creates a new empty credential store.
    ///
    /// # Example
    /// ```
    /// use aria2_core::auth::credential_store::CredentialStore;
    ///
    /// let store = CredentialStore::new();
    /// assert_eq!(store.count(), 0);
    /// ```
    pub fn new() -> Self {
        CredentialStore {
            credentials: RwLock::new(HashMap::new()),
        }
    }

    /// Stores credentials for a domain.
    ///
    /// If credentials already exist for the given domain, they will be replaced
    /// and the old password will be zeroized before being dropped.
    ///
    /// # Arguments
    /// * `domain` - Domain or hostname (e.g., "example.com")
    /// * `username` - Username for authentication
    /// * `password` - Password as byte slice (will be copied and stored securely)
    ///
    /// # Example
    /// ```no_run
    /// use aria2_core::auth::credential_store::{CredentialStore, PasswordEntry};
    ///
    /// let mut store = CredentialStore::new();
    /// store.store("api.example.com", "admin", b"secure-password-123");
    /// ```
    pub fn store(&self, domain: &str, username: &str, password: &[u8]) {
        let mut creds = self.credentials.write().unwrap();
        let entry = PasswordEntry {
            username: username.to_string(),
            password: password.to_vec(),
        };
        creds.insert(domain.to_string(), entry);
    }

    /// Retrieves credentials for a domain.
    ///
    /// Returns a clone of the `PasswordEntry` if found, or `None` if no
    /// credentials exist for the specified domain.
    ///
    /// # Arguments
    /// * `domain` - Domain or hostname to look up
    ///
    /// # Returns
    /// - `Some(PasswordEntry)` if credentials exist
    /// - `None` if no credentials found
    ///
    /// # Security Note
    /// The returned `PasswordEntry` contains the actual password. Handle it
    /// carefully and allow it to be dropped naturally so the password gets
    /// zeroized automatically.
    ///
    /// # Example
    /// ```
    /// use aria2_core::auth::credential_store::{CredentialStore, PasswordEntry};
    ///
    /// let store = CredentialStore::new();
    /// store.store("example.com", "user", b"pass");
    /// if let Some(creds) = store.get("example.com") {
    ///     println!("Username: {}", creds.username);
    /// } // Password is zeroized when creds goes out of scope
    /// ```
    pub fn get(&self, domain: &str) -> Option<PasswordEntry> {
        let creds = self.credentials.read().unwrap();
        creds.get(domain).cloned()
    }

    /// Removes credentials for a domain.
    ///
    /// The removed entry's password will be zeroized before this method returns.
    ///
    /// # Arguments
    /// * `domain` - Domain or hostname to remove credentials for
    ///
    /// # Returns
    /// - `Some(PasswordEntry)` that was removed
    /// - `None` if no credentials existed for the domain
    ///
    /// # Example
    /// ```
    /// use aria2_core::auth::credential_store::{CredentialStore, PasswordEntry};
    ///
    /// let mut store = CredentialStore::new();
    /// store.store("old-server.com", "admin", b"pass");
    /// if let Some(removed) = store.remove("old-server.com") {
    ///     println!("Removed credentials for user: {}", removed.username);
    /// }
    /// ```
    pub fn remove(&self, domain: &str) -> Option<PasswordEntry> {
        let mut creds = self.credentials.write().unwrap();
        creds.remove(domain)
    }

    /// Clears all stored credentials.
    ///
    /// All passwords in the store will be zeroized before this method returns.
    /// After calling this method, the store will be empty.
    ///
    /// # Example
    /// ```
    /// use aria2_core::auth::credential_store::CredentialStore;
    ///
    /// let mut store = CredentialStore::new();
    /// store.store("example.com", "user", b"pass");
    /// store.clear(); // All passwords securely erased
    /// assert_eq!(store.count(), 0);
    /// ```
    pub fn clear(&self) {
        let mut creds = self.credentials.write().unwrap();
        creds.clear(); // All PasswordEntries are dropped, triggering zeroization
    }

    /// Returns the number of stored credential entries.
    pub fn count(&self) -> usize {
        let creds = self.credentials.read().unwrap();
        creds.len()
    }

    /// Checks if credentials exist for a domain.
    ///
    /// # Arguments
    /// * `domain` - Domain or hostname to check
    ///
    /// # Returns
    /// `true` if credentials exist, `false` otherwise
    pub fn has_credentials(&self, domain: &str) -> bool {
        let creds = self.credentials.read().unwrap();
        creds.contains_key(domain)
    }

    /// Lists all domains that have stored credentials.
    ///
    /// # Returns
    /// A vector of domain strings (not the actual credentials)
    ///
    /// # Note
    /// This does not expose any sensitive data, only domain names.
    pub fn list_domains(&self) -> Vec<String> {
        let creds = self.credentials.read().unwrap();
        creds.keys().cloned().collect()
    }
}

impl Default for CredentialStore {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Debug for CredentialStore {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CredentialStore")
            .field("entry_count", &self.count())
            .finish()
    }
}

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

    #[test]
    fn test_credential_store_creation() {
        let store = CredentialStore::new();
        assert_eq!(store.count(), 0);
        assert!(!store.has_credentials("example.com"));
    }

    #[test]
    fn test_credential_store_default() {
        let store = CredentialStore::default();
        assert_eq!(store.count(), 0);
    }

    #[test]
    fn test_credential_store_operations() {
        let store = CredentialStore::new();

        // Test initial state
        assert_eq!(store.count(), 0);

        // Store credentials
        store.store("example.com", "alice", b"secret123");
        assert_eq!(store.count(), 1);
        assert!(store.has_credentials("example.com"));

        // Retrieve credentials
        let creds = store.get("example.com").expect("Should find credentials");
        assert_eq!(creds.username, "alice");
        assert_eq!(creds.password, b"secret123");

        // Remove credentials
        let removed = store
            .remove("example.com")
            .expect("Should remove credentials");
        assert_eq!(removed.username, "alice");
        assert_eq!(store.count(), 0);
        assert!(!store.has_credentials("example.com"));
    }

    #[test]
    fn test_credential_store_multiple_domains() {
        let store = CredentialStore::new();

        store.store("api.example.com", "admin", b"admin-pass");
        store.store("db.example.com", "dbuser", b"db-pass");
        store.store("auth.example.com", "authuser", b"auth-pass");

        assert_eq!(store.count(), 3);

        // Verify each domain has correct credentials
        let api_creds = store.get("api.example.com").unwrap();
        assert_eq!(api_creds.username, "admin");

        let db_creds = store.get("db.example.com").unwrap();
        assert_eq!(db_creds.username, "dbuser");

        // List domains
        let domains = store.list_domains();
        assert_eq!(domains.len(), 3);
        assert!(domains.contains(&"api.example.com".to_string()));
        assert!(domains.contains(&"db.example.com".to_string()));
        assert!(domains.contains(&"auth.example.com".to_string()));
    }

    #[test]
    fn test_credential_store_overwrite() {
        let store = CredentialStore::new();

        // Store initial credentials
        store.store("example.com", "user1", b"pass1");

        // Overwrite with new credentials
        store.store("example.com", "user2", b"pass2");

        // Should have new credentials
        assert_eq!(store.count(), 1); // Still only one entry

        let creds = store.get("example.com").unwrap();
        assert_eq!(creds.username, "user2");
        assert_eq!(creds.password, b"pass2");
    }

    #[test]
    fn test_credential_store_remove_nonexistent() {
        let store = CredentialStore::new();

        let result = store.remove("nonexistent.com");
        assert!(result.is_none());
        assert_eq!(store.count(), 0);
    }

    #[test]
    fn test_credential_store_clear() {
        let store = CredentialStore::new();

        store.store("domain1.com", "user1", b"pass1");
        store.store("domain2.com", "user2", b"pass2");
        store.store("domain3.com", "user3", b"pass3");

        assert_eq!(store.count(), 3);

        store.clear();

        assert_eq!(store.count(), 0);
        assert!(!store.has_credentials("domain1.com"));
        assert!(!store.has_credentials("domain2.com"));
        assert!(!store.has_credentials("domain3.com"));
    }

    #[test]
    fn test_password_entry_debug_masking() {
        let entry = PasswordEntry {
            username: "testuser".to_string(),
            password: b"super-secret-password".to_vec(),
        };

        let debug_output = format!("{:?}", entry);

        // Verify username is visible but password is masked
        assert!(debug_output.contains("testuser"));
        assert!(debug_output.contains("***")); // Masked password
        assert!(!debug_output.contains("super-secret-password"));
    }

    #[test]
    fn test_credential_store_debug() {
        let store = CredentialStore::new();
        store.store("example.com", "user", b"pass");

        let debug_output = format!("{:?}", store);

        // Should show count but not expose credentials
        assert!(debug_output.contains("CredentialStore"));
        assert!(debug_output.contains("entry_count"));
        assert!(debug_output.contains("1"));
        assert!(!debug_output.contains("user"));
        assert!(!debug_output.contains("pass"));
    }

    #[test]
    fn test_credential_store_concurrent_access() {
        use std::sync::Arc;
        use std::thread;

        let store = Arc::new(CredentialStore::new());
        let mut handles = vec![];

        // Spawn multiple threads that write concurrently
        for i in 0..10 {
            let store_clone = Arc::clone(&store);
            handles.push(thread::spawn(move || {
                let domain = format!("domain{}.com", i);
                store_clone.store(
                    &domain,
                    &format!("user{}", i),
                    format!("pass{}", i).as_bytes(),
                );
            }));
        }

        // Wait for all threads to complete
        for handle in handles {
            handle.join().unwrap();
        }

        // Verify all entries were created
        assert_eq!(store.count(), 10);

        // Verify we can read from multiple threads
        let mut read_handles = vec![];
        for i in 0..10 {
            let store_clone = Arc::clone(&store);
            read_handles.push(thread::spawn(move || {
                let domain = format!("domain{}.com", i);
                let creds = store_clone.get(&domain).unwrap();
                assert_eq!(creds.username, format!("user{}", i));
            }));
        }

        for handle in read_handles {
            handle.join().unwrap();
        }
    }

    #[test]
    fn test_empty_domain_and_credentials() {
        let store = CredentialStore::new();

        // Edge case: empty domain string
        store.store("", "user", b"pass");
        assert!(store.has_credentials(""));
        let creds = store.get("").unwrap();
        assert_eq!(creds.username, "user");

        // Edge case: empty username
        store.store("empty-user.com", "", b"pass");
        let creds = store.get("empty-user.com").unwrap();
        assert_eq!(creds.username, "");

        // Edge case: empty password
        store.store("empty-pass.com", "user", b"");
        let creds = store.get("empty-pass.com").unwrap();
        assert_eq!(creds.password, b"");

        assert_eq!(store.count(), 3);
    }

    #[test]
    fn test_binary_password_storage() {
        let store = CredentialStore::new();

        // Store binary data as password (e.g., hashed credentials)
        let binary_pass: Vec<u8> = vec![0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0xFF];
        store.store("binary-auth.com", "binary_user", &binary_pass);

        let creds = store.get("binary-auth.com").unwrap();
        assert_eq!(creds.password, binary_pass);
        assert_eq!(creds.password.len(), 6);
    }

    #[test]
    fn test_unicode_in_credentials() {
        let store = CredentialStore::new();

        // Unicode characters in all fields
        store.store("例子.コム", "ユーザー名", "パスワード".as_bytes());

        let creds = store.get("例子.コム").unwrap();
        assert_eq!(creds.username, "ユーザー名");
        assert_eq!(creds.password, "パスワード".as_bytes());
    }
}