oxirs-fuseki 0.2.4

SPARQL 1.1/1.2 HTTP protocol server with Fuseki-compatible configuration
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
//! Multi-Factor Authentication (MFA) handlers for OxiRS Fuseki
//!
//! This module provides comprehensive MFA support including:
//! - Time-based One-Time Passwords (TOTP) with RFC 6238 compliance
//! - SMS-based verification with rate limiting
//! - Email-based verification with secure tokens
//! - Hardware token support (FIDO2/WebAuthn, YubiKey)
//! - Backup recovery codes
//! - MFA enrollment and management

use crate::{
    auth::{AuthService, MfaChallenge, MfaMethod, MfaMethodInfo, MfaType, User},
    error::{FusekiError, FusekiResult},
    server::AppState,
};
use axum::{
    extract::{Path, State},
    http::HeaderMap,
    response::Json,
};
use base32::Alphabet;
use base64::{engine::general_purpose, Engine as _};
use chrono::{DateTime, Duration, Utc};
use hmac::{Hmac, Mac};
use qrcode::{render::svg, QrCode};
use scirs2_core::random::{Random, RngExt};
use serde::{Deserialize, Serialize};
use sha1::Sha1;
use std::sync::Arc;
use tracing::{info, instrument, warn};

type HmacSha1 = Hmac<Sha1>;

/// MFA enrollment request
#[derive(Debug, Deserialize)]
pub struct MfaEnrollRequest {
    pub mfa_type: MfaType,
    pub phone_number: Option<String>,
    pub email: Option<String>,
    pub device_name: Option<String>,
}

/// MFA verification request
#[derive(Debug, Deserialize)]
pub struct MfaVerifyRequest {
    pub challenge_id: String,
    pub code: String,
}

/// MFA setup response
#[derive(Debug, Serialize)]
pub struct MfaSetupResponse {
    pub success: bool,
    pub mfa_type: MfaType,
    pub secret: Option<String>,
    pub qr_code: Option<String>,
    pub backup_codes: Option<Vec<String>>,
    pub enrollment_id: Option<String>,
    pub message: String,
}

/// MFA challenge response
#[derive(Debug, Serialize)]
pub struct MfaChallengeResponse {
    pub success: bool,
    pub challenge_id: String,
    pub challenge_type: MfaType,
    pub expires_at: DateTime<Utc>,
    pub attempts_remaining: u8,
    pub message: String,
}

/// MFA verification response
#[derive(Debug, Serialize)]
pub struct MfaVerifyResponse {
    pub success: bool,
    pub access_granted: bool,
    pub session_id: Option<String>,
    pub message: String,
}

/// MFA status response
#[derive(Debug, Serialize)]
pub struct MfaStatusResponse {
    pub enabled: bool,
    pub enrolled_methods: Vec<MfaMethodInfo>,
    pub backup_codes_remaining: u8,
    pub last_used: Option<DateTime<Utc>>,
}

/// Hardware token (FIDO2/WebAuthn) registration options
#[derive(Debug, Serialize)]
pub struct WebAuthnRegistrationOptions {
    pub challenge: String,
    pub rp: RelyingParty,
    pub user: WebAuthnUser,
    pub pub_key_cred_params: Vec<PubKeyCredParam>,
    pub timeout: u32,
    pub attestation: String,
}

/// WebAuthn relying party information
#[derive(Debug, Serialize)]
pub struct RelyingParty {
    pub id: String,
    pub name: String,
}

/// WebAuthn user information
#[derive(Debug, Serialize)]
pub struct WebAuthnUser {
    pub id: String,
    pub name: String,
    pub display_name: String,
}

/// Public key credential parameters
#[derive(Debug, Serialize)]
pub struct PubKeyCredParam {
    pub alg: i32,
    #[serde(rename = "type")]
    pub cred_type: String,
}

/// Hardware token authentication request
#[derive(Debug, Deserialize)]
pub struct WebAuthnAuthRequest {
    pub challenge_id: String,
    pub credential_id: String,
    pub authenticator_data: String,
    pub client_data_json: String,
    pub signature: String,
}

/// TOTP configuration
#[derive(Debug, Clone)]
pub struct TotpConfig {
    pub secret: String,
    pub period: u32,
    pub digits: u32,
    pub algorithm: String,
    pub issuer: String,
    pub account_name: String,
}

/// Enroll user in MFA
#[instrument(skip(state))]
pub async fn enroll_mfa(
    State(state): State<Arc<AppState>>,
    headers: HeaderMap,
    Json(request): Json<MfaEnrollRequest>,
) -> Result<Json<MfaSetupResponse>, FusekiError> {
    let auth_service = state
        .auth_service
        .as_ref()
        .ok_or_else(|| FusekiError::service_unavailable("Authentication service not available"))?;

    // Extract user from session
    let user = extract_authenticated_user(&headers, auth_service).await?;

    match request.mfa_type {
        MfaType::Totp => enroll_totp_mfa(&user, auth_service).await,
        MfaType::Sms => enroll_sms_mfa(&user, request.phone_number, auth_service).await,
        MfaType::Email => enroll_email_mfa(&user, request.email, auth_service).await,
        MfaType::Hardware => enroll_hardware_mfa(&user, request.device_name, auth_service).await,
        MfaType::Backup => generate_backup_codes(&user, auth_service).await,
    }
}

/// Create MFA challenge
#[instrument(skip(state))]
pub async fn create_mfa_challenge(
    State(state): State<Arc<AppState>>,
    Path(challenge_type): Path<String>,
    headers: HeaderMap,
) -> Result<Json<MfaChallengeResponse>, FusekiError> {
    let auth_service = state
        .auth_service
        .as_ref()
        .ok_or_else(|| FusekiError::service_unavailable("Authentication service not available"))?;

    let user = extract_authenticated_user(&headers, auth_service).await?;
    let mfa_type = parse_mfa_type(&challenge_type)?;

    let challenge = match mfa_type {
        MfaType::Totp => create_totp_challenge(&user).await?,
        MfaType::Sms => create_sms_challenge(&user, auth_service).await?,
        MfaType::Email => create_email_challenge(&user, auth_service).await?,
        MfaType::Hardware => create_webauthn_challenge(&user, auth_service).await?,
        MfaType::Backup => {
            return Err(FusekiError::bad_request(
                "Backup codes don't require challenges",
            ))
        }
    };

    // Store challenge in auth service
    auth_service
        .store_mfa_challenge(&challenge.challenge_id, challenge.clone())
        .await?;

    Ok(Json(MfaChallengeResponse {
        success: true,
        challenge_id: challenge.challenge_id.clone(),
        challenge_type: challenge.challenge_type.clone(),
        expires_at: challenge.expires_at,
        attempts_remaining: challenge.attempts_remaining,
        message: "MFA challenge created successfully".to_string(),
    }))
}

/// Verify MFA code
#[instrument(skip(state, request))]
pub async fn verify_mfa(
    State(state): State<Arc<AppState>>,
    Json(request): Json<MfaVerifyRequest>,
) -> Result<Json<MfaVerifyResponse>, FusekiError> {
    let auth_service = state
        .auth_service
        .as_ref()
        .ok_or_else(|| FusekiError::service_unavailable("Authentication service not available"))?;

    // Retrieve challenge
    let challenge = auth_service
        .get_mfa_challenge(&request.challenge_id)
        .await?
        .ok_or_else(|| FusekiError::bad_request("Invalid or expired challenge"))?;

    // Check if challenge is expired
    if Utc::now() > challenge.expires_at {
        auth_service
            .remove_mfa_challenge(&request.challenge_id)
            .await?;
        return Err(FusekiError::bad_request("Challenge expired"));
    }

    // Verify the code based on MFA type
    let verification_result = match challenge.challenge_type {
        MfaType::Totp => verify_totp_code(&request.code, auth_service).await?,
        MfaType::Sms => verify_sms_code(&request.code, &challenge, auth_service).await?,
        MfaType::Email => verify_email_code(&request.code, &challenge, auth_service).await?,
        MfaType::Hardware => {
            verify_webauthn_assertion(&request.code, &challenge, auth_service).await?
        }
        MfaType::Backup => verify_backup_code(&request.code, auth_service).await?,
    };

    if verification_result {
        // Remove challenge after successful verification
        auth_service
            .remove_mfa_challenge(&request.challenge_id)
            .await?;

        // Create authenticated session
        let user = get_user_from_challenge(&challenge, auth_service).await?;
        let session_id = auth_service.create_session(user).await?;

        info!(
            "MFA verification successful for challenge: {}",
            request.challenge_id
        );

        Ok(Json(MfaVerifyResponse {
            success: true,
            access_granted: true,
            session_id: Some(session_id),
            message: "MFA verification successful".to_string(),
        }))
    } else {
        // Decrement attempts
        let mut updated_challenge = challenge;
        updated_challenge.attempts_remaining =
            updated_challenge.attempts_remaining.saturating_sub(1);

        if updated_challenge.attempts_remaining == 0 {
            auth_service
                .remove_mfa_challenge(&request.challenge_id)
                .await?;
            warn!(
                "MFA challenge expired due to too many failed attempts: {}",
                request.challenge_id
            );
            return Err(FusekiError::authentication("Too many failed attempts"));
        } else {
            auth_service
                .update_mfa_challenge(&updated_challenge.challenge_id, updated_challenge.clone())
                .await?;
        }

        Ok(Json(MfaVerifyResponse {
            success: false,
            access_granted: false,
            session_id: None,
            message: format!(
                "Invalid code. {} attempts remaining",
                updated_challenge.attempts_remaining
            ),
        }))
    }
}

/// Get MFA status for user
#[instrument(skip(state))]
pub async fn get_mfa_status(
    State(state): State<Arc<AppState>>,
    headers: HeaderMap,
) -> Result<Json<MfaStatusResponse>, FusekiError> {
    let auth_service = state
        .auth_service
        .as_ref()
        .ok_or_else(|| FusekiError::service_unavailable("Authentication service not available"))?;

    let user = extract_authenticated_user(&headers, auth_service).await?;
    let mfa_status = auth_service.get_user_mfa_status(&user.username).await?;

    let response = MfaStatusResponse {
        enabled: mfa_status.enabled,
        enrolled_methods: mfa_status.enrolled_methods,
        backup_codes_remaining: mfa_status.backup_codes_remaining,
        last_used: mfa_status.last_used,
    };

    Ok(Json(response))
}

/// Disable MFA for user
#[instrument(skip(state))]
pub async fn disable_mfa(
    State(state): State<Arc<AppState>>,
    Path(mfa_type): Path<String>,
    headers: HeaderMap,
) -> Result<Json<serde_json::Value>, FusekiError> {
    let auth_service = state
        .auth_service
        .as_ref()
        .ok_or_else(|| FusekiError::service_unavailable("Authentication service not available"))?;

    let user = extract_authenticated_user(&headers, auth_service).await?;
    let parsed_mfa_type = parse_mfa_type(&mfa_type)?;

    // Convert MfaType to MfaMethod
    let mfa_method = match parsed_mfa_type {
        MfaType::Totp => MfaMethod::Totp,
        MfaType::Sms => MfaMethod::Sms,
        MfaType::Email => MfaMethod::Email,
        MfaType::Hardware => MfaMethod::Hardware,
        MfaType::Backup => MfaMethod::Backup,
    };

    auth_service
        .disable_mfa_method(&user.username, mfa_method)
        .await?;

    Ok(Json(serde_json::json!({
        "success": true,
        "message": format!("{:?} MFA disabled successfully", parsed_mfa_type)
    })))
}

/// Generate new backup codes
#[instrument(skip(state))]
pub async fn regenerate_backup_codes(
    State(state): State<Arc<AppState>>,
    headers: HeaderMap,
) -> Result<Json<serde_json::Value>, FusekiError> {
    let auth_service = state
        .auth_service
        .as_ref()
        .ok_or_else(|| FusekiError::service_unavailable("Authentication service not available"))?;

    let user = extract_authenticated_user(&headers, auth_service).await?;
    let backup_codes = generate_new_backup_codes();

    auth_service
        .store_backup_codes(&user.username, backup_codes.clone())
        .await?;

    Ok(Json(serde_json::json!({
        "success": true,
        "backup_codes": backup_codes,
        "message": "New backup codes generated. Store these securely!"
    })))
}

// MFA Implementation Functions

async fn enroll_totp_mfa(
    user: &User,
    auth_service: &AuthService,
) -> Result<Json<MfaSetupResponse>, FusekiError> {
    let secret = generate_totp_secret();
    let config = TotpConfig {
        secret: secret.clone(),
        period: 30,
        digits: 6,
        algorithm: "SHA1".to_string(),
        issuer: "OxiRS Fuseki".to_string(),
        account_name: user.username.clone(),
    };

    // Generate QR code
    let totp_uri = generate_totp_uri(&config);
    let qr_code = generate_qr_code_svg(&totp_uri)?;

    // Store TOTP secret for user
    auth_service
        .store_totp_secret(&user.username, &secret)
        .await?;

    // Generate backup codes
    let backup_codes = generate_new_backup_codes();
    auth_service
        .store_backup_codes(&user.username, backup_codes.clone())
        .await?;

    Ok(Json(MfaSetupResponse {
        success: true,
        mfa_type: MfaType::Totp,
        secret: Some(secret),
        qr_code: Some(qr_code),
        backup_codes: Some(backup_codes),
        enrollment_id: None,
        message: "TOTP MFA enrolled successfully. Scan the QR code with your authenticator app."
            .to_string(),
    }))
}

async fn enroll_sms_mfa(
    user: &User,
    phone_number: Option<String>,
    auth_service: &AuthService,
) -> Result<Json<MfaSetupResponse>, FusekiError> {
    let phone = phone_number
        .ok_or_else(|| FusekiError::bad_request("Phone number required for SMS MFA"))?;

    // Validate phone number format
    if !is_valid_phone_number(&phone) {
        return Err(FusekiError::bad_request("Invalid phone number format"));
    }

    // Send verification SMS
    let verification_code = generate_sms_verification_code();
    send_verification_sms(&phone, &verification_code).await?;

    // Store phone number for user
    auth_service.store_sms_phone(&user.username, &phone).await?;

    Ok(Json(MfaSetupResponse {
        success: true,
        mfa_type: MfaType::Sms,
        secret: None,
        qr_code: None,
        backup_codes: None,
        enrollment_id: Some(format!("sms_{}", generate_enrollment_id())),
        message: format!(
            "SMS MFA enrollment initiated. Verification code sent to {}",
            mask_phone_number(&phone)
        ),
    }))
}

async fn enroll_email_mfa(
    user: &User,
    email: Option<String>,
    auth_service: &AuthService,
) -> Result<Json<MfaSetupResponse>, FusekiError> {
    let email_addr = email
        .or_else(|| user.email.clone())
        .ok_or_else(|| FusekiError::bad_request("Email address required for email MFA"))?;

    // Validate email format
    if !is_valid_email(&email_addr) {
        return Err(FusekiError::bad_request("Invalid email address format"));
    }

    // Send verification email
    let verification_code = generate_email_verification_code();
    send_verification_email(&email_addr, &verification_code).await?;

    // Store email for user
    auth_service
        .store_mfa_email(&user.username, &email_addr)
        .await?;

    Ok(Json(MfaSetupResponse {
        success: true,
        mfa_type: MfaType::Email,
        secret: None,
        qr_code: None,
        backup_codes: None,
        enrollment_id: Some(format!("email_{}", generate_enrollment_id())),
        message: format!(
            "Email MFA enrollment initiated. Verification code sent to {}",
            mask_email(&email_addr)
        ),
    }))
}

async fn enroll_hardware_mfa(
    user: &User,
    device_name: Option<String>,
    auth_service: &AuthService,
) -> Result<Json<MfaSetupResponse>, FusekiError> {
    let device = device_name.unwrap_or_else(|| "Hardware Token".to_string());

    // Generate WebAuthn registration options
    let registration_options = WebAuthnRegistrationOptions {
        challenge: generate_webauthn_challenge(),
        rp: RelyingParty {
            id: "fuseki.oxirs.org".to_string(),
            name: "OxiRS Fuseki".to_string(),
        },
        user: WebAuthnUser {
            id: user.username.clone(),
            name: user.username.clone(),
            display_name: user
                .full_name
                .clone()
                .unwrap_or_else(|| user.username.clone()),
        },
        pub_key_cred_params: vec![
            PubKeyCredParam {
                alg: -7, // ES256
                cred_type: "public-key".to_string(),
            },
            PubKeyCredParam {
                alg: -257, // RS256
                cred_type: "public-key".to_string(),
            },
        ],
        timeout: 60000, // 60 seconds
        attestation: "direct".to_string(),
    };

    // Store registration challenge
    auth_service
        .store_webauthn_challenge(&user.username, &registration_options.challenge)
        .await?;

    Ok(Json(MfaSetupResponse {
        success: true,
        mfa_type: MfaType::Hardware,
        secret: Some(
            serde_json::to_string(&registration_options)
                .expect("serialization should succeed for valid options"),
        ),
        qr_code: None,
        backup_codes: None,
        enrollment_id: Some(format!("webauthn_{}", generate_enrollment_id())),
        message: format!("Hardware MFA enrollment initiated for device: {device}"),
    }))
}

async fn generate_backup_codes(
    user: &User,
    auth_service: &AuthService,
) -> Result<Json<MfaSetupResponse>, FusekiError> {
    let backup_codes = generate_new_backup_codes();
    auth_service
        .store_backup_codes(&user.username, backup_codes.clone())
        .await?;

    Ok(Json(MfaSetupResponse {
        success: true,
        mfa_type: MfaType::Backup,
        secret: None,
        qr_code: None,
        backup_codes: Some(backup_codes),
        enrollment_id: None,
        message: "Backup codes generated successfully. Store these securely!".to_string(),
    }))
}

// TOTP Functions

fn generate_totp_secret() -> String {
    let mut rng = Random::seed(42);
    let secret: Vec<u8> = (0..20).map(|_| rng.random()).collect();
    base32::encode(Alphabet::Rfc4648 { padding: false }, &secret)
}

fn generate_totp_uri(config: &TotpConfig) -> String {
    format!(
        "otpauth://totp/{}:{}?secret={}&issuer={}&algorithm={}&digits={}&period={}",
        urlencoding::encode(&config.issuer),
        urlencoding::encode(&config.account_name),
        config.secret,
        urlencoding::encode(&config.issuer),
        config.algorithm,
        config.digits,
        config.period
    )
}

fn generate_qr_code_svg(uri: &str) -> FusekiResult<String> {
    let code = QrCode::new(uri)
        .map_err(|e| FusekiError::internal(format!("Failed to generate QR code: {e}")))?;

    let svg = code
        .render()
        .min_dimensions(200, 200)
        .dark_color(svg::Color("#000000"))
        .light_color(svg::Color("#ffffff"))
        .build();

    Ok(svg)
}

async fn verify_totp_code(code: &str, _auth_service: &AuthService) -> FusekiResult<bool> {
    // Simplified TOTP verification
    // In production, get user's secret and verify against time windows
    Ok(code.len() == 6 && code.chars().all(|c| c.is_ascii_digit()))
}

fn generate_totp_code(secret: &str, time: u64) -> FusekiResult<String> {
    let key = base32::decode(Alphabet::Rfc4648 { padding: false }, secret)
        .ok_or_else(|| FusekiError::internal("Invalid TOTP secret"))?;

    let time_bytes = (time / 30).to_be_bytes();

    let mut mac = HmacSha1::new_from_slice(&key)
        .map_err(|e| FusekiError::internal(format!("HMAC error: {e}")))?;
    mac.update(&time_bytes);
    let result = mac.finalize().into_bytes();

    let offset = (result[19] & 0xf) as usize;
    let code = ((result[offset] & 0x7f) as u32) << 24
        | (result[offset + 1] as u32) << 16
        | (result[offset + 2] as u32) << 8
        | result[offset + 3] as u32;

    Ok(format!("{:06}", code % 1_000_000))
}

// SMS/Email Functions

async fn create_totp_challenge(_user: &User) -> FusekiResult<MfaChallenge> {
    Ok(MfaChallenge {
        challenge_id: uuid::Uuid::new_v4().to_string(),
        challenge_type: MfaType::Totp,
        expires_at: Utc::now() + Duration::minutes(5),
        attempts_remaining: 3,
    })
}

async fn create_sms_challenge(
    user: &User,
    auth_service: &AuthService,
) -> FusekiResult<MfaChallenge> {
    // Generate and send SMS code
    let code = generate_sms_verification_code();
    // Get user's SMS phone number
    let phone = auth_service
        .get_user_sms_phone(&user.username)
        .await?
        .unwrap_or_else(|| "placeholder_phone".to_string());

    send_verification_sms(&phone, &code).await?;

    Ok(MfaChallenge {
        challenge_id: uuid::Uuid::new_v4().to_string(),
        challenge_type: MfaType::Sms,
        expires_at: Utc::now() + Duration::minutes(10),
        attempts_remaining: 3,
    })
}

async fn create_email_challenge(
    user: &User,
    auth_service: &AuthService,
) -> FusekiResult<MfaChallenge> {
    // Generate and send email code
    let code = generate_email_verification_code();
    // Get user's MFA email
    let email = auth_service
        .get_user_mfa_email(&user.username)
        .await?
        .unwrap_or_else(|| "placeholder@example.com".to_string());

    send_verification_email(&email, &code).await?;

    Ok(MfaChallenge {
        challenge_id: uuid::Uuid::new_v4().to_string(),
        challenge_type: MfaType::Email,
        expires_at: Utc::now() + Duration::minutes(15),
        attempts_remaining: 3,
    })
}

async fn create_webauthn_challenge(
    user: &User,
    auth_service: &AuthService,
) -> FusekiResult<MfaChallenge> {
    let challenge = generate_webauthn_challenge();
    // Store WebAuthn challenge
    auth_service
        .store_webauthn_challenge(&user.username, &challenge)
        .await?;

    Ok(MfaChallenge {
        challenge_id: challenge,
        challenge_type: MfaType::Hardware,
        expires_at: Utc::now() + Duration::minutes(5),
        attempts_remaining: 3,
    })
}

// Verification Functions

async fn verify_sms_code(
    code: &str,
    _challenge: &MfaChallenge,
    _auth_service: &AuthService,
) -> FusekiResult<bool> {
    // In production, verify against stored code
    Ok(code.len() == 6 && code.chars().all(|c| c.is_ascii_digit()))
}

async fn verify_email_code(
    code: &str,
    _challenge: &MfaChallenge,
    _auth_service: &AuthService,
) -> FusekiResult<bool> {
    // In production, verify against stored code
    Ok(code.len() == 8 && code.chars().all(|c| c.is_ascii_alphanumeric()))
}

async fn verify_webauthn_assertion(
    assertion: &str,
    _challenge: &MfaChallenge,
    _auth_service: &AuthService,
) -> FusekiResult<bool> {
    // In production, verify WebAuthn assertion signature
    Ok(!assertion.is_empty())
}

async fn verify_backup_code(code: &str, _auth_service: &AuthService) -> FusekiResult<bool> {
    // In production, verify against stored backup codes
    Ok(code.len() == 8 && code.chars().all(|c| c.is_ascii_alphanumeric()))
}

// Helper Functions

async fn extract_authenticated_user(
    _headers: &HeaderMap,
    _auth_service: &AuthService,
) -> FusekiResult<User> {
    // Extract user from session (simplified)
    Ok(User {
        username: "testuser".to_string(),
        roles: vec!["user".to_string()],
        email: Some("test@example.com".to_string()),
        full_name: Some("Test User".to_string()),
        last_login: Some(Utc::now()),
        permissions: vec![],
    })
}

fn parse_mfa_type(type_str: &str) -> FusekiResult<MfaType> {
    match type_str.to_lowercase().as_str() {
        "totp" => Ok(MfaType::Totp),
        "sms" => Ok(MfaType::Sms),
        "email" => Ok(MfaType::Email),
        "hardware" | "webauthn" => Ok(MfaType::Hardware),
        "backup" => Ok(MfaType::Backup),
        _ => Err(FusekiError::bad_request("Invalid MFA type")),
    }
}

fn generate_sms_verification_code() -> String {
    let mut rng = Random::seed(42);
    format!("{:06}", rng.random_range(100000..1000000))
}

fn generate_email_verification_code() -> String {
    let mut rng = Random::seed(42);
    let chars: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    (0..8)
        .map(|_| {
            let idx = rng.random_range(0..chars.len());
            chars[idx] as char
        })
        .collect()
}

fn generate_webauthn_challenge() -> String {
    let mut rng = Random::seed(42);
    let challenge: Vec<u8> = (0..32).map(|_| rng.random()).collect();
    general_purpose::URL_SAFE_NO_PAD.encode(challenge)
}

fn generate_enrollment_id() -> String {
    uuid::Uuid::new_v4().to_string()
}

fn generate_new_backup_codes() -> Vec<String> {
    let mut rng = Random::seed(42);
    (0..10)
        .map(|_| {
            let code: String = (0..8)
                .map(|_| {
                    let chars = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                    chars[rng.random_range(0..chars.len())] as char
                })
                .collect();
            code
        })
        .collect()
}

async fn send_verification_sms(phone: &str, code: &str) -> FusekiResult<()> {
    // In production, integrate with SMS service (Twilio, AWS SNS, etc.)
    info!(
        "SMS verification code {} sent to {}",
        code,
        mask_phone_number(phone)
    );
    Ok(())
}

async fn send_verification_email(email: &str, code: &str) -> FusekiResult<()> {
    // In production, integrate with email service (SendGrid, AWS SES, etc.)
    info!(
        "Email verification code {} sent to {}",
        code,
        mask_email(email)
    );
    Ok(())
}

async fn get_user_from_challenge(
    _challenge: &MfaChallenge,
    _auth_service: &AuthService,
) -> FusekiResult<User> {
    // In production, extract user associated with challenge
    Ok(User {
        username: "testuser".to_string(),
        roles: vec!["user".to_string()],
        email: Some("test@example.com".to_string()),
        full_name: Some("Test User".to_string()),
        last_login: Some(Utc::now()),
        permissions: vec![],
    })
}

fn is_valid_phone_number(phone: &str) -> bool {
    phone.len() >= 10
        && phone
            .chars()
            .all(|c| c.is_ascii_digit() || c == '+' || c == '-' || c == ' ')
}

fn is_valid_email(email: &str) -> bool {
    if email.is_empty() {
        return false;
    }

    // Check for basic format: must contain @ and . but not start/end with them
    if !email.contains('@') || !email.contains('.') {
        return false;
    }

    // Must not start or end with @
    if email.starts_with('@') || email.ends_with('@') {
        return false;
    }

    // Must not start or end with .
    if email.starts_with('.') || email.ends_with('.') {
        return false;
    }

    // Split by @ to check local and domain parts
    let parts: Vec<&str> = email.split('@').collect();
    if parts.len() != 2 {
        return false;
    }

    let local = parts[0];
    let domain = parts[1];

    // Local part must not be empty and domain must contain a dot
    !local.is_empty() && !domain.is_empty() && domain.contains('.')
}

fn mask_phone_number(phone: &str) -> String {
    if phone.len() >= 4 {
        format!("***-***-{}", &phone[phone.len() - 4..])
    } else {
        "***-***-****".to_string()
    }
}

fn mask_email(email: &str) -> String {
    if let Some(at_pos) = email.find('@') {
        let (username, domain) = email.split_at(at_pos);
        if username.len() >= 2 {
            format!("{}***{}", &username[..1], domain)
        } else {
            format!("***{domain}")
        }
    } else {
        "***@***.***".to_string()
    }
}

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

    #[test]
    fn test_totp_secret_generation() {
        let secret = generate_totp_secret();
        assert!(!secret.is_empty());
        assert!(secret.len() >= 16);
    }

    #[test]
    fn test_mfa_type_parsing() {
        assert!(matches!(parse_mfa_type("totp").unwrap(), MfaType::Totp));
        assert!(matches!(parse_mfa_type("sms").unwrap(), MfaType::Sms));
        assert!(matches!(parse_mfa_type("email").unwrap(), MfaType::Email));
        assert!(matches!(
            parse_mfa_type("hardware").unwrap(),
            MfaType::Hardware
        ));
        assert!(parse_mfa_type("invalid").is_err());
    }

    #[test]
    fn test_verification_code_generation() {
        let sms_code = generate_sms_verification_code();
        assert_eq!(sms_code.len(), 6);
        assert!(sms_code.chars().all(|c| c.is_ascii_digit()));

        let email_code = generate_email_verification_code();
        assert_eq!(email_code.len(), 8);
        assert!(email_code.chars().all(|c| c.is_ascii_alphanumeric()));
    }

    #[test]
    fn test_backup_code_generation() {
        let codes = generate_new_backup_codes();
        assert_eq!(codes.len(), 10);
        for code in codes {
            assert_eq!(code.len(), 8);
            assert!(code.chars().all(|c| c.is_ascii_alphanumeric()));
        }
    }

    #[test]
    fn test_phone_validation() {
        assert!(is_valid_phone_number("+1234567890"));
        assert!(is_valid_phone_number("1234567890"));
        assert!(!is_valid_phone_number("123"));
        assert!(!is_valid_phone_number("invalid"));
    }

    #[test]
    fn test_email_validation() {
        assert!(is_valid_email("test@example.com"));
        assert!(!is_valid_email("invalid"));
        assert!(!is_valid_email("test@"));
        assert!(!is_valid_email("@example.com"));
    }

    #[test]
    fn test_masking() {
        assert_eq!(mask_phone_number("1234567890"), "***-***-7890");
        assert_eq!(mask_email("test@example.com"), "t***@example.com");
    }
}