auth-framework 0.5.0-rc19

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
//! API Response Types
//!
//! Common response types for the REST API

use axum::{
    Json,
    http::StatusCode,
    response::{IntoResponse, Response},
};
use serde::Serialize;

/// Standard API response wrapper.
///
/// Encapsulates success/error status, optional data payload,
/// optional error details, and an optional human-readable message.
///
/// # Example
/// ```rust
/// use auth_framework::api::responses::ApiResponse;
///
/// let resp = ApiResponse::success("hello");
/// assert!(resp.success);
/// assert_eq!(resp.data, Some("hello"));
/// ```
#[derive(Debug, Serialize)]
pub struct ApiResponse<T> {
    pub success: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<T>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<ApiError>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
}

/// API error details attached to a failed [`ApiResponse`].
///
/// Contains a machine-readable `code`, human-readable `message`,
/// and optional structured `details`.
///
/// # Example
/// ```rust
/// use auth_framework::api::responses::ApiResponse;
///
/// let resp = ApiResponse::<()>::error("BAD_INPUT", "missing field");
/// let err = resp.error.unwrap();
/// assert_eq!(err.code, "BAD_INPUT");
/// ```
#[derive(Debug, Serialize)]
pub struct ApiError {
    pub code: String,
    pub message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub details: Option<serde_json::Value>,
}

/// Pagination metadata for list endpoints.
///
/// # Example
/// ```rust
/// use auth_framework::api::responses::Pagination;
///
/// let page = Pagination { page: 1, limit: 25, total: 100, pages: 4 };
/// assert_eq!(page.pages, 4);
/// ```
#[derive(Debug, Serialize)]
pub struct Pagination {
    pub page: u32,
    pub limit: u32,
    pub total: u64,
    pub pages: u32,
}

/// API result type
pub type ApiResult<T> = Result<ApiResponse<T>, ApiResponse<()>>;

impl<T> ApiResponse<T> {
    /// Create a successful response carrying `data`.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp = ApiResponse::success(42u32);
    /// assert!(resp.success);
    /// assert_eq!(resp.data, Some(42));
    /// ```
    pub fn success(data: T) -> Self {
        Self {
            success: true,
            data: Some(data),
            error: None,
            message: None,
        }
    }

    /// Convert this response to another data type, discarding the payload.
    ///
    /// Useful for propagating error responses where the data type differs.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let err = ApiResponse::<()>::error("FAIL", "oops");
    /// let typed: ApiResponse<String> = err.cast();
    /// assert!(!typed.success);
    /// ```
    pub fn cast<U>(self) -> ApiResponse<U> {
        ApiResponse {
            success: self.success,
            data: None,
            error: self.error,
            message: self.message,
        }
    }

    /// Create a forbidden (403) response for any data type `T`.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp: ApiResponse<String> = ApiResponse::forbidden_typed();
    /// assert!(!resp.success);
    /// ```
    pub fn forbidden_typed() -> ApiResponse<T> {
        ApiResponse::<()>::forbidden().cast()
    }

    /// Create an unauthorized (401) response for any data type `T`.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp: ApiResponse<Vec<u8>> = ApiResponse::unauthorized_typed();
    /// assert!(!resp.success);
    /// ```
    pub fn unauthorized_typed() -> ApiResponse<T> {
        ApiResponse::<()>::unauthorized().cast()
    }

    /// Create an error response for any data type `T`.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp: ApiResponse<i32> = ApiResponse::error_typed("FAIL", "bad input");
    /// assert!(!resp.success);
    /// ```
    pub fn error_typed(code: &str, message: impl Into<String>) -> ApiResponse<T> {
        ApiResponse::<()>::error(code, message).cast()
    }

    /// Create a validation error (400) response for any data type `T`.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp: ApiResponse<()> = ApiResponse::validation_error_typed("bad field");
    /// assert!(!resp.success);
    /// ```
    pub fn validation_error_typed(message: impl Into<String>) -> ApiResponse<T> {
        ApiResponse::<()>::validation_error(message).cast()
    }

    /// Create a not-found (404) response for any data type `T`.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp: ApiResponse<String> = ApiResponse::not_found_typed("user");
    /// assert!(!resp.success);
    /// ```
    pub fn not_found_typed(message: impl Into<String>) -> ApiResponse<T> {
        ApiResponse::<()>::not_found(message).cast()
    }

    /// Create a forbidden (403) response with a custom message for any data type `T`.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp: ApiResponse<()> = ApiResponse::forbidden_with_message_typed("admin only");
    /// assert!(!resp.success);
    /// ```
    pub fn forbidden_with_message_typed(message: impl Into<String>) -> ApiResponse<T> {
        ApiResponse::<()>::forbidden_with_message(message).cast()
    }

    /// Create an error response with a custom code and message for any data type `T`.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp: ApiResponse<()> = ApiResponse::error_with_message_typed("QUOTA", "exceeded");
    /// assert_eq!(resp.error.unwrap().code, "QUOTA");
    /// ```
    pub fn error_with_message_typed(code: &str, message: impl Into<String>) -> ApiResponse<T> {
        ApiResponse::<()>::error_with_message(code, message).cast()
    }

    /// Create a not-found (404) response with a custom message for any data type `T`.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp: ApiResponse<()> = ApiResponse::not_found_with_message_typed("gone");
    /// assert!(!resp.success);
    /// ```
    pub fn not_found_with_message_typed(message: impl Into<String>) -> ApiResponse<T> {
        ApiResponse::<()>::not_found_with_message(message).cast()
    }

    /// Create an internal server error (500) response for any data type `T`.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp: ApiResponse<()> = ApiResponse::internal_error_typed();
    /// assert!(!resp.success);
    /// ```
    pub fn internal_error_typed() -> ApiResponse<T> {
        ApiResponse::<()>::internal_error().cast()
    }

    /// Create a successful response with data and a human-readable message.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp = ApiResponse::success_with_message("done", "operation complete");
    /// assert!(resp.success);
    /// assert_eq!(resp.message, Some("operation complete".into()));
    /// ```
    pub fn success_with_message(data: T, message: impl Into<String>) -> Self {
        Self {
            success: true,
            data: Some(data),
            error: None,
            message: Some(message.into()),
        }
    }

    /// Create a simple success response with no data.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp = ApiResponse::<()>::ok();
    /// assert!(resp.success);
    /// assert!(resp.data.is_none());
    /// ```
    pub fn ok() -> ApiResponse<()> {
        ApiResponse {
            success: true,
            data: None,
            error: None,
            message: None,
        }
    }

    /// Create a success response with a message but no data.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp = ApiResponse::<()>::ok_with_message("saved");
    /// assert!(resp.success);
    /// assert_eq!(resp.message, Some("saved".into()));
    /// ```
    pub fn ok_with_message(message: impl Into<String>) -> ApiResponse<()> {
        ApiResponse {
            success: true,
            data: None,
            error: None,
            message: Some(message.into()),
        }
    }
}

impl ApiResponse<()> {
    /// Create an error response with a code and message.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp = ApiResponse::<()>::error("BAD_REQUEST", "missing param");
    /// assert!(!resp.success);
    /// assert_eq!(resp.error.as_ref().unwrap().code, "BAD_REQUEST");
    /// ```
    pub fn error(code: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            success: false,
            data: None,
            error: Some(ApiError {
                code: code.into(),
                message: message.into(),
                details: None,
            }),
            message: None,
        }
    }

    /// Create an error response with structured details.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let details = serde_json::json!({"fields": ["name"]});
    /// let resp = ApiResponse::<()>::error_with_details("VALIDATION", "invalid", details);
    /// assert!(resp.error.as_ref().unwrap().details.is_some());
    /// ```
    pub fn error_with_details(
        code: impl Into<String>,
        message: impl Into<String>,
        details: serde_json::Value,
    ) -> Self {
        Self {
            success: false,
            data: None,
            error: Some(ApiError {
                code: code.into(),
                message: message.into(),
                details: Some(details),
            }),
            message: None,
        }
    }

    /// Create a validation error (400) response.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp = ApiResponse::<()>::validation_error("email is invalid");
    /// assert_eq!(resp.error.as_ref().unwrap().code, "VALIDATION_ERROR");
    /// ```
    pub fn validation_error(message: impl Into<String>) -> Self {
        Self::error("VALIDATION_ERROR", message)
    }

    /// Create an unauthorized (401) error response.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp = ApiResponse::<()>::unauthorized();
    /// assert_eq!(resp.error.as_ref().unwrap().code, "UNAUTHORIZED");
    /// ```
    pub fn unauthorized() -> Self {
        Self::error("UNAUTHORIZED", "Authentication required")
    }

    /// Create a forbidden (403) error response.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp = ApiResponse::<()>::forbidden();
    /// assert_eq!(resp.error.as_ref().unwrap().code, "FORBIDDEN");
    /// ```
    pub fn forbidden() -> Self {
        Self::error("FORBIDDEN", "Insufficient permissions")
    }

    /// Create a forbidden (403) error with a custom message.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp = ApiResponse::<()>::forbidden_with_message("admin area");
    /// assert_eq!(resp.error.as_ref().unwrap().message, "admin area");
    /// ```
    pub fn forbidden_with_message(message: impl Into<String>) -> Self {
        Self::error("FORBIDDEN", message)
    }

    /// Create a not-found (404) error naming the missing resource.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp = ApiResponse::<()>::not_found("User");
    /// assert!(resp.error.as_ref().unwrap().message.contains("not found"));
    /// ```
    pub fn not_found(resource: impl Into<String>) -> Self {
        Self::error("NOT_FOUND", format!("{} not found", resource.into()))
    }

    /// Create a not-found (404) error with a custom message.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp = ApiResponse::<()>::not_found_with_message("deleted");
    /// assert_eq!(resp.error.as_ref().unwrap().code, "NOT_FOUND");
    /// ```
    pub fn not_found_with_message(message: impl Into<String>) -> Self {
        Self::error("NOT_FOUND", message)
    }

    /// Create an error response with a custom code and message (alias for [`error`](Self::error)).
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp = ApiResponse::<()>::error_with_message("LIMIT", "rate exceeded");
    /// assert_eq!(resp.error.as_ref().unwrap().code, "LIMIT");
    /// ```
    pub fn error_with_message(code: impl Into<String>, message: impl Into<String>) -> Self {
        Self::error(code, message)
    }

    /// Create an internal server error (500) response.
    ///
    /// # Example
    /// ```rust
    /// use auth_framework::api::responses::ApiResponse;
    ///
    /// let resp = ApiResponse::<()>::internal_error();
    /// assert_eq!(resp.error.as_ref().unwrap().code, "SERVER_ERROR");
    /// ```
    pub fn internal_error() -> Self {
        Self::error("SERVER_ERROR", "Internal server error")
    }
}

impl<T> IntoResponse for ApiResponse<T>
where
    T: Serialize,
{
    fn into_response(self) -> Response {
        let status = if self.success {
            StatusCode::OK
        } else {
            match self.error.as_ref().map(|e| e.code.as_str()) {
                Some("UNAUTHORIZED") => StatusCode::UNAUTHORIZED,
                Some("FORBIDDEN") => StatusCode::FORBIDDEN,
                Some("NOT_FOUND") => StatusCode::NOT_FOUND,
                Some("VALIDATION_ERROR") => StatusCode::BAD_REQUEST,
                Some("RATE_LIMITED") => StatusCode::TOO_MANY_REQUESTS,
                // Authentication failures should be 401, not 500
                Some(
                    "AUTHENTICATION_FAILED"
                    | "INVALID_CREDENTIALS"
                    | "AUTH_ERROR"
                    | "MFA_REQUIRED"
                    | "TOKEN_EXPIRED"
                    | "INVALID_TOKEN",
                ) => StatusCode::UNAUTHORIZED,
                // Client-side errors (bad input / missing resource)
                Some("CONFLICT" | "DUPLICATE_USER") => StatusCode::CONFLICT,
                Some("NOT_IMPLEMENTED") => StatusCode::NOT_IMPLEMENTED,
                // RFC 6749 OAuth error codes (lowercase) and internal codes (uppercase)
                Some(
                    "UNSUPPORTED_GRANT_TYPE"
                    | "UNSUPPORTED_RESPONSE_TYPE"
                    | "unsupported_grant_type"
                    | "unsupported_response_type"
                    | "invalid_grant"
                    | "invalid_request"
                    | "invalid_scope",
                ) => StatusCode::BAD_REQUEST,
                _ => StatusCode::INTERNAL_SERVER_ERROR,
            }
        };

        (status, Json(self)).into_response()
    }
}

/// Convert an [`AuthError`](crate::errors::AuthError) into an appropriate API error response.
///
/// Maps error variants to HTTP-semantic error codes:
/// - `Token` → `INVALID_TOKEN`
/// - `Validation` → `VALIDATION_ERROR`
/// - `AuthMethod` → `INVALID_CREDENTIALS`
/// - `UserNotFound` → `NOT_FOUND`
/// - `Permission` → `FORBIDDEN`
/// - `RateLimit` → `RATE_LIMITED`
/// - everything else → `SERVER_ERROR`
impl From<crate::errors::AuthError> for ApiResponse<()> {
    fn from(error: crate::errors::AuthError) -> Self {
        match &error {
            crate::errors::AuthError::Token(_) => Self::error("INVALID_TOKEN", error.to_string()),
            crate::errors::AuthError::Validation { .. } => {
                Self::validation_error(error.to_string())
            }
            crate::errors::AuthError::AuthMethod { .. } => {
                Self::error("INVALID_CREDENTIALS", error.to_string())
            }
            crate::errors::AuthError::UserNotFound => Self::not_found(error.to_string()),
            crate::errors::AuthError::Permission(_) => Self::forbidden(),
            crate::errors::AuthError::RateLimit { .. } => {
                Self::error("RATE_LIMITED", error.to_string())
            }
            _ => Self::internal_error(),
        }
    }
}