auth-framework 0.4.2

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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
# API Reference Guide


This comprehensive API reference covers all public interfaces, traits, and structures in auth-framework.

## Core Framework


### AuthFramework


The main entry point for the authentication framework.

```rust
pub struct AuthFramework {
    // Storage is handled internally as MemoryStorage
    // private fields...
}
```

#### Constructor


```rust
impl AuthFramework {
    pub fn new(config: AuthConfig) -> Self
}
```

**Parameters:**

- `config`: Authentication configuration

**Returns:** `AuthFramework` instance

**Example:**

```rust
let config = AuthConfig::new();
let mut auth = AuthFramework::new(config);
```

#### Core Methods


##### Method Registration


```rust
pub fn register_method(&mut self, name: impl Into<String>, method: AuthMethodEnum)
```

Registers an authentication method with the framework.

**Parameters:**

- `name`: Unique name for the authentication method
- `method`: Authentication method wrapped in `AuthMethodEnum`

**Example:**

```rust
use auth_framework::methods::{JwtMethod, AuthMethodEnum};

let jwt_method = JwtMethod::new().secret_key("key").issuer("issuer");
auth.register_method("jwt", AuthMethodEnum::Jwt(jwt_method));
```

##### Framework Initialization


```rust
pub async fn initialize(&mut self) -> Result<()>
```

Initializes the authentication framework. Must be called before using the framework.

**Example:**

```rust
auth.initialize().await?;
```

##### Authentication


```rust
pub async fn authenticate(
    &self,
    method_name: impl Into<String>,
    credential: Credential,
) -> Result<AuthResult>
```

Authenticates a credential using the specified method.

**Parameters:**

- `method_name`: Name of the registered authentication method
- `credential`: Credential to authenticate

**Returns:** `Result<AuthResult, AuthError>` where `AuthResult` can be:

- `Success(AuthToken)` - Authentication successful
- `MfaRequired(MfaChallenge)` - Multi-factor authentication required
- `Failure(String)` - Authentication failed

**Example:**

```rust
use auth_framework::credentials::Credential;

let credential = Credential::password("user123", "password");
match auth.authenticate("jwt", credential).await? {
    AuthResult::Success(token) => println!("Login successful"),
    AuthResult::MfaRequired(challenge) => println!("MFA required"),
    AuthResult::Failure(reason) => println!("Login failed: {}", reason),
}
```

##### Token Validation


```rust
pub async fn validate_token(&self, token: &AuthToken) -> Result<bool>
```

Validates an authentication token.

**Parameters:**

- `token`: The authentication token to validate

**Returns:** `Result<bool, AuthError>`

**Example:**

```rust
if auth.validate_token(&token).await? {
    println!("Token is valid");
}
```

##### Token Creation (Testing)


```rust
pub async fn create_auth_token(
    &self,
    user_id: impl Into<String>,
    scopes: Vec<String>,
    method_name: impl Into<String>,
    lifetime: Option<Duration>,
) -> Result<AuthToken>
```

Creates an authentication token directly (primarily for testing).

**Parameters:**

- `user_id`: User identifier
- `scopes`: List of permissions/scopes
- `method_name`: Name of the authentication method
- `lifetime`: Optional token lifetime

**Returns:** `Result<AuthToken, AuthError>`

**Example:**

```rust
let claims = auth.verify_token(&token.access_token).await?;
println!("User ID from token: {}", claims.sub);
```

##### Token Refresh


```rust
pub async fn refresh_token(&self, refresh_token: &str) -> Result<AuthToken>
```

Generates a new access token using a refresh token.

**Parameters:**

- `refresh_token`: Valid refresh token

**Returns:** `Result<AuthToken, AuthError>`

**Example:**

```rust
let new_token = auth.refresh_token(&old_token.refresh_token.unwrap()).await?;
```

##### Permission Management


```rust
pub async fn assign_permission(
    &self,
    user_id: &str,
    permission: &str
) -> Result<()>

pub async fn revoke_permission(
    &self,
    user_id: &str,
    permission: &str
) -> Result<()>

pub async fn has_permission(
    &self,
    user_id: &str,
    permission: &str
) -> Result<bool>

pub async fn get_user_permissions(&self, user_id: &str) -> Result<Vec<String>>
```

**Example:**

```rust
// Assign permission
auth.assign_permission("john_doe", "admin").await?;

// Check permission
let has_admin = auth.has_permission("john_doe", "admin").await?;

// Get all permissions
let permissions = auth.get_user_permissions("john_doe").await?;
```

##### Role Management


```rust
pub async fn assign_role(&self, user_id: &str, role: &str) -> Result<()>
pub async fn revoke_role(&self, user_id: &str, role: &str) -> Result<()>
pub async fn has_role(&self, user_id: &str, role: &str) -> Result<bool>
pub async fn get_user_roles(&self, user_id: &str) -> Result<Vec<String>>
```

**Example:**

```rust
// Assign role
auth.assign_role("john_doe", "moderator").await?;

// Check role
let is_moderator = auth.has_role("john_doe", "moderator").await?;
```

## Storage Traits and Types


### AuthStorage Trait


The core storage abstraction that all storage backends must implement.

```rust
#[async_trait]

pub trait AuthStorage: Send + Sync + Clone {
    // Token operations
    async fn store_token(&self, token: &AuthToken) -> Result<()>;
    async fn get_token(&self, token_id: &str) -> Result<Option<AuthToken>>;
    async fn get_token_by_access_token(&self, access_token: &str) -> Result<Option<AuthToken>>;
    async fn update_token(&self, token: &AuthToken) -> Result<()>;
    async fn delete_token(&self, token_id: &str) -> Result<()>;
    async fn list_user_tokens(&self, user_id: &str) -> Result<Vec<AuthToken>>;

    // Session operations
    async fn store_session(&self, session_id: &str, data: &SessionData) -> Result<()>;
    async fn get_session(&self, session_id: &str) -> Result<Option<SessionData>>;
    async fn delete_session(&self, session_id: &str) -> Result<()>;

    // Key-value operations
    async fn store_kv(&self, key: &str, value: &[u8], ttl: Option<Duration>) -> Result<()>;
    async fn get_kv(&self, key: &str) -> Result<Option<Vec<u8>>>;
    async fn delete_kv(&self, key: &str) -> Result<()>;

    // Maintenance
    async fn cleanup_expired(&self) -> Result<()>;
}
```

### Storage Implementations


#### InMemoryStorage


Fast in-memory storage for development and testing.

```rust
pub struct InMemoryStorage {
    // private fields...
}

impl InMemoryStorage {
    pub fn new() -> Self
    pub fn with_config(cleanup_interval: Duration, default_ttl: Duration) -> Self
}
```

**Configuration:**

```rust
pub struct InMemoryConfig {
    pub cleanup_interval: Duration,
    pub default_ttl: Duration,
}

impl InMemoryConfig {
    pub fn new() -> Self
    pub fn with_cleanup_interval(mut self, interval: Duration) -> Self
    pub fn with_default_ttl(mut self, ttl: Duration) -> Self
    pub fn build(self) -> InMemoryStorage
}
```

**Example:**

```rust
// Basic usage
let storage = InMemoryStorage::new();

// Custom configuration
let storage = InMemoryConfig::new()
    .with_cleanup_interval(Duration::from_secs(60))
    .with_default_ttl(Duration::from_secs(1800))
    .build();
```

#### RedisStorage (Feature: "redis")


High-performance Redis-backed storage.

```rust
#[cfg(feature = "redis")]

pub struct RedisStorage {
    // private fields...
}

#[cfg(feature = "redis")]

impl RedisStorage {
    pub async fn new(url: &str) -> Result<Self>
    pub async fn with_config(config: RedisConfig) -> Result<Self>
}
```

**Configuration:**

```rust
#[cfg(feature = "redis")]

pub struct RedisConfig {
    pub url: String,
    pub pool_size: u32,
    pub timeout: Duration,
    pub key_prefix: String,
    pub default_ttl: Duration,
}
```

**Example:**

```rust
// Basic usage
let storage = RedisStorage::new("redis://localhost:6379").await?;

// Custom configuration
let config = RedisConfig {
    url: "redis://localhost:6379".to_string(),
    pool_size: 20,
    timeout: Duration::from_secs(5),
    key_prefix: "auth:".to_string(),
    default_ttl: Duration::from_secs(3600),
};
let storage = RedisStorage::with_config(config).await?;
```

#### PostgresStorage (Feature: "postgres")


Robust PostgreSQL storage for production.

```rust
#[cfg(feature = "postgres")]

pub struct PostgresStorage {
    // private fields...
}

#[cfg(feature = "postgres")]

impl PostgresStorage {
    pub async fn new(database_url: &str) -> Result<Self>
    pub async fn with_config(config: PostgresConfig) -> Result<Self>
}
```

## Core Types


### AuthToken


Represents an authentication token with metadata.

```rust
#[derive(Debug, Clone, Serialize, Deserialize)]

pub struct AuthToken {
    pub token_id: String,
    pub user_id: String,
    pub access_token: String,
    pub refresh_token: Option<String>,
    pub token_type: String,
    pub expires_at: DateTime<Utc>,
    pub created_at: DateTime<Utc>,
    pub permissions: Vec<String>,
    pub roles: Vec<String>,
    pub metadata: serde_json::Value,
}
```

### TokenClaims


JWT token claims structure.

```rust
#[derive(Debug, Clone, Serialize, Deserialize)]

pub struct TokenClaims {
    pub sub: String,        // Subject (user ID)
    pub exp: i64,          // Expiration timestamp
    pub iat: i64,          // Issued at timestamp
    pub permissions: Vec<String>,
    pub roles: Vec<String>,
    pub metadata: serde_json::Value,
}
```

### UserCredentials


User credential information.

```rust
#[derive(Debug, Clone)]

pub struct UserCredentials {
    pub user_id: String,
    pub password_hash: String,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub is_active: bool,
    pub permissions: Vec<String>,
    pub roles: Vec<String>,
}
```

### SessionData


Session storage structure.

```rust
#[derive(Debug, Clone, Serialize, Deserialize)]

pub struct SessionData {
    pub user_id: String,
    pub data: serde_json::Value,
    pub created_at: DateTime<Utc>,
    pub last_accessed: DateTime<Utc>,
}
```

## Configuration


### AuthConfig


Main framework configuration.

```rust
#[derive(Debug, Clone)]

pub struct AuthConfig {
    pub jwt_secret: String,
    pub jwt_algorithm: Algorithm,
    pub token_expiry: Duration,
    pub refresh_token_expiry: Duration,
    pub session_ttl: Duration,
    pub password_hash_cost: u32,
    pub max_login_attempts: u32,
    pub lockout_duration: Duration,
    // ... additional fields
}
```

#### Builder Pattern


```rust
impl AuthConfig {
    pub fn builder() -> AuthConfigBuilder
    pub fn default() -> Self
}

pub struct AuthConfigBuilder {
    // private fields...
}

impl AuthConfigBuilder {
    pub fn jwt_secret(mut self, secret: String) -> Self
    pub fn jwt_algorithm(mut self, algorithm: Algorithm) -> Self
    pub fn token_expiry(mut self, expiry: Duration) -> Self
    pub fn refresh_token_expiry(mut self, expiry: Duration) -> Self
    pub fn session_ttl(mut self, ttl: Duration) -> Self
    pub fn password_hash_cost(mut self, cost: u32) -> Self
    pub fn max_login_attempts(mut self, attempts: u32) -> Self
    pub fn lockout_duration(mut self, duration: Duration) -> Self
    pub fn build(self) -> AuthConfig
}
```

**Example:**

```rust
let config = AuthConfig::builder()
    .jwt_secret("your-secret-key".to_string())
    .token_expiry(Duration::hours(24))
    .refresh_token_expiry(Duration::days(30))
    .session_ttl(Duration::hours(2))
    .password_hash_cost(12)
    .max_login_attempts(5)
    .lockout_duration(Duration::minutes(15))
    .build();
```

### RSA Key Format Support


When using RSA keys for JWT signing and token management, the framework supports both standard PEM formats:

#### Supported Formats


- **PKCS#1 Format**: `-----BEGIN RSA PRIVATE KEY-----` (traditional RSA format)
- **PKCS#8 Format**: `-----BEGIN PRIVATE KEY-----` (modern standard format, recommended)

#### TokenManager with RSA Keys


```rust
use auth_framework::tokens::TokenManager;

// Both PKCS#1 and PKCS#8 formats are automatically detected
let private_key = std::fs::read("private.pem")?;
let public_key = std::fs::read("public.pem")?;

let token_manager = TokenManager::new_rsa(
    &private_key,
    &public_key,
    "issuer",
    "audience"
)?;
```

#### Key Generation Examples


```bash
# PKCS#1 format (traditional)

openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -pubout -out public.pem

# PKCS#8 format (recommended)

openssl genpkey -algorithm RSA -out private_pkcs8.pem -pkcs8
openssl pkey -in private_pkcs8.pem -pubout -out public_spki.pem
```

**Note**: No format conversion is required - both formats are automatically detected and parsed.

## Web Framework Integrations


### Actix-web Integration (Feature: "actix-web")


```rust
#[cfg(feature = "actix-web")]

pub mod actix_web {
    use actix_web::{FromRequest, HttpRequest, Result as ActixResult};

    // Middleware
    pub struct AuthMiddleware {
        // private fields...
    }

    impl AuthMiddleware {
        pub fn new() -> Self
        pub fn with_config(config: AuthConfig) -> Self
    }

    // Extractors
    pub struct AuthenticatedUser {
        pub user_id: String,
        pub permissions: Vec<String>,
        pub roles: Vec<String>,
        pub token_claims: TokenClaims,
    }

    impl FromRequest for AuthenticatedUser {
        type Error = actix_web::Error;
        type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>;

        fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
    }

    // Permission guards
    pub struct RequirePermission<const PERMISSION: &'static str>;

    impl<const PERMISSION: &'static str> FromRequest for RequirePermission<PERMISSION> {
        type Error = actix_web::Error;
        type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>;

        fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
    }

    // Role guards
    pub struct RequireRole<const ROLE: &'static str>;
}
```

**Usage Example:**

```rust
use actix_web::{web, App, HttpServer, Result};
use auth_framework::integrations::actix_web::{
    AuthMiddleware, AuthenticatedUser, RequirePermission
};

#[actix_web::main]

async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .wrap(AuthMiddleware::new())
            .route("/profile", web::get().to(get_profile))
            .route("/admin", web::get().to(admin_only))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

async fn get_profile(user: AuthenticatedUser) -> Result<String> {
    Ok(format!("User: {}", user.user_id))
}

async fn admin_only(
    _user: AuthenticatedUser,
    _admin: RequirePermission<"admin">
) -> Result<String> {
    Ok("Admin panel".to_string())
}
```

### Warp Integration (Feature: "warp")


```rust
#[cfg(feature = "warp")]

pub mod warp {
    use warp::{Filter, Rejection};

    pub fn with_auth<S: AuthStorage>(
        auth: AuthFramework<S>
    ) -> impl Filter<Extract = (AuthenticatedUser,), Error = Rejection> + Clone

    pub fn require_permission(
        permission: &'static str
    ) -> impl Filter<Extract = (), Error = Rejection> + Clone

    pub fn require_role(
        role: &'static str
    ) -> impl Filter<Extract = (), Error = Rejection> + Clone

    // Types
    pub struct AuthenticatedUser {
        pub user_id: String,
        pub permissions: Vec<String>,
        pub roles: Vec<String>,
        pub token_claims: TokenClaims,
    }

    // Custom rejection types
    #[derive(Debug)]
    pub struct AuthenticationError;
    impl warp::reject::Reject for AuthenticationError {}

    #[derive(Debug)]
    pub struct PermissionError;
    impl warp::reject::Reject for PermissionError {}
}
```

**Usage Example:**

```rust
use warp::Filter;
use auth_framework::integrations::warp::{with_auth, require_permission};

#[tokio::main]

async fn main() {
    let auth = /* initialize auth framework */;
    let auth_filter = with_auth(auth);

    let profile = warp::path("profile")
        .and(warp::get())
        .and(auth_filter.clone())
        .map(|user: AuthenticatedUser| {
            format!("User: {}", user.user_id)
        });

    let admin = warp::path("admin")
        .and(warp::get())
        .and(auth_filter)
        .and(require_permission("admin"))
        .map(|user: AuthenticatedUser| {
            "Admin panel"
        });

    let routes = profile.or(admin);
    warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}
```

### Rocket Integration (Feature: "rocket")


```rust
#[cfg(feature = "rocket")]

pub mod rocket {
    use rocket::{Request, request::{self, FromRequest}};

    // Request guards
    pub struct AuthenticatedUser {
        pub user_id: String,
        pub permissions: Vec<String>,
        pub roles: Vec<String>,
        pub token_claims: TokenClaims,
    }

    #[rocket::async_trait]
    impl<'r> FromRequest<'r> for AuthenticatedUser {
        type Error = ();

        async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error>
    }

    pub struct RequirePermission<const PERMISSION: &'static str>;

    #[rocket::async_trait]
    impl<'r, const PERMISSION: &'static str> FromRequest<'r> for RequirePermission<PERMISSION> {
        type Error = ();

        async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error>
    }

    pub struct RequireRole<const ROLE: &'static str>;

    // Fairings
    pub struct AuthFairing {
        // private fields...
    }

    impl AuthFairing {
        pub fn new() -> Self
        pub fn with_config(config: AuthConfig) -> Self
    }
}
```

**Usage Example:**

```rust
use rocket::{get, launch, routes, State};
use auth_framework::integrations::rocket::{AuthenticatedUser, RequirePermission};

#[get("/profile")]

fn get_profile(user: AuthenticatedUser) -> String {
    format!("User: {}", user.user_id)
}

#[get("/admin")]

fn admin_only(_user: AuthenticatedUser, _admin: RequirePermission<"admin">) -> &'static str {
    "Admin panel"
}

#[launch]

async fn rocket() -> _ {
    rocket::build()
        .manage(/* auth framework instance */)
        .mount("/", routes![get_profile, admin_only])
}
```

## Error Handling


### AuthError


The main error type for all authentication operations.

```rust
#[derive(Debug, thiserror::Error)]

pub enum AuthError {
    #[error("Invalid credentials")]
    InvalidCredentials,

    #[error("Token expired")]
    TokenExpired,

    #[error("Token invalid")]
    TokenInvalid,

    #[error("User not found")]
    UserNotFound,

    #[error("Permission denied")]
    PermissionDenied,

    #[error("Storage error: {0}")]
    StorageError(String),

    #[error("Configuration error: {0}")]
    ConfigError(String),

    #[error("Serialization error: {0}")]
    SerializationError(String),

    #[error("Internal error: {0}")]
    InternalError(String),
}
```

### Result Type


Convenience type alias for operations that may fail.

```rust
pub type Result<T> = std::result::Result<T, AuthError>;
```

## Advanced Features


### Multi-Factor Authentication


```rust
use auth_framework::methods::MfaAuth;

pub struct MfaAuth<S: AuthStorage> {
    // private fields...
}

impl<S: AuthStorage> MfaAuth<S> {
    pub fn new(storage: S) -> Self

    // TOTP methods
    pub async fn enable_totp(&self, user_id: &str) -> Result<String>
    pub async fn verify_totp(&self, user_id: &str, code: &str) -> Result<bool>
    pub async fn disable_totp(&self, user_id: &str) -> Result<()>

    // SMS methods
    pub async fn send_sms_code(&self, user_id: &str, phone: &str) -> Result<()>
    pub async fn verify_sms_code(&self, user_id: &str, code: &str) -> Result<bool>

    // Backup codes
    pub async fn generate_backup_codes(&self, user_id: &str) -> Result<Vec<String>>
    pub async fn use_backup_code(&self, user_id: &str, code: &str) -> Result<bool>
}
```

### API Key Management


```rust
use auth_framework::methods::ApiKeyAuth;

pub struct ApiKeyAuth<S: AuthStorage> {
    // private fields...
}

pub struct ApiKey {
    pub key: String,
    pub user_id: String,
    pub name: String,
    pub permissions: Vec<String>,
    pub expires_at: Option<DateTime<Utc>>,
    pub created_at: DateTime<Utc>,
    pub last_used: Option<DateTime<Utc>>,
}

impl<S: AuthStorage> ApiKeyAuth<S> {
    pub fn new(storage: S) -> Self

    pub async fn create_api_key(
        &self,
        user_id: &str,
        name: &str,
        expires_at: Option<DateTime<Utc>>
    ) -> Result<ApiKey>

    pub async fn verify_api_key(&self, key: &str) -> Result<Option<ApiKey>>
    pub async fn revoke_api_key(&self, key: &str) -> Result<()>
    pub async fn list_user_api_keys(&self, user_id: &str) -> Result<Vec<ApiKey>>
}
```

### OAuth2 Device Flow


```rust
use auth_framework::methods::enhanced_device::{EnhancedDeviceFlow, DeviceFlowConfig};

pub struct DeviceFlowConfig {
    pub client_id: String,
    pub verification_uri: String,
    pub verification_uri_complete: Option<String>,
    pub device_code_expiry: Duration,
    pub poll_interval: Duration,
    pub scope: Option<String>,
}

pub struct DeviceAuthorizationResponse {
    pub device_code: String,
    pub user_code: String,
    pub verification_uri: String,
    pub verification_uri_complete: Option<String>,
    pub expires_in: u64,
    pub interval: u64,
}

pub struct DeviceTokenResponse {
    pub access_token: String,
    pub token_type: String,
    pub expires_in: u64,
    pub refresh_token: Option<String>,
    pub scope: Option<String>,
}

impl<S: AuthStorage> EnhancedDeviceFlow<S> {
    pub fn new(storage: S, config: DeviceFlowConfig) -> Self

    pub async fn start_authorization(&self) -> Result<DeviceAuthorizationResponse>

    pub async fn poll_for_token(&self, device_code: &str) -> Result<DeviceTokenResponse>

    pub async fn complete_authorization(
        &self,
        device_code: &str,
        user_id: &str,
        permissions: Vec<String>
    ) -> Result<()>
}
```

## Testing Utilities


```rust
#[cfg(feature = "testing")]

pub mod testing {
    use crate::*;

    pub struct TestAuthFramework {
        // private fields...
    }

    impl TestAuthFramework {
        pub async fn new() -> Self
        pub async fn with_config(config: AuthConfig) -> Self

        // Helper methods for testing
        pub async fn create_test_user(&self, user_id: &str) -> Result<UserCredentials>
        pub async fn create_test_token(&self, user_id: &str) -> Result<AuthToken>
        pub fn create_expired_token(&self, user_id: &str) -> AuthToken
    }

    pub mod helpers {
        use super::*;

        pub fn create_test_user(user_id: &str) -> UserCredentials
        pub fn create_test_token(user_id: &str) -> AuthToken
        pub fn create_test_claims(user_id: &str) -> TokenClaims
        pub fn create_test_config() -> AuthConfig
    }
}
```

## Feature Flags


The following Cargo features are available:

| Feature | Description | Default |
|---------|-------------|---------|
| `actix-web` | Actix-web framework integration | No |
| `warp` | Warp framework integration | No |
| `rocket` | Rocket framework integration | No |
| `redis` | Redis storage backend | No |
| `postgres` | PostgreSQL storage backend | No |
| `testing` | Testing utilities | No |
| `full` | Enable all features | No |

**Example Cargo.toml:**

```toml
[dependencies]
auth-framework = { version = "0.1.0", features = ["actix-web", "redis", "testing"] }
```

This API reference provides comprehensive coverage of all public interfaces in auth-framework. For implementation examples, see the examples directory and integration guides.