authen 0.1.0

A robust, modular, high-level, and secure authentication crate for Rust.
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
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
//! Web server and HTTP API integration for Authen using Axum.
//!
//! This module provides the Axum-based web server and HTTP API endpoints for authentication,
//! user management, token operations, and OAuth2 integration. It exposes routes for signup,
//! login, health check, token refresh, token validation, OAuth2 authorization, and OAuth2
//! callbacks, connecting them to the core authentication service.
//!
//! All endpoints expect and return JSON. Error responses are also JSON-encoded.
//!
//! # Features
//! - Only compiled and available with the `web` feature enabled.
//! - Designed for use with the `AuthService` abstraction.
//! - Full OAuth2 support for Google, GitHub, Discord, and Microsoft.
//!
//! # API Endpoints Reference
//!
//! ## Authentication Endpoints
//!
//! ### POST `/signup`
//! Create a new user account with username and password.
//!
//! **Request:**
//! ```json
//! {
//!   "username": "john_doe",
//!   "password": "secure_password123"
//! }
//! ```
//!
//! **Success Response (200):**
//! ```json
//! {
//!   "id": "user-uuid-here",
//!   "identifier": "john_doe"
//! }
//! ```
//!
//! **Error Response (400/500):**
//! ```json
//! {
//!   "error": "Username already exists"
//! }
//! ```
//!
//! ### POST `/login`
//! Authenticate user and receive access and refresh tokens.
//!
//! **Request:**
//! ```json
//! {
//!   "username": "john_doe",
//!   "password": "secure_password123"
//! }
//! ```
//!
//! **Success Response (200):**
//! ```json
//! {
//!   "id": "user-uuid-here",
//!   "identifier": "john_doe",
//!   "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
//!   "refresh_token": "refresh-token-string-here"
//! }
//! ```
//!
//! **Error Response (401/400):**
//! ```json
//! {
//!   "error": "Invalid credentials"
//! }
//! ```
//!
//! ## Token Management
//!
//! ### POST `/token/refresh`
//! Refresh an expired access token using a valid refresh token.
//!
//! **Request:**
//! ```json
//! {
//!   "refresh_token": "refresh-token-string-here"
//! }
//! ```
//!
//! **Success Response (200):**
//! ```json
//! {
//!   "access_token": "new-access-token-here",
//!   "refresh_token": "new-or-same-refresh-token"
//! }
//! ```
//!
//! ### POST `/token/validate`
//! Validate an access token and get user claims.
//!
//! **Request:**
//! ```json
//! {
//!   "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
//! }
//! ```
//!
//! **Success Response (200):**
//! ```json
//! {
//!   "valid": true,
//!   "claims": {
//!     "sub": "user-uuid-here",
//!     "exp": 1640995200
//!   }
//! }
//! ```
//!
//! ## OAuth2 Endpoints
//!
//! ### GET `/oauth/{provider}/auth`
//! Generate OAuth2 authorization URL for the specified provider.
//! Supported providers: `google`, `github`, `discord`, `microsoft`
//!
//! **Query Parameters:**
//! - `state` (required): CSRF protection state parameter
//! - `scopes` (optional): Comma-separated additional scopes
//!
//! **Example Request:**
//! ```
//! GET /oauth/google/auth?state=random-csrf-token&scopes=openid,email,profile
//! ```
//!
//! **Success Response (200):**
//! ```json
//! {
//!   "authorization_url": "https://accounts.google.com/o/oauth2/v2/auth?client_id=..."
//! }
//! ```
//!
//! ### GET `/oauth/{provider}/callback`
//! OAuth2 callback endpoint (usually called by the provider after user authorization).
//! This endpoint automatically redirects users to the configured frontend URI with tokens
//! included in the URL fragment for security.
//!
//! **Query Parameters:**
//! - `code` (required): Authorization code from provider
//! - `state` (required): State parameter for CSRF verification
//!
//! **Example Request:**
//! ```
//! GET /oauth/google/callback?code=auth-code-from-provider&state=random-csrf-token
//! ```
//!
//! **Success Response:**
//! HTTP 302 Redirect to `{redirect_frontend_uri}#access_token=...&refresh_token=...&user_id=...&token_type=Bearer&expires_in=3600`
//!
//! **Error Response:**
//! HTTP 302 Redirect to `{redirect_frontend_uri}#error=authentication_failed&error_description=...`
//!
//!
//! ### POST `/oauth/signup`
//! Create a new user account using OAuth2 authorization code.
//!
//! **Request:**
//! ```json
//! {
//!   "provider": "google",
//!   "code": "authorization-code-from-provider",
//!   "state": "random-csrf-token"
//! }
//! ```
//!
//! **Success Response (200):**
//! ```json
//! {
//!   "id": "user-uuid-here",
//!   "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
//!   "refresh_token": "refresh-token-string-here",
//!   "oauth_info": {
//!     "provider": "google",
//!     "email": "user@example.com",
//!     "name": "John Doe"
//!   }
//! }
//! ```
//!
//! ### POST `/oauth/login`
//! Login with an existing OAuth2 account.
//!
//! **Request:**
//! ```json
//! {
//!   "provider": "google",
//!   "code": "authorization-code-from-provider",
//!   "state": "random-csrf-token"
//! }
//! ```
//!
//! **Success Response (200):**
//! ```json
//! {
//!   "id": "user-uuid-here",
//!   "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
//!   "refresh_token": "refresh-token-string-here"
//! }
//! ```
//!
//! ## Health Check
//!
//! ### GET `/health`
//! Simple health check endpoint.
//!
//! **Response (200):**
//! ```
//! OK
//! ```
//!
//! # Client Usage Examples
//!
//! ## Basic Authentication Flow (JavaScript/TypeScript)
//!
//! ```javascript
//! // 1. Sign up a new user
//! const signupResponse = await fetch('http://localhost:3000/signup', {
//!   method: 'POST',
//!   headers: { 'Content-Type': 'application/json' },
//!   body: JSON.stringify({
//!     username: 'john_doe',
//!     password: 'secure_password123'
//!   })
//! });
//! const userData = await signupResponse.json();
//!
//! // 2. Login and get tokens
//! const loginResponse = await fetch('http://localhost:3000/login', {
//!   method: 'POST',
//!   headers: { 'Content-Type': 'application/json' },
//!   body: JSON.stringify({
//!     username: 'john_doe',
//!     password: 'secure_password123'
//!   })
//! });
//! const { access_token, refresh_token } = await loginResponse.json();
//!
//! // 3. Use access token for authenticated requests
//! const protectedResponse = await fetch('http://your-api.com/protected', {
//!   headers: { 'Authorization': `Bearer ${access_token}` }
//! });
//!
//! // 4. Refresh token when needed
//! const refreshResponse = await fetch('http://localhost:3000/token/refresh', {
//!   method: 'POST',
//!   headers: { 'Content-Type': 'application/json' },
//!   body: JSON.stringify({ refresh_token })
//! });
//! const { access_token: newAccessToken } = await refreshResponse.json();
//! ```
//!
//! ## OAuth2 Flow (JavaScript/TypeScript)
//!
//! ```javascript
//! // 1. Generate authorization URL
//! const state = crypto.randomUUID(); // Generate CSRF token
//! const authResponse = await fetch(
//!   `http://localhost:3000/oauth/google/auth?state=${state}&scopes=openid,email,profile`
//! );
//! const { authorization_url } = await authResponse.json();
//!
//! // 2. Redirect user to authorization URL
//! window.location.href = authorization_url;
//!
//! // 3. Handle callback automatically via redirect
//! // The OAuth2 callback endpoint will automatically redirect the user back to your
//! // frontend application with tokens in the URL fragment. Your frontend should
//! // handle this redirect and extract tokens from the URL fragment:
//!
//! // Example frontend redirect handler (e.g., at your redirect_frontend_uri)
//! function handleOAuthCallback() {
//!   const fragment = window.location.hash.substring(1); // Remove the '#'
//!   const params = new URLSearchParams(fragment);
//!
//!   if (params.has('error')) {
//!     const error = params.get('error');
//!     const errorDescription = params.get('error_description');
//!     console.error('OAuth error:', error, errorDescription);
//!     // Handle error
//!   } else {
//!     const accessToken = params.get('access_token');
//!     const refreshToken = params.get('refresh_token');
//!     const userId = params.get('user_id');
//!
//!     // Store tokens and proceed with authentication
//!     localStorage.setItem('accessToken', accessToken);
//!     localStorage.setItem('refreshToken', refreshToken);
//!     localStorage.setItem('userId', userId);
//!
//!     // Redirect to your app's main page or show success
//!     window.location.href = '/dashboard';
//!   }
//! }
//! ```
//!
//! ## cURL Examples
//!
//! ```bash
//! # Sign up
//! curl -X POST http://localhost:3000/signup \
//!   -H "Content-Type: application/json" \
//!   -d '{"username":"john_doe","password":"secure_password123"}'
//!
//! # Login
//! curl -X POST http://localhost:3000/login \
//!   -H "Content-Type: application/json" \
//!   -d '{"username":"john_doe","password":"secure_password123"}'
//!
//! # Refresh token
//! curl -X POST http://localhost:3000/token/refresh \
//!   -H "Content-Type: application/json" \
//!   -d '{"refresh_token":"your-refresh-token-here"}'
//!
//! # Validate token
//! curl -X POST http://localhost:3000/token/validate \
//!   -H "Content-Type: application/json" \
//!   -d '{"token":"your-access-token-here"}'
//!
//! # Get OAuth authorization URL
//! curl "http://localhost:3000/oauth/google/auth?state=csrf-token"
//!
//! # OAuth signup
//! curl -X POST http://localhost:3000/oauth/signup \
//!   -H "Content-Type: application/json" \
//!   -d '{"provider":"google","code":"auth-code","state":"csrf-token"}'
//! ```
//!
//! # Error Handling
//!
//! All error responses follow this format:
//! ```json
//! {
//!   "error": "Descriptive error message"
//! }
//! ```
//!
//! Common HTTP status codes:
//! - `200`: Success
//! - `400`: Bad Request (invalid JSON, missing fields)
//! - `401`: Unauthorized (invalid credentials, expired token)
//! - `409`: Conflict (username already exists)
//! - `500`: Internal Server Error
//!
//! # Server Setup Example
//! ```no_run
//! use authen::auth_service::AuthService;
//! use authen::web_axum::start_server;
//! use std::sync::Arc;
//! # async fn run(auth_service: Arc<AuthService>) {
//!     start_server(auth_service, None).await;
//! # }
//! ```
use crate::auth_service::AuthService;
#[cfg(feature = "axum")]
use axum::{
    Json, Router,
    extract::{Path, Query, State},
    response::{IntoResponse, Response},
};
use serde::Deserialize;
use std::sync::Arc;

/// Simple URL encoding function for OAuth2 callback parameters
fn url_encode(s: &str) -> String {
    s.bytes()
        .map(|b| match b {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                (b as char).to_string()
            }
            _ => format!("%{b:02X}"),
        })
        .collect()
}

#[cfg(feature = "axum")]
/// Starts the Axum web server with all authentication routes.
///
/// Binds to `0.0.0.0:3000` and serves the API endpoints for signup, login, health check,
/// token refresh, token validation, and OAuth2 integration (authorization URLs, callbacks,
/// OAuth login/signup). This function does not return unless the server fails.
///
/// # Arguments
/// * `auth_service` - An `Arc` to the shared `AuthService` instance used for all authentication logic.
///
/// # Panics
/// Panics if the TCP listener cannot bind or the server fails to start.
pub async fn start_server(
    auth_service: Arc<AuthService>,
    address: impl Into<Option<std::net::SocketAddr>>,
) {
    use axum::serve;
    use tokio::net::TcpListener;
    let app = get_authen_axum_router(auth_service.clone());

    let addr = address
        .into()
        .unwrap_or_else(|| std::net::SocketAddr::from(([0, 0, 0, 0], 3000)));
    log::info!("Axum server running at http://{addr}");
    let listener = TcpListener::bind(addr).await.unwrap();
    serve(listener, app).await.unwrap();
}

#[cfg(feature = "axum")]
/// Returns an Axum `Router` with all Authen authentication routes registered.
///
/// This is useful for integrating Authen's API into an existing Axum application or for testing.
/// Includes all authentication endpoints: credentials-based signup/login, token operations,
/// and OAuth2 integration with support for Google, GitHub, Discord, and Microsoft.
///
/// # Arguments
/// * `auth_service` - An `Arc` to the shared `AuthService` instance.
///
/// # Returns
/// An Axum `Router` with all authentication, token, and OAuth2 endpoints.
pub fn get_authen_axum_router(auth_service: Arc<AuthService>) -> Router {
    use axum::routing::{get, post};
    Router::new()
        .route("/signup", post(signup_handler))
        .route("/login", post(login_handler))
        .route("/health", get(health_handler).post(health_handler))
        .route("/token/refresh", post(refresh_token_handler))
        .route("/token/validate", post(validate_token_handler))
        .route("/oauth/{provider}/auth", get(oauth_auth_handler))
        .route("/oauth/{provider}/callback", get(oauth_callback_handler))
        .route("/oauth/signup", post(oauth_signup_handler))
        .route("/oauth/login", post(oauth_login_handler))
        .with_state(auth_service)
}

#[cfg(feature = "axum")]
/// HTTP handler for the `/signup` endpoint.
///
/// Accepts a JSON body with `username` and `password` fields, creates a new user,
/// and returns the user ID and identifier on success. On error, returns a JSON error message.
///
/// # Request JSON
/// ```json
/// { "username": "string", "password": "string" }
/// ```
///
/// # Response JSON
/// - Success: `{ "id": "...", "identifier": "..." }`
/// - Error: `{ "error": "..." }`
async fn signup_handler(
    State(_auth): State<Arc<AuthService>>,
    Json(_body): Json<serde_json::Value>,
) -> Response {
    log::info!("Received /signup request: {_body}");
    #[derive(Deserialize)]
    struct SignupRequest {
        username: String,
        password: String,
    }

    let req: Result<SignupRequest, _> = serde_json::from_value(_body);
    match req {
        Ok(signup) => {
            log::info!(
                "Attempting signup for user: {username}",
                username = signup.username
            );
            // Use the new unified signup method with credentials
            match _auth
                .signup(crate::auth_service::SignupMethod::Credentials {
                    identifier: signup.username,
                    password: signup.password,
                })
                .await
            {
                Ok((user, _tokens)) => {
                    log::info!("Signup successful for user_id: {id}", id = user.id);
                    serde_json::json!({
                        "id": user.id,
                        "identifier": user.credentials.as_ref().map(|c| &c.identifier).unwrap_or(&"".to_string())
                    })
                    .to_string().into_response()
                }
                Err(e) => {
                    log::error!("Signup failed: {e}");
                    (
                        axum::http::StatusCode::BAD_REQUEST,
                        serde_json::json!({
                            "error": e.to_string()
                        })
                        .to_string(),
                    )
                        .into_response()
                }
            }
        }
        Err(e) => {
            log::error!("Invalid signup request body: {e}");
            (
                axum::http::StatusCode::BAD_REQUEST,
                serde_json::json!({
                    "error": format!("Invalid request body: {e}")
                })
                .to_string(),
            )
                .into_response()
        }
    }
}

#[cfg(feature = "axum")]
/// HTTP handler for the `/login` endpoint.
///
/// Accepts a JSON body with `username` and `password` fields, authenticates the user,
/// and returns user info and tokens on success. On error, returns a JSON error message.
///
/// # Request JSON
/// ```json
/// { "username": "string", "password": "string" }
/// ```
///
/// # Response JSON
/// - Success: `{ "id": "...", "identifier": "...", "access_token": "...", "refresh_token": "..." }`
/// - Error: `{ "error": "..." }`
async fn login_handler(
    State(_auth): State<Arc<AuthService>>,
    Json(_body): Json<serde_json::Value>,
) -> Response {
    log::info!("Received /login request: {_body}");
    #[derive(Deserialize)]
    struct LoginRequest {
        username: String,
        password: String,
    }

    let req: Result<LoginRequest, _> = serde_json::from_value(_body);
    match req {
        Ok(login) => {
            log::info!(
                "Attempting login for user: {username}",
                username = login.username
            );
            match _auth
                .login(crate::auth_service::LoginMethod::Credentials {
                    identifier: login.username,
                    password: login.password,
                })
                .await
            {
                Ok((user, tokens)) => {
                    log::info!("Login successful for user_id: {id}", id = user.id);
                    serde_json::json!({
                        "id": user.id,
                        "identifier": user.credentials.as_ref().map(|c| &c.identifier).unwrap_or(&"".to_string()),
                        "access_token": tokens.access_token,
                        "refresh_token": tokens.refresh_token
                    })
                    .to_string().into_response()
                }
                Err(e) => {
                    log::error!("Login failed: {e}");
                    (
                        axum::http::StatusCode::BAD_REQUEST,
                        serde_json::json!({
                            "error": e.to_string()
                        })
                        .to_string(),
                    )
                        .into_response()
                }
            }
        }
        Err(e) => {
            log::error!("Invalid login request body: {e}");
            (
                axum::http::StatusCode::BAD_REQUEST,
                serde_json::json!({
                    "error": format!("Invalid request body: {e}")
                })
                .to_string(),
            )
                .into_response()
        }
    }
}

#[cfg(feature = "axum")]
/// HTTP handler for the `/health` endpoint.
///
/// Returns a simple "OK" string for health checks.
async fn health_handler() -> String {
    log::info!("Received /health request");
    "OK".to_string()
}

#[cfg(feature = "axum")]
/// HTTP handler for the `/token/refresh` endpoint.
///
/// Accepts a JSON body with a `refresh_token` field, and returns new access and refresh tokens
/// if the refresh token is valid. On error, returns a JSON error message.
///
/// # Request JSON
/// ```json
/// { "refresh_token": "string" }
/// ```
///
/// # Response JSON
/// - Success: `{ "access_token": "...", "refresh_token": "..." }`
/// - Error: `{ "error": "..." }`
async fn refresh_token_handler(
    State(_auth): State<Arc<AuthService>>,
    Json(_body): Json<serde_json::Value>,
) -> Response {
    log::info!("Received /token/refresh request: {_body}");
    #[derive(Deserialize)]
    struct RefreshRequest {
        refresh_token: String,
    }

    let req: Result<RefreshRequest, _> = serde_json::from_value(_body);
    match req {
        Ok(refresh) => {
            log::info!("Attempting token refresh");
            match _auth.refresh_access_token(&refresh.refresh_token).await {
                Ok(tokens) => {
                    log::info!("Token refresh successful");
                    serde_json::json!({
                        "access_token": tokens.access_token,
                        "refresh_token": tokens.refresh_token
                    })
                    .to_string()
                    .into_response()
                }
                Err(e) => {
                    log::error!("Token refresh failed: {e}");
                    (
                        axum::http::StatusCode::BAD_REQUEST,
                        serde_json::json!({
                            "error": e.to_string()
                        })
                        .to_string(),
                    )
                        .into_response()
                }
            }
        }
        Err(e) => {
            log::error!("Invalid refresh token request body: {e}");
            (
                axum::http::StatusCode::BAD_REQUEST,
                serde_json::json!({
                    "error": format!("Invalid request body: {}", e)
                })
                .to_string(),
            )
                .into_response()
        }
    }
}

#[cfg(feature = "axum")]
/// HTTP handler for the `/token/validate` endpoint.
///
/// Accepts a JSON body with a `token` field, validates the access token, and returns
/// claims information if valid. On error, returns a JSON error message.
///
/// # Request JSON
/// ```json
/// { "token": "string" }
/// ```
///
/// # Response JSON
/// - Success: `{ "valid": true, "subject": "...", "expiration": ... }`
/// - Error: `{ "valid": false, "error": "..." }`
async fn validate_token_handler(
    State(_auth): State<Arc<AuthService>>,
    Json(_body): Json<serde_json::Value>,
) -> Response {
    log::info!("Received /token/validate request: {_body}");
    #[derive(Deserialize)]
    struct ValidateRequest {
        token: String,
    }

    let req: Result<ValidateRequest, _> = serde_json::from_value(_body);
    match req {
        Ok(validate) => {
            log::info!("Validating access token");
            match _auth.validate_access_token(&validate.token).await {
                Ok(claims) => {
                    log::info!(
                        "Token valid for subject: {subject}",
                        subject = claims.get_subject()
                    );
                    serde_json::json!({
                        "valid": true,
                        "subject": claims.get_subject(),
                        "expiration": claims.get_expiration()
                    })
                    .to_string()
                    .into_response()
                }
                Err(e) => {
                    log::error!("Token validation failed: {e}");
                    (
                        axum::http::StatusCode::BAD_REQUEST,
                        serde_json::json!({
                            "valid": false,
                            "error": e.to_string()
                        })
                        .to_string(),
                    )
                        .into_response()
                }
            }
        }
        Err(e) => {
            log::error!("Invalid validate token request body: {e}");
            (
                axum::http::StatusCode::BAD_REQUEST,
                serde_json::json!({
                    "error": format!("Invalid request body: {}", e)
                })
                .to_string(),
            )
                .into_response()
        }
    }
}

#[cfg(feature = "axum")]
/// HTTP handler for the `/oauth/{provider}/auth` endpoint.
///
/// Generates an OAuth2 authorization URL for the specified provider.
/// Accepts query parameters for state and optional scopes.
///
/// # Query Parameters
/// - `state`: Required state parameter for CSRF protection
/// - `scopes`: Optional comma-separated list of additional scopes
///
/// # Response JSON
/// - Success: `{ "auth_url": "..." }`
/// - Error: `{ "error": "..." }`
async fn oauth_auth_handler(
    State(_auth): State<Arc<AuthService>>,
    Path(provider_str): Path<String>,
    Query(params): Query<std::collections::HashMap<String, String>>,
) -> Response {
    log::info!("Received /oauth/{provider_str}/auth request with params: {params:?}");
    // Parse the provider from the path parameter
    let provider = match provider_str.to_lowercase().as_str() {
        "google" => crate::core::oauth::store::OAuth2Provider::Google,
        "github" => crate::core::oauth::store::OAuth2Provider::GitHub,
        "discord" => crate::core::oauth::store::OAuth2Provider::Discord,
        "microsoft" => crate::core::oauth::store::OAuth2Provider::Microsoft,
        _ => {
            return (
                axum::http::StatusCode::BAD_REQUEST,
                serde_json::json!({
                    "error": format!("Unsupported OAuth2 provider: {}", provider_str)
                })
                .to_string(),
            )
                .into_response();
        }
    };

    // Get the state parameter (required)
    let state = match params.get("state") {
        Some(state) => state,
        None => {
            return (
                axum::http::StatusCode::BAD_REQUEST,
                serde_json::json!({
                    "error": "Missing required 'state' parameter"
                })
                .to_string(),
            )
                .into_response();
        }
    };

    // Parse optional scopes parameter
    let scopes = params
        .get("scopes")
        .map(|s| s.split(',').map(|scope| scope.trim().to_string()).collect());

    // Generate the OAuth2 authorization URL
    match _auth
        .generate_oauth2_auth_url(provider, state, scopes)
        .await
    {
        Ok(auth_url) => {
            log::info!("Generated OAuth2 auth URL for provider: {provider_str}");
            serde_json::json!({
                "auth_url": auth_url
            })
            .to_string()
            .into_response()
        }
        Err(e) => {
            log::error!("OAuth2 auth URL generation failed: {e}");
            (
                axum::http::StatusCode::BAD_REQUEST,
                serde_json::json!({
                    "error": e.to_string()
                })
                .to_string(),
            )
                .into_response()
        }
    }
}

#[cfg(feature = "axum")]
/// HTTP handler for the `/oauth/{provider}/callback` endpoint.
///
/// Handles OAuth2 callback from providers. This endpoint would typically be called
/// by the OAuth2 provider after user authorization. Redirects user to the frontend
/// application with tokens included in the URL fragment.
///
/// # Query Parameters
/// - `code`: The authorization code from the provider
/// - `state`: The state parameter for CSRF verification
///
/// # Response
/// - Success: HTTP 302 redirect to frontend URI with tokens in URL fragment
/// - Error: JSON error response
async fn oauth_callback_handler(
    State(_auth): State<Arc<AuthService>>,
    Path(provider_str): Path<String>,
    Query(params): Query<std::collections::HashMap<String, String>>,
) -> Response {
    log::info!("Received /oauth/{provider_str}/callback request with params: {params:?}");
    // Parse the provider from the path parameter
    let provider = match provider_str.to_lowercase().as_str() {
        "google" => crate::core::oauth::store::OAuth2Provider::Google,
        "github" => crate::core::oauth::store::OAuth2Provider::GitHub,
        "discord" => crate::core::oauth::store::OAuth2Provider::Discord,
        "microsoft" => crate::core::oauth::store::OAuth2Provider::Microsoft,
        _ => {
            return (
                axum::http::StatusCode::BAD_REQUEST,
                serde_json::json!({
                    "error": format!("Unsupported OAuth2 provider: {}", provider_str)
                })
                .to_string(),
            )
                .into_response();
        }
    };

    // Get required parameters
    let code = match params.get("code") {
        Some(code) => code,
        None => {
            return (
                axum::http::StatusCode::BAD_REQUEST,
                serde_json::json!({
                    "error": "Missing required 'code' parameter"
                })
                .to_string(),
            )
                .into_response();
        }
    };

    let state = match params.get("state") {
        Some(state) => state,
        None => {
            return (
                axum::http::StatusCode::BAD_REQUEST,
                serde_json::json!({
                    "error": "Missing required 'state' parameter"
                })
                .to_string(),
            )
                .into_response();
        }
    };

    // Get the frontend redirect URI for this provider
    let frontend_uri = match _auth.get_oauth2_redirect_frontend_uri(provider).await {
        Ok(uri) => uri,
        Err(e) => {
            log::error!("Failed to get frontend redirect URI: {e}");
            return (
                axum::http::StatusCode::INTERNAL_SERVER_ERROR,
                serde_json::json!({
                    "error": format!("Configuration error: {e}")
                })
                .to_string(),
            )
                .into_response();
        }
    };

    // Use the login method with OAuth2
    match _auth
        .login(crate::auth_service::LoginMethod::OAuth2 {
            provider,
            code: code.clone(),
            state: state.clone(),
        })
        .await
    {
        Ok((user, tokens)) => {
            log::info!(
                "OAuth2 callback login successful for user_id: {id}",
                id = user.id
            );

            // Build the redirect URL with tokens in the fragment
            let redirect_url = format!(
                "{}#access_token={}&refresh_token={}&user_id={}&token_type=Bearer&expires_in=3600",
                frontend_uri,
                url_encode(&tokens.access_token),
                url_encode(&tokens.refresh_token),
                url_encode(&user.id)
            );

            log::info!("Redirecting user to frontend: {redirect_url}");

            // Return HTTP 302 redirect
            axum::response::Redirect::permanent(&redirect_url).into_response()
        }
        Err(e) => {
            log::error!("OAuth2 callback login failed: {e}");

            // Redirect to frontend with error
            let error_redirect_url = format!(
                "{}#error={}&error_description={}",
                frontend_uri,
                url_encode("authentication_failed"),
                url_encode(&e.to_string())
            );

            log::info!("Redirecting user to frontend with error: {error_redirect_url}");
            axum::response::Redirect::permanent(&error_redirect_url).into_response()
        }
    }
}

#[cfg(feature = "axum")]
/// HTTP handler for the `/oauth/signup` endpoint.
///
/// Handles OAuth2 signup/registration using an authorization code.
/// Creates a new user account or links to existing account.
///
/// # Request JSON
/// ```json
/// { "provider": "google|github|discord|microsoft", "code": "string", "state": "string" }
/// ```
///
/// # Response JSON
/// - Success: `{ "id": "...", "access_token": "...", "refresh_token": "..." }`
/// - Error: `{ "error": "..." }`
async fn oauth_signup_handler(
    State(_auth): State<Arc<AuthService>>,
    Json(_body): Json<serde_json::Value>,
) -> Response {
    log::info!("Received /oauth/signup request: {_body}");
    #[derive(Deserialize)]
    struct OAuth2SignupRequest {
        provider: String,
        code: String,
        state: String,
    }

    let req: Result<OAuth2SignupRequest, _> = serde_json::from_value(_body);
    match req {
        Ok(signup) => {
            log::info!(
                "Attempting OAuth2 signup for provider: {provider}",
                provider = signup.provider
            );
            // Parse the provider
            let provider = match signup.provider.to_lowercase().as_str() {
                "google" => crate::core::oauth::store::OAuth2Provider::Google,
                "github" => crate::core::oauth::store::OAuth2Provider::GitHub,
                "discord" => crate::core::oauth::store::OAuth2Provider::Discord,
                "microsoft" => crate::core::oauth::store::OAuth2Provider::Microsoft,
                _ => {
                    log::error!(
                        "Unsupported OAuth2 provider: {provider}",
                        provider = signup.provider
                    );
                    return (
                        axum::http::StatusCode::BAD_REQUEST,
                        serde_json::json!({
                            "error": format!("Unsupported OAuth2 provider: {}", signup.provider)
                        })
                        .to_string(),
                    )
                        .into_response();
                }
            };

            // Use the unified signup method with OAuth2
            match _auth
                .signup(crate::auth_service::SignupMethod::OAuth2 {
                    provider,
                    code: signup.code,
                    state: signup.state,
                })
                .await
            {
                Ok((user, tokens)) => {
                    log::info!("OAuth2 signup successful for user_id: {id}", id = user.id);
                    serde_json::json!({
                        "id": user.id,
                        "access_token": tokens.access_token,
                        "refresh_token": tokens.refresh_token
                    })
                    .to_string()
                    .into_response()
                }
                Err(e) => {
                    log::error!("OAuth2 signup failed: {e}");
                    (
                        axum::http::StatusCode::BAD_REQUEST,
                        serde_json::json!({
                            "error": e.to_string()
                        })
                        .to_string(),
                    )
                        .into_response()
                }
            }
        }
        Err(e) => {
            log::error!("Invalid OAuth2 signup request body: {e}");
            (
                axum::http::StatusCode::BAD_REQUEST,
                serde_json::json!({
                    "error": format!("Invalid request body: {}", e)
                })
                .to_string(),
            )
                .into_response()
        }
    }
}

#[cfg(feature = "axum")]
/// HTTP handler for the `/oauth/login` endpoint.
///
/// Handles OAuth2 login using an authorization code.
/// Authenticates existing user or creates account if needed.
///
/// # Request JSON
/// ```json
/// { "provider": "google|github|discord|microsoft", "code": "string", "state": "string" }
/// ```
///
/// # Response JSON
/// - Success: `{ "id": "...", "access_token": "...", "refresh_token": "..." }`
/// - Error: `{ "error": "..." }`
async fn oauth_login_handler(
    State(_auth): State<Arc<AuthService>>,
    Json(_body): Json<serde_json::Value>,
) -> Response {
    log::info!("Received /oauth/login request: {_body}");
    #[derive(Deserialize)]
    struct OAuth2LoginRequest {
        provider: String,
        code: String,
        state: String,
    }

    let req: Result<OAuth2LoginRequest, _> = serde_json::from_value(_body);
    match req {
        Ok(login) => {
            log::info!(
                "Attempting OAuth2 login for provider: {provider}",
                provider = login.provider
            );
            // Parse the provider
            let provider = match login.provider.to_lowercase().as_str() {
                "google" => crate::core::oauth::store::OAuth2Provider::Google,
                "github" => crate::core::oauth::store::OAuth2Provider::GitHub,
                "discord" => crate::core::oauth::store::OAuth2Provider::Discord,
                "microsoft" => crate::core::oauth::store::OAuth2Provider::Microsoft,
                _ => {
                    log::error!(
                        "Unsupported OAuth2 provider: {provider}",
                        provider = login.provider
                    );
                    return (
                        axum::http::StatusCode::BAD_REQUEST,
                        serde_json::json!({
                            "error": format!("Unsupported OAuth2 provider: {}", login.provider)
                        })
                        .to_string(),
                    )
                        .into_response();
                }
            };

            // Use the unified login method with OAuth2
            match _auth
                .login(crate::auth_service::LoginMethod::OAuth2 {
                    provider,
                    code: login.code,
                    state: login.state,
                })
                .await
            {
                Ok((user, tokens)) => {
                    log::info!("OAuth2 login successful for user_id: {id}", id = user.id);
                    serde_json::json!({
                        "id": user.id,
                        "access_token": tokens.access_token,
                        "refresh_token": tokens.refresh_token
                    })
                    .to_string()
                    .into_response()
                }
                Err(e) => {
                    log::error!("OAuth2 login failed: {e}");
                    (
                        axum::http::StatusCode::BAD_REQUEST,
                        serde_json::json!({
                            "error": e.to_string()
                        })
                        .to_string(),
                    )
                        .into_response()
                }
            }
        }
        Err(e) => {
            log::error!("Invalid OAuth2 login request body: {e}");
            (
                axum::http::StatusCode::BAD_REQUEST,
                serde_json::json!({
                    "error": format!("Invalid request body: {}", e)
                })
                .to_string(),
            )
                .into_response()
        }
    }
}