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
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
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
//! Authentication API Endpoints
//!
//! Handles login, logout, token refresh, and related authentication operations

use crate::api::{ApiResponse, ApiState, extract_bearer_token};
use axum::{Json, extract::State, http::HeaderMap};
use serde::{Deserialize, Serialize};

/// Login request payload.
///
/// When `challenge_id` and `mfa_code` are both present the login completes an
/// in-progress MFA challenge instead of performing a fresh password check.
/// The two fields must appear together — providing one without the other is
/// rejected with a validation error.
#[derive(Debug, Deserialize)]
pub struct LoginRequest {
    pub username: String,
    pub password: String,
    /// Identifier of a pending MFA challenge (returned by a prior login attempt).
    #[serde(default)]
    pub challenge_id: Option<String>,
    /// The TOTP or backup code that answers the MFA challenge.
    #[serde(default)]
    pub mfa_code: Option<String>,
    /// When `true`, the session lifetime is extended (e.g. "remember me" cookie).
    #[serde(default)]
    pub remember_me: bool,
}

/// Successful login response.
///
/// Contains access and refresh tokens plus user metadata and adaptive risk
/// information.  Clients should inspect `login_risk_level` and
/// `security_warnings` to decide whether to prompt for additional verification.
#[derive(Debug, Serialize)]
pub struct LoginResponse {
    pub access_token: String,
    pub refresh_token: String,
    pub token_type: String,
    pub expires_in: u64,
    pub user: LoginUserInfo,
    /// Risk level of this login attempt: "low", "medium", "high", or "critical".
    /// Clients can use this to prompt the user to enable MFA or perform additional verification.
    pub login_risk_level: String,
    /// Non-blocking security advisories for the authenticated session.
    /// Empty in the common case; populated when adaptive risk policy detects elevated risk.
    pub security_warnings: Vec<String>,
}

/// User information embedded in login and validation responses.
#[derive(Debug, Serialize)]
pub struct LoginUserInfo {
    pub id: String,
    pub username: String,
    pub roles: Vec<String>,
    pub permissions: Vec<String>,
}

async fn build_login_response(
    state: &ApiState,
    user_id: &str,
    username: String,
    permissions: Vec<String>,
) -> ApiResponse<LoginResponse> {
    let user_key = format!("user:{}", user_id);
    let roles: Vec<String> = match state.auth_framework.storage().get_kv(&user_key).await {
        Ok(Some(bytes)) => {
            let json: serde_json::Value = serde_json::from_slice(&bytes).unwrap_or_default();
            json["roles"]
                .as_array()
                .map(|arr| {
                    arr.iter()
                        .filter_map(|value| value.as_str())
                        .map(|value| value.to_string())
                        .collect()
                })
                .unwrap_or_default()
        }
        _ => vec![],
    };

    let user_info = LoginUserInfo {
        id: user_id.to_string(),
        username,
        roles: roles.clone(),
        permissions,
    };

    let token_lifetime = state.auth_framework.config().token_lifetime;
    let access_token = match state.auth_framework.token_manager().create_jwt_token(
        user_id,
        roles,
        Some(token_lifetime),
    ) {
        Ok(jwt) => jwt,
        Err(e) => {
            tracing::error!("Failed to create JWT token: {}", e);
            return ApiResponse::error_typed(
                "TOKEN_CREATION_FAILED",
                "Failed to create access token",
            );
        }
    };

    let refresh_token_lifetime = state.auth_framework.config().refresh_token_lifetime;
    let refresh_token = match state.auth_framework.token_manager().create_jwt_token(
        user_id,
        vec!["refresh".to_string()],
        Some(refresh_token_lifetime),
    ) {
        Ok(jwt) => jwt,
        Err(e) => {
            tracing::error!("Failed to create refresh token: {}", e);
            return ApiResponse::error_typed(
                "TOKEN_CREATION_FAILED",
                "Failed to create refresh token",
            );
        }
    };

    ApiResponse::success(LoginResponse {
        access_token,
        refresh_token,
        token_type: "Bearer".to_string(),
        expires_in: token_lifetime.as_secs(),
        user: user_info,
        login_risk_level: "low".to_string(), // Caller may override after build
        security_warnings: Vec::new(),       // Caller may override after build
    })
}

/// Token refresh request.
#[derive(Debug, Deserialize)]
pub struct RefreshRequest {
    /// The refresh token issued during a previous login.
    pub refresh_token: String,
}

/// Token refresh response.
#[derive(Debug, Serialize)]
pub struct RefreshResponse {
    /// The newly issued access token.
    pub access_token: String,
    /// Always `"Bearer"`.
    pub token_type: String,
    /// Seconds until the new access token expires.
    pub expires_in: u64,
}

/// Logout request.
#[derive(Debug, Deserialize)]
pub struct LogoutRequest {
    /// Optional refresh token to revoke alongside the access token.
    #[serde(default)]
    pub refresh_token: Option<String>,
}

/// Compute a login risk level string from request headers.
///
/// This is a lightweight, header-only heuristic used to populate the
/// `login_risk_level` field in the login response and to trigger security
/// warnings when MFA is not enrolled.  It does **not** replace a full
/// risk-engine evaluation — for that, see `AuthorizationContextBuilder`.
pub(crate) fn login_risk_level(headers: &HeaderMap) -> (&'static str, Vec<String>) {
    let user_agent = headers
        .get("user-agent")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("");

    let forwarded_for = headers
        .get("x-forwarded-for")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("");

    let mut risk_points: u8 = 0;
    let mut warnings: Vec<String> = Vec::new();

    if user_agent.is_empty() {
        risk_points = risk_points.saturating_add(30);
        warnings.push(
            "No browser User-Agent detected; this request may originate from an automated script."
                .to_string(),
        );
    }

    // Detect Tor exit nodes by well-known header patterns.  This is a best-effort
    // check and can be defeated; a proper geo-IP database lookup is more reliable.
    if user_agent.to_lowercase().contains("tor browser") {
        risk_points = risk_points.saturating_add(40);
        warnings.push("Login originated from the Tor Browser.".to_string());
    }

    // Multiple IPs in X-Forwarded-For typically indicate a proxy or VPN hop.
    let hop_count = forwarded_for.split(',').count();
    if hop_count >= 2 {
        risk_points = risk_points.saturating_add(15);
        warnings.push(format!(
            "Request passed through {} proxy hops (X-Forwarded-For).",
            hop_count
        ));
    }

    let level = match risk_points {
        0..=9 => "low",
        10..=29 => "medium",
        30..=59 => "high",
        _ => "critical",
    };
    (level, warnings)
}

/// Increment the per-username failed login counter with a sliding TTL window.
async fn increment_login_failure(state: &ApiState, lockout_key: &str, window_secs: u64) {
    let current: u64 = match state.auth_framework.storage().get_kv(lockout_key).await {
        Ok(Some(bytes)) => std::str::from_utf8(&bytes)
            .ok()
            .and_then(|s| s.parse().ok())
            .unwrap_or(0),
        _ => 0,
    };
    let new_count = current.saturating_add(1);
    let _ = state
        .auth_framework
        .storage()
        .store_kv(
            lockout_key,
            new_count.to_string().as_bytes(),
            Some(std::time::Duration::from_secs(window_secs)),
        )
        .await;
}

/// `POST /auth/login` — authenticate a user with username/password.
///
/// On success returns access + refresh tokens, user metadata, and adaptive
/// risk information.  Returns `ACCOUNT_LOCKED` after 5 consecutive failures
/// within 15 minutes.
pub async fn login(
    State(state): State<ApiState>,
    headers: HeaderMap,
    Json(req): Json<LoginRequest>,
) -> ApiResponse<LoginResponse> {
    // Validate required fields
    if req.username.is_empty() || req.password.is_empty() {
        return ApiResponse::validation_error_typed("Username and password are required");
    }

    if req.challenge_id.is_some() ^ req.mfa_code.is_some() {
        return ApiResponse::validation_error_typed(
            "challenge_id and mfa_code must be provided together",
        );
    }

    if let (Some(challenge_id), Some(mfa_code)) =
        (req.challenge_id.clone(), req.mfa_code.as_deref())
    {
        return match state
            .auth_framework
            .complete_mfa_by_id(&challenge_id, mfa_code)
            .await
        {
            Ok(token) => {
                let mut response = build_login_response(
                    &state,
                    &token.user_id,
                    req.username,
                    token.permissions.to_vec(),
                )
                .await;
                // MFA completion means the step-up was satisfied — mark as low risk.
                if let Some(data) = response.data.as_mut() {
                    data.login_risk_level = "low".to_string();
                }
                response
            }
            Err(e) => {
                tracing::debug!("MFA completion failed during login: {}", e);
                ApiResponse::error_typed(
                    "MFA_INVALID_CODE",
                    "Invalid or expired MFA challenge or code",
                )
            }
        };
    }

    // Compute adaptive risk from request headers before touching the auth core
    // so the risk assessment can influence the response even on success.
    let (risk_level, mut risk_warnings) = login_risk_level(&headers);

    // SEC-M1/L1: Per-username failed login rate limiting / account lockout.
    // Key: "login_failures:{username}" — stores the current failure count as a
    // decimal string with a 15-minute TTL (auto-resets after 15 min of no attempts).
    let lockout_key = format!("login_failures:{}", req.username);
    const MAX_FAILED_ATTEMPTS: u64 = 5;
    const LOCKOUT_WINDOW_SECS: u64 = 900; // 15 minutes
    if let Ok(Some(count_bytes)) = state.auth_framework.storage().get_kv(&lockout_key).await {
        if let Ok(count_str) = std::str::from_utf8(&count_bytes) {
            if let Ok(count) = count_str.parse::<u64>() {
                if count >= MAX_FAILED_ATTEMPTS {
                    tracing::warn!(
                        username = %req.username,
                        failed_attempts = count,
                        "Login rejected — account temporarily locked due to repeated failures"
                    );
                    return ApiResponse::error_typed(
                        "ACCOUNT_LOCKED",
                        "Too many failed login attempts. Please try again later.",
                    );
                }
            }
        }
    }

    // Create credential for authentication
    let credential = crate::authentication::credentials::Credential::Password {
        username: req.username.clone(),
        password: req.password.clone(),
    };

    // Attempt authentication
    match state
        .auth_framework
        .authenticate("password", credential)
        .await
    {
        Ok(auth_result) => match auth_result {
            crate::auth::AuthResult::Success(token) => {
                // Check if the user has MFA enrolled.  If not and the risk level is
                // elevated, add a security advisory recommending MFA enrollment.
                let mfa_enrolled =
                    crate::api::mfa::check_user_mfa_status(&state.auth_framework, &token.user_id)
                        .await;

                if !mfa_enrolled && matches!(risk_level, "high" | "critical") {
                    risk_warnings.push(
                        "Your account does not have multi-factor authentication enabled. \
                         Enable MFA to protect this account from high-risk login contexts."
                            .to_string(),
                    );
                    tracing::warn!(
                        user_id = %token.user_id,
                        risk_level = %risk_level,
                        "High-risk login without MFA enrolled"
                    );
                } else {
                    tracing::info!(
                        user_id = %token.user_id,
                        risk_level = %risk_level,
                        mfa_enrolled = %mfa_enrolled,
                        "Successful login"
                    );
                }

                let mut response = build_login_response(
                    &state,
                    &token.user_id,
                    req.username,
                    token.permissions.to_vec(),
                )
                .await;

                // Reset failed login counter on successful authentication
                let _ = state.auth_framework.storage().delete_kv(&lockout_key).await;

                if let Some(data) = response.data.as_mut() {
                    data.login_risk_level = risk_level.to_string();
                    data.security_warnings = risk_warnings;
                }
                response
            }
            crate::auth::AuthResult::MfaRequired(challenge) => {
                // Return the challenge data so the client knows how to fulfill MFA.
                // The client should prompt for the appropriate second factor and then
                // re-submit the login request with both challenge_id and mfa_code.
                let mfa_type_str = match &challenge.mfa_type {
                    crate::methods::MfaType::Totp => "totp",
                    crate::methods::MfaType::Sms { .. } => "sms",
                    crate::methods::MfaType::Email { .. } => "email",
                    crate::methods::MfaType::Push { .. } => "push",
                    crate::methods::MfaType::SecurityKey => "security_key",
                    crate::methods::MfaType::BackupCode => "backup_code",
                    crate::methods::MfaType::MultiMethod => "totp_or_backup_code",
                };
                ApiResponse::<()>::error_with_details(
                    "MFA_REQUIRED",
                    "Multi-factor authentication required",
                    serde_json::json!({
                        "challenge_id": challenge.id,
                        "mfa_type": mfa_type_str,
                        "expires_at": challenge.expires_at.to_rfc3339(),
                        "message": challenge.message,
                    }),
                )
                .cast()
            }
            crate::auth::AuthResult::Failure(reason) => {
                // Increment failed login counter
                increment_login_failure(&state, &lockout_key, LOCKOUT_WINDOW_SECS).await;
                ApiResponse::error_typed("AUTHENTICATION_FAILED", reason)
            }
        },
        Err(e) => {
            // Increment failed login counter
            increment_login_failure(&state, &lockout_key, LOCKOUT_WINDOW_SECS).await;
            // Always return the same error code/message regardless of *why* auth failed.
            // This prevents timing and enumeration attacks that could distinguish
            // "user not found" from "wrong password".
            tracing::debug!(
                "Authentication error (reported as INVALID_CREDENTIALS): {}",
                e
            );
            ApiResponse::error_typed("INVALID_CREDENTIALS", "Invalid username or password")
        }
    }
}

/// `POST /auth/refresh` — exchange a valid refresh token for a new access token.
pub async fn refresh_token(
    State(state): State<ApiState>,
    Json(req): Json<RefreshRequest>,
) -> ApiResponse<RefreshResponse> {
    if req.refresh_token.is_empty() {
        return ApiResponse::validation_error_typed("Invalid request");
    }

    // Validate the refresh token
    match state
        .auth_framework
        .token_manager()
        .validate_jwt_token(&req.refresh_token)
    {
        Ok(claims) => {
            // Check if this is actually a refresh token
            if !claims.scope.contains("refresh") {
                return ApiResponse::error_typed(
                    "INVALID_TOKEN",
                    "Expected a refresh token, but received an access token",
                );
            }

            // SECURITY: Reject revoked refresh tokens (e.g. those already passed to /auth/logout).
            let revocation_key = format!("revoked_token:{}", claims.jti);
            match state.auth_framework.storage().get_kv(&revocation_key).await {
                Ok(Some(_)) => {
                    return ApiResponse::error_typed(
                        "INVALID_TOKEN",
                        "Refresh token has been revoked",
                    );
                }
                Ok(None) => {} // Not revoked — proceed
                Err(e) => {
                    tracing::error!("Refresh token revocation check failed: {}", e);
                    return ApiResponse::error_typed(
                        "INTERNAL_ERROR",
                        "Unable to verify token status",
                    );
                }
            }

            // Create new access token, preserving the user's actual permissions from storage.
            // H-1 fix: do NOT hard-code ["read","write"] — load the real permission set.

            // SECURITY: Reject refresh if the user's account has been deactivated
            // since the refresh token was issued.  Without this check a deactivated
            // user can indefinitely obtain new access tokens.
            {
                let user_key = format!("user:{}", claims.sub);
                if let Ok(Some(user_bytes)) = state.auth_framework.storage().get_kv(&user_key).await
                {
                    let user_json: serde_json::Value =
                        serde_json::from_slice(&user_bytes).unwrap_or_default();
                    let active = user_json["active"].as_bool().unwrap_or(true);
                    if !active {
                        return ApiResponse::error_typed(
                            "ACCOUNT_DEACTIVATED",
                            "Account has been deactivated",
                        );
                    }
                }
            }

            let permissions: Vec<String> = match state
                .auth_framework
                .storage()
                .get_kv(&format!("user_permissions:{}", claims.sub))
                .await
            {
                Ok(Some(data)) => serde_json::from_slice(&data).unwrap_or_default(),
                _ => vec![],
            };

            let token_lifetime = state.auth_framework.config().token_lifetime;
            let new_access_token = match state.auth_framework.token_manager().create_jwt_token(
                &claims.sub,
                permissions,
                Some(token_lifetime),
            ) {
                Ok(jwt) => jwt,
                Err(e) => {
                    tracing::error!("Failed to create new access token: {}", e);
                    return ApiResponse::error_typed(
                        "TOKEN_CREATION_FAILED",
                        "Failed to create new access token",
                    );
                }
            };

            let response = RefreshResponse {
                access_token: new_access_token,
                token_type: "Bearer".to_string(),
                expires_in: token_lifetime.as_secs(),
            };

            ApiResponse::success(response)
        }
        Err(e) => {
            tracing::warn!("Invalid refresh token: {}", e);
            ApiResponse::error_typed("INVALID_TOKEN", "Invalid or expired refresh token")
        }
    }
}

/// `POST /auth/logout` — revoke access and (optionally) refresh tokens.
pub async fn logout(
    State(state): State<ApiState>,
    headers: HeaderMap,
    Json(req): Json<LogoutRequest>,
) -> ApiResponse<()> {
    // Revoke the access token by storing it in the blocklist keyed by JTI
    if let Some(token) = extract_bearer_token(&headers) {
        match state
            .auth_framework
            .token_manager()
            .validate_jwt_token(&token)
        {
            Ok(claims) => {
                let revocation_key = format!("revoked_token:{}", claims.jti);
                // TTL: longer of token's remaining lifetime or 1 hour; cap at 7 days
                let ttl = std::time::Duration::from_secs(7 * 86400);
                if let Err(e) = state
                    .auth_framework
                    .storage()
                    .store_kv(revocation_key.as_str(), b"revoked", Some(ttl))
                    .await
                {
                    tracing::error!("Failed to revoke access token JTI {}: {}", claims.jti, e);
                } else {
                    tracing::info!("Access token revoked (JTI: {})", claims.jti);
                }
            }
            Err(_) => {
                // Token was already invalid; no action needed
                tracing::debug!("Logout called with invalid/expired access token");
            }
        }
    }

    // If refresh token provided, revoke it too
    if let Some(ref refresh_token) = req.refresh_token {
        match state
            .auth_framework
            .token_manager()
            .validate_jwt_token(refresh_token)
        {
            Ok(claims) => {
                let revocation_key = format!("revoked_token:{}", claims.jti);
                let ttl = std::time::Duration::from_secs(7 * 86400);
                if let Err(e) = state
                    .auth_framework
                    .storage()
                    .store_kv(revocation_key.as_str(), b"revoked", Some(ttl))
                    .await
                {
                    tracing::error!("Failed to revoke refresh token JTI {}: {}", claims.jti, e);
                } else {
                    tracing::info!("Refresh token revoked (JTI: {})", claims.jti);
                }
            }
            Err(_) => {
                tracing::debug!("Logout called with invalid/expired refresh token");
            }
        }
    }

    ApiResponse::<()>::ok_with_message("Successfully logged out")
}

/// GET /auth/validate
/// Validate current token and return user information
pub async fn validate_token(
    State(state): State<ApiState>,
    headers: HeaderMap,
) -> ApiResponse<LoginUserInfo> {
    match extract_bearer_token(&headers) {
        Some(token) => {
            match crate::api::validate_api_token(&state.auth_framework, &token).await {
                Ok(auth_token) => {
                    // Fetch actual user information from storage
                    let username = match state
                        .auth_framework
                        .get_user_profile(&auth_token.user_id)
                        .await
                    {
                        Ok(profile) => profile
                            .username
                            .unwrap_or_else(|| format!("user_{}", auth_token.user_id)),
                        Err(_) => format!("user_{}", auth_token.user_id), // Fallback if profile fetch fails
                    };

                    let user_info = LoginUserInfo {
                        id: auth_token.user_id,
                        username,
                        roles: auth_token.roles.to_vec(),
                        permissions: auth_token.permissions.to_vec(),
                    };
                    ApiResponse::success(user_info)
                }
                Err(_e) => ApiResponse::error_typed("AUTH_ERROR", "Token validation failed"),
            }
        }
        None => ApiResponse::unauthorized_typed(),
    }
}

/// `GET /auth/providers` — list available OAuth providers and their authorize URLs.
pub async fn list_providers(State(_state): State<ApiState>) -> ApiResponse<Vec<ProviderInfo>> {
    let providers = vec![
        ProviderInfo {
            name: "google".to_string(),
            display_name: "Google".to_string(),
            auth_url: "/oauth/google".to_string(),
        },
        ProviderInfo {
            name: "github".to_string(),
            display_name: "GitHub".to_string(),
            auth_url: "/oauth/github".to_string(),
        },
        ProviderInfo {
            name: "microsoft".to_string(),
            display_name: "Microsoft".to_string(),
            auth_url: "/oauth/microsoft".to_string(),
        },
    ];

    ApiResponse::success(providers)
}

/// Summary of an available OAuth login provider.
#[derive(Debug, Serialize)]
pub struct ProviderInfo {
    /// Machine-readable identifier (e.g. `"google"`).
    pub name: String,
    /// Human-readable label.
    pub display_name: String,
    /// Relative URL to initiate the OAuth flow.
    pub auth_url: String,
}

/// User registration request.
#[derive(Debug, Deserialize)]
pub struct RegisterRequest {
    /// Desired username (validated for length, characters, must start with a letter).
    pub username: String,
    /// Email address for the new account.
    pub email: String,
    /// Password (validated against minimum complexity rules).
    pub password: String,
}

/// User registration response.
#[derive(Debug, Serialize)]
pub struct RegisterResponse {
    /// System-generated unique user identifier.
    pub user_id: String,
    /// The registered username.
    pub username: String,
    /// The registered email.
    pub email: String,
}

/// `POST /auth/register` — create a new user account.
///
/// Validates username format, password complexity, and email uniqueness
/// before persisting the new user.
pub async fn register(
    State(state): State<ApiState>,
    Json(req): Json<RegisterRequest>,
) -> ApiResponse<RegisterResponse> {
    // Validate required fields
    if req.username.is_empty() || req.password.is_empty() || req.email.is_empty() {
        return ApiResponse::validation_error_typed("Username, password, and email are required");
    }

    // Validate username format (length, allowed characters, must start with letter).
    if let Err(e) = crate::utils::validation::validate_username(&req.username) {
        return ApiResponse::validation_error_typed(format!("{e}"));
    }

    // Validate password strength (min 8 characters with complexity)
    if let Err(e) = crate::utils::validation::validate_password(&req.password) {
        return ApiResponse::validation_error_typed(format!("{e}"));
    }

    // Check password against known data breaches (HIBP k-anonymity API)
    match crate::utils::breach_check::is_password_breached(&req.password).await {
        Ok(true) => {
            return ApiResponse::validation_error_typed(
                "This password has appeared in a known data breach. Please choose a different password.",
            );
        }
        Ok(false) => {} // Password not breached, continue
        Err(_) => {}    // HIBP API unreachable; fail open to avoid blocking registration
    }

    // Validate email format
    if let Err(e) = crate::utils::validation::validate_email(&req.email) {
        return ApiResponse::validation_error_typed(format!("{e}"));
    }

    // Check if username already exists
    let username_key = format!("user:credentials:{}", req.username);
    match state.auth_framework.storage().get_kv(&username_key).await {
        Ok(Some(_)) => {
            // Use a generic message for both username and email conflicts to prevent
            // enumeration of existing accounts via the public registration endpoint.
            return ApiResponse::error_typed(
                "CONFLICT",
                "An account with the provided details already exists",
            );
        }
        Err(e) => {
            tracing::error!("Storage error checking username: {}", e);
            return ApiResponse::internal_error_typed();
        }
        Ok(None) => {}
    }

    // Check if email already exists
    let email_key = format!("user:email:{}", req.email);
    match state.auth_framework.storage().get_kv(&email_key).await {
        Ok(Some(_)) => {
            return ApiResponse::error_typed(
                "CONFLICT",
                "An account with the provided details already exists",
            );
        }
        Err(e) => {
            tracing::error!("Storage error checking email: {}", e);
            return ApiResponse::internal_error_typed();
        }
        Ok(None) => {}
    }

    // Hash the password
    let password_hash = match crate::utils::password::hash_password(&req.password) {
        Ok(hash) => hash,
        Err(e) => {
            tracing::error!("Password hashing failed: {:?}", e);
            return ApiResponse::error_typed("REGISTRATION_FAILED", "Failed to process password");
        }
    };

    // Generate user ID
    let user_id = format!("user_{}", uuid::Uuid::new_v4().as_simple());
    let created_at = chrono::Utc::now().to_rfc3339();

    // Build user record
    let user_data = serde_json::json!({
        "user_id": user_id,
        "username": req.username,
        "email": req.email,
        "password_hash": password_hash,
        "created_at": created_at,
    });
    let user_data_bytes = user_data.to_string().into_bytes();

    // Store main user record
    if let Err(e) = state
        .auth_framework
        .storage()
        .store_kv(&username_key, &user_data_bytes, None)
        .await
    {
        tracing::error!("User registration storage failed: {:?}", e);
        return ApiResponse::error_typed("REGISTRATION_FAILED", "Failed to create user account");
    }

    // Store email → user_id mapping for duplicate checking
    if let Err(e) = state
        .auth_framework
        .storage()
        .store_kv(&email_key, user_id.as_bytes(), None)
        .await
    {
        tracing::error!("Email mapping storage failed: {:?}", e);
        // Best-effort rollback
        let _ = state
            .auth_framework
            .storage()
            .delete_kv(&username_key)
            .await;
        return ApiResponse::error_typed("REGISTRATION_FAILED", "Failed to create user account");
    }

    // Store the canonical user record used by admin operations (set_user_active,
    // update_user_password, delete_user, etc.) which all key on user:{user_id}.
    let canonical_user_data = serde_json::json!({
        "user_id": user_id,
        "username": req.username,
        "email": req.email,
        "password_hash": password_hash,
        "roles": ["user"],
        "active": true,
        "created_at": created_at,
    });
    let canonical_key = format!("user:{}", user_id);
    if let Err(e) = state
        .auth_framework
        .storage()
        .store_kv(
            &canonical_key,
            canonical_user_data.to_string().as_bytes(),
            None,
        )
        .await
    {
        tracing::error!("Canonical user record storage failed: {:?}", e);
        let _ = state
            .auth_framework
            .storage()
            .delete_kv(&username_key)
            .await;
        let _ = state.auth_framework.storage().delete_kv(&email_key).await;
        return ApiResponse::error_typed("REGISTRATION_FAILED", "Failed to create user account");
    }

    // Store username → user_id mapping (used by get_username_by_id reverse-lookup).
    let username_id_key = format!("user:username:{}", req.username);
    if let Err(e) = state
        .auth_framework
        .storage()
        .store_kv(&username_id_key, user_id.as_bytes(), None)
        .await
    {
        tracing::error!("Username-id mapping storage failed: {:?}", e);
        let _ = state
            .auth_framework
            .storage()
            .delete_kv(&username_key)
            .await;
        let _ = state.auth_framework.storage().delete_kv(&email_key).await;
        let _ = state
            .auth_framework
            .storage()
            .delete_kv(&canonical_key)
            .await;
        return ApiResponse::error_typed("REGISTRATION_FAILED", "Failed to create user account");
    }

    // Add user to the global users:index so admin list endpoints include self-registered users.
    let index_key = "users:index";
    let mut ids: Vec<String> = match state.auth_framework.storage().get_kv(index_key).await {
        Ok(Some(bytes)) => serde_json::from_slice(&bytes).unwrap_or_default(),
        _ => vec![],
    };
    ids.push(user_id.clone());
    if let Ok(idx_json) = serde_json::to_vec(&ids) {
        if let Err(e) = state
            .auth_framework
            .storage()
            .store_kv(index_key, &idx_json, None)
            .await
        {
            tracing::warn!("Failed to update user index after registration: {}", e);
        }
    }

    tracing::info!("New user registered: {} ({})", req.username, user_id);

    ApiResponse::success(RegisterResponse {
        user_id,
        username: req.username,
        email: req.email,
    })
}

/// Response returned when an API key is successfully created.
#[derive(Debug, Serialize)]
pub struct CreateApiKeyResponse {
    /// The new API key – treat as a secret and display only once.
    pub api_key: String,
    /// Always `"ApiKey"`.
    pub token_type: String,
}

/// POST /api-keys – create an API key (requires authentication)
pub async fn create_api_key(
    State(state): State<ApiState>,
    headers: HeaderMap,
) -> ApiResponse<CreateApiKeyResponse> {
    // Require a valid Bearer token; reject unauthenticated requests with 401.
    let token = match crate::api::extract_bearer_token(&headers) {
        Some(t) => t,
        None => return ApiResponse::unauthorized_typed(),
    };

    // Validate the token and extract the caller's identity.
    let auth_token = match crate::api::validate_api_token(&state.auth_framework, &token).await {
        Ok(t) => t,
        Err(_) => return ApiResponse::unauthorized_typed(),
    };

    // Create an API key scoped to the authenticated user.
    match state
        .auth_framework
        .create_api_key(&auth_token.user_id, None)
        .await
    {
        Ok(api_key) => ApiResponse::success(CreateApiKeyResponse {
            api_key,
            token_type: "ApiKey".to_string(),
        }),
        Err(e) => {
            tracing::error!(
                "Failed to create API key for user {}: {}",
                auth_token.user_id,
                e
            );
            ApiResponse::internal_error_typed()
        }
    }
}

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

    #[test]
    fn test_login_risk_low_normal_request() {
        let mut headers = HeaderMap::new();
        headers.insert(
            "user-agent",
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64)".parse().unwrap(),
        );
        let (level, warnings) = login_risk_level(&headers);
        assert_eq!(level, "low");
        assert!(warnings.is_empty());
    }

    #[test]
    fn test_login_risk_high_no_user_agent() {
        let headers = HeaderMap::new();
        let (level, warnings) = login_risk_level(&headers);
        assert_eq!(level, "high");
        assert!(!warnings.is_empty());
    }

    #[test]
    fn test_login_risk_tor_browser() {
        let mut headers = HeaderMap::new();
        headers.insert("user-agent", "Mozilla/5.0 (Tor Browser)".parse().unwrap());
        let (level, warnings) = login_risk_level(&headers);
        // Tor alone is 40 points → "high"
        assert!(level == "high" || level == "critical");
        assert!(warnings.iter().any(|w| w.contains("Tor")));
    }

    #[test]
    fn test_login_risk_proxy_hops() {
        let mut headers = HeaderMap::new();
        headers.insert("user-agent", "Mozilla/5.0".parse().unwrap());
        headers.insert("x-forwarded-for", "1.2.3.4, 5.6.7.8".parse().unwrap());
        let (level, warnings) = login_risk_level(&headers);
        assert_eq!(level, "medium");
        assert!(warnings.iter().any(|w| w.contains("proxy")));
    }

    #[test]
    fn test_login_request_deserialization() {
        let json = r#"{"username":"alice","password":"secret"}"#;
        let req: LoginRequest = serde_json::from_str(json).unwrap();
        assert_eq!(req.username, "alice");
        assert_eq!(req.password, "secret");
        assert!(!req.remember_me);
        assert!(req.challenge_id.is_none());
        assert!(req.mfa_code.is_none());
    }

    #[test]
    fn test_login_response_serialization() {
        let resp = LoginResponse {
            access_token: "at".into(),
            refresh_token: "rt".into(),
            token_type: "Bearer".into(),
            expires_in: 3600,
            user: LoginUserInfo {
                id: "uid".into(),
                username: "alice".into(),
                roles: vec!["user".into()],
                permissions: vec![],
            },
            login_risk_level: "low".into(),
            security_warnings: vec![],
        };
        let json = serde_json::to_value(&resp).unwrap();
        assert_eq!(json["token_type"], "Bearer");
        assert_eq!(json["expires_in"], 3600);
        assert_eq!(json["user"]["username"], "alice");
    }

    #[test]
    fn test_register_request_deserialization() {
        let json = r#"{"username":"bob","password":"StrongP@ss1","email":"bob@example.com"}"#;
        let req: RegisterRequest = serde_json::from_str(json).unwrap();
        assert_eq!(req.username, "bob");
        assert_eq!(req.email, "bob@example.com");
    }

    #[test]
    fn test_refresh_request_deserialization() {
        let json = r#"{"refresh_token":"some_token"}"#;
        let req: RefreshRequest = serde_json::from_str(json).unwrap();
        assert_eq!(req.refresh_token, "some_token");
    }
}