auth-framework 0.5.0-rc18

A comprehensive, production-ready authentication and authorization framework for Rust 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
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
//! Device Authorization Grant Implementation - RFC 8628
//!
//! This module implements RFC 8628 - OAuth 2.0 Device Authorization Grant
//! which allows devices with limited input capability (smart TVs, printers, etc.)
//! to obtain user authorization.

use crate::errors::{AuthError, Result};
use crate::storage::AuthStorage;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use uuid::Uuid;

/// Device authorization request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeviceAuthorizationRequest {
    /// Client identifier
    pub client_id: String,

    /// Requested scopes
    pub scope: Option<String>,
}

/// Device authorization response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeviceAuthorizationResponse {
    /// Device verification code (for storage)
    pub device_code: String,

    /// User-friendly code to enter
    pub user_code: String,

    /// URL where user should authorize
    pub verification_uri: String,

    /// Complete verification URL with user_code (optional)
    pub verification_uri_complete: Option<String>,

    /// Polling interval in seconds
    pub interval: u64,

    /// Device code expires in seconds
    pub expires_in: u64,
}

/// Token request for device code grant
#[derive(Debug, Clone, Deserialize)]
pub struct DeviceTokenRequest {
    /// Grant type (must be "urn:ietf:params:oauth:grant-type:device_code")
    pub grant_type: String,

    /// Device code received from device authorization
    pub device_code: String,

    /// Client identifier
    pub client_id: String,
}

/// Stored device authorization data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StoredDeviceAuthorization {
    /// Device code
    pub device_code: String,

    /// User code
    pub user_code: String,

    /// Client ID
    pub client_id: String,

    /// Requested scopes
    pub scope: Option<String>,

    /// Authorization status
    pub status: DeviceAuthorizationStatus,

    /// User ID (once authorized)
    pub user_id: Option<String>,

    /// When the request was created
    pub created_at: SystemTime,

    /// When the request expires
    pub expires_at: SystemTime,

    /// Last poll time (for slow_down error)
    pub last_poll: Option<SystemTime>,

    /// Number of times the client has been told to slow down (RFC 8628 ยง3.5).
    /// Each slow_down increases the required interval by 5 seconds.
    #[serde(default)]
    pub slow_down_count: u32,
}

/// Device authorization status
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum DeviceAuthorizationStatus {
    /// Awaiting user authorization
    Pending,
    /// User has authorized
    Authorized,
    /// User has denied
    Denied,
    /// Authorization expired
    Expired,
}

/// Device authorization manager with persistent storage
use std::fmt;

#[derive(Clone)]
pub struct DeviceAuthManager {
    /// Persistent storage backend
    storage: Arc<dyn AuthStorage>,

    /// Memory cache for fast access
    authorizations: Arc<tokio::sync::RwLock<HashMap<String, StoredDeviceAuthorization>>>,

    /// Default expiration time for device codes
    default_expiration: Duration,

    /// Minimum polling interval
    min_interval: Duration,

    /// Base verification URI
    verification_uri: String,
}

impl fmt::Debug for DeviceAuthManager {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("DeviceAuthManager")
            .field("storage", &"<dyn AuthStorage>")
            .field("default_expiration", &self.default_expiration)
            .field("min_interval", &self.min_interval)
            .field("verification_uri", &self.verification_uri)
            .finish()
    }
}

impl DeviceAuthManager {
    /// Create a new device authorization manager
    pub fn new(storage: Arc<dyn AuthStorage>, verification_uri: String) -> Self {
        Self {
            storage,
            authorizations: Arc::new(tokio::sync::RwLock::new(HashMap::new())),
            default_expiration: Duration::from_secs(600), // 10 minutes (RFC 8628 recommendation)
            min_interval: Duration::from_secs(5),         // 5 seconds minimum
            verification_uri,
        }
    }

    /// Create a new device authorization manager with custom settings
    pub fn with_settings(
        storage: Arc<dyn AuthStorage>,
        verification_uri: String,
        expiration: Duration,
        min_interval: Duration,
    ) -> Self {
        Self {
            storage,
            authorizations: Arc::new(tokio::sync::RwLock::new(HashMap::new())),
            default_expiration: expiration,
            min_interval,
            verification_uri,
        }
    }

    /// Set the default expiration time for device codes (chainable).
    ///
    /// Default: 600 seconds (10 minutes, per RFC 8628 recommendation).
    pub fn expiration(mut self, expiration: Duration) -> Self {
        self.default_expiration = expiration;
        self
    }

    /// Set the minimum polling interval (chainable).
    ///
    /// Default: 5 seconds.
    pub fn interval(mut self, interval: Duration) -> Self {
        self.min_interval = interval;
        self
    }

    /// Initiate device authorization flow
    pub async fn create_authorization(
        &self,
        request: DeviceAuthorizationRequest,
    ) -> Result<DeviceAuthorizationResponse> {
        // Validate the request
        self.validate_request(&request)?;

        // Generate device code and user code
        let device_code = format!("dc_{}", Uuid::new_v4().simple());
        let user_code = self.generate_user_code();

        // Calculate expiration
        let now = SystemTime::now();
        let expires_at = now + self.default_expiration;

        // Create stored authorization
        let stored = StoredDeviceAuthorization {
            device_code: device_code.clone(),
            user_code: user_code.clone(),
            client_id: request.client_id.clone(),
            scope: request.scope.clone(),
            status: DeviceAuthorizationStatus::Pending,
            user_id: None,
            created_at: now,
            expires_at,
            last_poll: None,
            slow_down_count: 0,
        };

        // Store in persistent backend with TTL
        let device_key = format!("device_code:{}", device_code);
        let user_key = format!("user_code:{}", user_code);

        let serialized = serde_json::to_string(&stored)
            .map_err(|e| AuthError::internal(format!("Failed to serialize device auth: {}", e)))?;

        self.storage
            .store_kv(
                &device_key,
                serialized.as_bytes(),
                Some(self.default_expiration),
            )
            .await
            .map_err(|e| {
                AuthError::internal(format!("Failed to store device authorization: {}", e))
            })?;

        // Also store under user_code for verification page
        self.storage
            .store_kv(
                &user_key,
                serialized.as_bytes(),
                Some(self.default_expiration),
            )
            .await
            .map_err(|e| {
                AuthError::internal(format!("Failed to store user code mapping: {}", e))
            })?;

        // Cache in memory
        let mut authorizations = self.authorizations.write().await;
        authorizations.insert(device_code.clone(), stored);

        // Cleanup expired entries
        self.cleanup_expired(&mut authorizations, now);

        // Create response
        let verification_uri_complete =
            format!("{}?user_code={}", self.verification_uri, user_code);

        Ok(DeviceAuthorizationResponse {
            device_code,
            user_code,
            verification_uri: self.verification_uri.clone(),
            verification_uri_complete: Some(verification_uri_complete),
            interval: self.min_interval.as_secs(),
            expires_in: self.default_expiration.as_secs(),
        })
    }

    /// Poll for authorization status (used during token endpoint polling)
    pub async fn poll_authorization(&self, device_code: &str) -> Result<StoredDeviceAuthorization> {
        let device_key = format!("device_code:{}", device_code);

        // Try to load from persistent storage first
        let mut stored = if let Some(data) = self.storage.get_kv(&device_key).await? {
            let serialized = String::from_utf8(data)
                .map_err(|_| AuthError::internal("Invalid UTF-8 in stored device auth data"))?;

            serde_json::from_str::<StoredDeviceAuthorization>(&serialized).map_err(|e| {
                AuthError::internal(format!("Failed to deserialize device auth: {}", e))
            })?
        } else {
            // Fallback to memory cache
            let authorizations = self.authorizations.read().await;
            authorizations
                .get(device_code)
                .cloned()
                .ok_or_else(|| AuthError::auth_method("device_auth", "Invalid device_code"))?
        };

        // Check expiration
        let now = SystemTime::now();
        if now > stored.expires_at {
            stored.status = DeviceAuthorizationStatus::Expired;
            return Err(AuthError::auth_method("device_auth", "Device code expired"));
        }

        // Check for slow_down (polling too frequently) โ€” RFC 8628 ยง3.5
        // The required interval increases by 5 seconds each time the client
        // polls too fast, implementing exponential backoff.
        let effective_interval = self.min_interval
            + Duration::from_secs(5 * u64::from(stored.slow_down_count));
        if let Some(last_poll) = stored.last_poll {
            let elapsed = now.duration_since(last_poll).unwrap_or(Duration::ZERO);
            if elapsed < effective_interval {
                stored.slow_down_count += 1;
                // Persist the updated slow_down_count
                stored.last_poll = Some(now);
                let serialized = serde_json::to_string(&stored).map_err(|e| {
                    AuthError::internal(format!("Failed to serialize device auth: {}", e))
                })?;
                self.storage
                    .store_kv(
                        &device_key,
                        serialized.as_bytes(),
                        Some(self.default_expiration),
                    )
                    .await
                    .ok();
                let mut authorizations = self.authorizations.write().await;
                authorizations.insert(device_code.to_string(), stored);
                return Err(AuthError::auth_method("device_auth", "slow_down"));
            }
        }

        // Update last poll time
        stored.last_poll = Some(now);

        // Persist updated state
        let serialized = serde_json::to_string(&stored)
            .map_err(|e| AuthError::internal(format!("Failed to serialize device auth: {}", e)))?;

        self.storage
            .store_kv(
                &device_key,
                serialized.as_bytes(),
                Some(self.default_expiration),
            )
            .await
            .ok(); // Ignore errors for poll time update

        // Update memory cache
        let mut authorizations = self.authorizations.write().await;
        authorizations.insert(device_code.to_string(), stored.clone());

        // Return current status
        match stored.status {
            DeviceAuthorizationStatus::Pending => Err(AuthError::auth_method(
                "device_auth",
                "authorization_pending",
            )),
            DeviceAuthorizationStatus::Authorized => Ok(stored),
            DeviceAuthorizationStatus::Denied => {
                Err(AuthError::auth_method("device_auth", "access_denied"))
            }
            DeviceAuthorizationStatus::Expired => {
                Err(AuthError::auth_method("device_auth", "expired_token"))
            }
        }
    }

    /// Authorize a device (called when user approves on verification page)
    pub async fn authorize_device(&self, user_code: &str, user_id: &str) -> Result<()> {
        let user_key = format!("user_code:{}", user_code);

        // Load from storage
        let mut stored = if let Some(data) = self.storage.get_kv(&user_key).await? {
            let serialized = String::from_utf8(data)
                .map_err(|_| AuthError::internal("Invalid UTF-8 in stored device auth data"))?;

            serde_json::from_str::<StoredDeviceAuthorization>(&serialized).map_err(|e| {
                AuthError::internal(format!("Failed to deserialize device auth: {}", e))
            })?
        } else {
            return Err(AuthError::auth_method("device_auth", "Invalid user_code"));
        };

        // Check if expired
        let now = SystemTime::now();
        if now > stored.expires_at {
            return Err(AuthError::auth_method("device_auth", "Device code expired"));
        }

        // Update status
        stored.status = DeviceAuthorizationStatus::Authorized;
        stored.user_id = Some(user_id.to_string());

        // Persist updated state
        let serialized = serde_json::to_string(&stored)
            .map_err(|e| AuthError::internal(format!("Failed to serialize device auth: {}", e)))?;

        let device_key = format!("device_code:{}", stored.device_code);

        self.storage
            .store_kv(
                &device_key,
                serialized.as_bytes(),
                Some(self.default_expiration),
            )
            .await?;

        self.storage
            .store_kv(
                &user_key,
                serialized.as_bytes(),
                Some(self.default_expiration),
            )
            .await?;

        // Update memory cache
        let mut authorizations = self.authorizations.write().await;
        authorizations.insert(stored.device_code.clone(), stored);

        Ok(())
    }

    /// Deny a device authorization
    pub async fn deny_device(&self, user_code: &str) -> Result<()> {
        let user_key = format!("user_code:{}", user_code);

        // Load from storage
        let mut stored = if let Some(data) = self.storage.get_kv(&user_key).await? {
            let serialized = String::from_utf8(data)
                .map_err(|_| AuthError::internal("Invalid UTF-8 in stored device auth data"))?;

            serde_json::from_str::<StoredDeviceAuthorization>(&serialized).map_err(|e| {
                AuthError::internal(format!("Failed to deserialize device auth: {}", e))
            })?
        } else {
            return Err(AuthError::auth_method("device_auth", "Invalid user_code"));
        };

        // Update status
        stored.status = DeviceAuthorizationStatus::Denied;

        // Persist updated state
        let serialized = serde_json::to_string(&stored)
            .map_err(|e| AuthError::internal(format!("Failed to serialize device auth: {}", e)))?;

        let device_key = format!("device_code:{}", stored.device_code);

        self.storage
            .store_kv(
                &device_key,
                serialized.as_bytes(),
                Some(self.default_expiration),
            )
            .await?;

        self.storage
            .store_kv(
                &user_key,
                serialized.as_bytes(),
                Some(self.default_expiration),
            )
            .await?;

        // Update memory cache
        let mut authorizations = self.authorizations.write().await;
        authorizations.insert(stored.device_code.clone(), stored);

        Ok(())
    }

    /// Get device authorization by user code (for verification page)
    pub async fn get_by_user_code(&self, user_code: &str) -> Result<StoredDeviceAuthorization> {
        let user_key = format!("user_code:{}", user_code);

        if let Some(data) = self.storage.get_kv(&user_key).await? {
            let serialized = String::from_utf8(data)
                .map_err(|_| AuthError::internal("Invalid UTF-8 in stored device auth data"))?;

            let stored: StoredDeviceAuthorization =
                serde_json::from_str(&serialized).map_err(|e| {
                    AuthError::internal(format!("Failed to deserialize device auth: {}", e))
                })?;

            // Check expiration
            let now = SystemTime::now();
            if now > stored.expires_at {
                return Err(AuthError::auth_method("device_auth", "User code expired"));
            }

            Ok(stored)
        } else {
            Err(AuthError::auth_method("device_auth", "Invalid user_code"))
        }
    }

    /// Validate device authorization request
    fn validate_request(&self, request: &DeviceAuthorizationRequest) -> Result<()> {
        if request.client_id.is_empty() {
            return Err(AuthError::auth_method("device_auth", "Missing client_id"));
        }

        // In production, validate client_id against registered clients

        Ok(())
    }

    /// Generate a user-friendly code (uppercase, no ambiguous characters)
    fn generate_user_code(&self) -> String {
        use rand::RngExt;
        const CHARS: &[u8] = b"ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; // No ambiguous: 0,O,I,1
        let mut rng = rand::rng();

        // Generate 9-character code with dash for readability: XXXX-XXXX
        let code: String = (0..9)
            .map(|i| {
                if i == 4 {
                    '-'
                } else {
                    let idx = rng.random_range(0..CHARS.len());
                    CHARS[idx] as char
                }
            })
            .collect();

        code
    }

    /// Clean up expired entries from memory cache
    fn cleanup_expired(
        &self,
        authorizations: &mut HashMap<String, StoredDeviceAuthorization>,
        now: SystemTime,
    ) {
        authorizations.retain(|_, auth| now <= auth.expires_at);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::MemoryStorage;
    use tokio::time::sleep;

    fn create_test_manager() -> DeviceAuthManager {
        let storage = Arc::new(MemoryStorage::new());
        DeviceAuthManager::new(storage, "https://example.com/device".to_string())
    }

    #[tokio::test]
    async fn test_create_authorization() {
        let manager = create_test_manager();

        let request = DeviceAuthorizationRequest {
            client_id: "test_client".to_string(),
            scope: Some("openid profile".to_string()),
        };

        let response = manager.create_authorization(request).await.unwrap();

        assert!(response.device_code.starts_with("dc_"));
        assert_eq!(response.user_code.len(), 9); // XXXX-XXXX
        assert!(response.user_code.contains('-'));
        assert_eq!(response.verification_uri, "https://example.com/device");
        assert!(response.verification_uri_complete.is_some());
        assert_eq!(response.interval, 5);
        assert_eq!(response.expires_in, 600);
    }

    #[tokio::test]
    async fn test_poll_pending() {
        let manager = create_test_manager();

        let request = DeviceAuthorizationRequest {
            client_id: "test_client".to_string(),
            scope: None,
        };

        let response = manager.create_authorization(request).await.unwrap();

        // Poll should return authorization_pending
        let result = manager.poll_authorization(&response.device_code).await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("authorization_pending"));
    }

    #[tokio::test]
    async fn test_authorize_and_poll() {
        let manager = create_test_manager();

        let request = DeviceAuthorizationRequest {
            client_id: "test_client".to_string(),
            scope: Some("openid".to_string()),
        };

        let response = manager.create_authorization(request).await.unwrap();

        // Authorize the device
        manager
            .authorize_device(&response.user_code, "user_123")
            .await
            .unwrap();

        // Poll should now succeed
        let stored = manager
            .poll_authorization(&response.device_code)
            .await
            .unwrap();
        assert_eq!(stored.status, DeviceAuthorizationStatus::Authorized);
        assert_eq!(stored.user_id, Some("user_123".to_string()));
    }

    #[tokio::test]
    async fn test_deny_device() {
        let manager = create_test_manager();

        let request = DeviceAuthorizationRequest {
            client_id: "test_client".to_string(),
            scope: None,
        };

        let response = manager.create_authorization(request).await.unwrap();

        // Deny the device
        manager.deny_device(&response.user_code).await.unwrap();

        // Poll should return access_denied
        let result = manager.poll_authorization(&response.device_code).await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("access_denied"));
    }

    #[tokio::test]
    async fn test_slow_down() {
        let manager = create_test_manager();

        let request = DeviceAuthorizationRequest {
            client_id: "test_client".to_string(),
            scope: None,
        };

        let response = manager.create_authorization(request).await.unwrap();

        // First poll
        let _ = manager.poll_authorization(&response.device_code).await;

        // Immediate second poll should return slow_down
        let result = manager.poll_authorization(&response.device_code).await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("slow_down"));
    }

    #[tokio::test]
    async fn test_expiration() {
        let storage = Arc::new(MemoryStorage::new());
        // Create manager with very short expiration
        let manager = DeviceAuthManager::with_settings(
            storage,
            "https://example.com/device".to_string(),
            Duration::from_millis(100),
            Duration::from_secs(1),
        );

        let request = DeviceAuthorizationRequest {
            client_id: "test_client".to_string(),
            scope: None,
        };

        let response = manager.create_authorization(request).await.unwrap();

        // Wait for expiration
        sleep(Duration::from_millis(150)).await;

        // Poll should return expired
        let result = manager.poll_authorization(&response.device_code).await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("expired"));
    }

    #[tokio::test]
    async fn test_chainable_expiration_and_interval() {
        let storage = Arc::new(MemoryStorage::new());
        let manager = DeviceAuthManager::new(storage, "https://example.com/device".to_string())
            .expiration(Duration::from_secs(300))
            .interval(Duration::from_secs(10));

        let request = DeviceAuthorizationRequest {
            client_id: "test_client".to_string(),
            scope: None,
        };

        let response = manager.create_authorization(request).await.unwrap();
        assert_eq!(response.expires_in, 300);
        assert_eq!(response.interval, 10);
    }
}