cedros-login-server 0.0.45

Authentication server for cedros-login with email/password, Google OAuth, and Solana wallet sign-in
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
//! Error types for the authentication server

// H-10: Standardized forbidden error message constants
// Word order: "owners and admins" (higher permission first)

/// User is not a member of the organization
pub const ERR_NOT_A_MEMBER: &str = "Not a member of this organization";

/// Action requires at least admin role
pub const ERR_ADMIN_REQUIRED: &str = "Only owners and admins can perform this action";

/// Action requires owner role
pub const ERR_OWNER_REQUIRED: &str = "Only owners can perform this action";

/// Action requires system administrator
pub const ERR_SYSTEM_ADMIN_REQUIRED: &str = "Only system administrators can access this resource";

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

use crate::repositories::RepositoryError;

/// Application error type
#[derive(Debug, Error)]
pub enum AppError {
    #[error("Invalid credentials")]
    InvalidCredentials,

    #[error("Account locked: {0}")]
    AccountLocked(String),

    #[error("Email already exists")]
    EmailExists,

    #[error("Account linking required")]
    AccountLinkRequired { provider: String },

    #[error("Wallet already exists")]
    WalletExists,

    #[error("Invalid token")]
    InvalidToken,

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

    #[error("Invalid signature")]
    InvalidSignature,

    #[error("Challenge expired")]
    ChallengeExpired,

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

    #[error("Rate limited")]
    RateLimited,

    /// SEC-04: Rate limited with custom message (e.g., MFA lockout with retry time)
    #[error("Too many requests: {0}")]
    TooManyRequests(String),

    #[error("Not found: {0}")]
    NotFound(String),

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

    #[error("Internal server error")]
    Internal(#[from] anyhow::Error),

    #[error("Database error")]
    Database(String),

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

    #[error("Service unavailable: {0}")]
    ServiceUnavailable(String),

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

    #[error("Step-up authentication required")]
    StepUpRequired,

    #[error("Disposable email addresses are not allowed")]
    DisposableEmailBlocked,
}

/// Error code for API responses
#[derive(Debug, Serialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ErrorCode {
    InvalidCredentials,
    AccountLocked,
    EmailExists,
    AccountLinkRequired,
    WalletExists,
    InvalidToken,
    TokenExpired,
    InvalidSignature,
    ChallengeExpired,
    ValidationError,
    RateLimited,
    NotFound,
    Forbidden,
    Unauthorized,
    StepUpRequired,
    DisposableEmailBlocked,
    ServiceUnavailable,
    ServerError,
}

/// Error response body
#[derive(Debug, Serialize)]
pub struct ErrorResponse {
    pub code: ErrorCode,
    pub message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub details: Option<serde_json::Value>,
}

impl IntoResponse for AppError {
    fn into_response(self) -> Response {
        let (status, code, message) = match &self {
            AppError::InvalidCredentials => (
                StatusCode::UNAUTHORIZED,
                ErrorCode::InvalidCredentials,
                self.to_string(),
            ),
            AppError::AccountLocked(msg) => (
                StatusCode::TOO_MANY_REQUESTS,
                ErrorCode::AccountLocked,
                msg.clone(),
            ),
            AppError::EmailExists => (
                StatusCode::CONFLICT,
                ErrorCode::EmailExists,
                self.to_string(),
            ),
            AppError::AccountLinkRequired { .. } => {
                let provider = match &self {
                    AppError::AccountLinkRequired { provider } => provider.clone(),
                    _ => unreachable!(),
                };
                let body = ErrorResponse {
                    code: ErrorCode::AccountLinkRequired,
                    message: self.to_string(),
                    details: Some(serde_json::json!({ "provider": provider })),
                };
                return (StatusCode::CONFLICT, Json(body)).into_response();
            }
            AppError::WalletExists => (
                StatusCode::CONFLICT,
                ErrorCode::WalletExists,
                self.to_string(),
            ),
            AppError::InvalidToken => (
                StatusCode::UNAUTHORIZED,
                ErrorCode::InvalidToken,
                self.to_string(),
            ),
            AppError::TokenExpired => (
                StatusCode::UNAUTHORIZED,
                ErrorCode::TokenExpired,
                self.to_string(),
            ),
            AppError::InvalidSignature => (
                StatusCode::UNAUTHORIZED,
                ErrorCode::InvalidSignature,
                self.to_string(),
            ),
            AppError::ChallengeExpired => (
                StatusCode::BAD_REQUEST,
                ErrorCode::ChallengeExpired,
                self.to_string(),
            ),
            AppError::Validation(msg) => (
                StatusCode::BAD_REQUEST,
                ErrorCode::ValidationError,
                msg.clone(),
            ),
            AppError::RateLimited => (
                StatusCode::TOO_MANY_REQUESTS,
                ErrorCode::RateLimited,
                self.to_string(),
            ),
            AppError::TooManyRequests(msg) => (
                StatusCode::TOO_MANY_REQUESTS,
                ErrorCode::RateLimited,
                msg.clone(),
            ),
            AppError::NotFound(msg) => (StatusCode::NOT_FOUND, ErrorCode::NotFound, msg.clone()),
            AppError::Forbidden(msg) => (StatusCode::FORBIDDEN, ErrorCode::Forbidden, msg.clone()),
            AppError::ServiceUnavailable(msg) => (
                StatusCode::SERVICE_UNAVAILABLE,
                ErrorCode::ServiceUnavailable,
                msg.clone(),
            ),
            AppError::Unauthorized(msg) => (
                StatusCode::UNAUTHORIZED,
                ErrorCode::Unauthorized,
                msg.clone(),
            ),
            AppError::StepUpRequired => (
                StatusCode::FORBIDDEN,
                ErrorCode::StepUpRequired,
                self.to_string(),
            ),
            AppError::DisposableEmailBlocked => (
                StatusCode::BAD_REQUEST,
                ErrorCode::DisposableEmailBlocked,
                self.to_string(),
            ),
            AppError::Internal(err) => {
                // Debug-only detail: avoid exposing sensitive data at higher log levels.
                tracing::debug!(error = %err, "Internal error detail");
                tracing::error!(
                    error_kind = %"internal",
                    error_code = ?ErrorCode::ServerError,
                    "Request failed"
                );
                (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    ErrorCode::ServerError,
                    "Internal server error".to_string(),
                )
            }
            AppError::Database(msg) => {
                tracing::debug!(error = %msg, "Database error detail");
                tracing::error!(
                    error_kind = %"database",
                    error_code = ?ErrorCode::ServerError,
                    "Request failed"
                );
                (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    ErrorCode::ServerError,
                    "Internal server error".to_string(),
                )
            }
            AppError::Config(msg) => {
                tracing::debug!(error = %msg, "Config error detail");
                // Avoid logging internal error details at error level since they may contain
                // secrets/PII. The current span should already include request context (e.g.
                // request id) when present.
                tracing::error!(
                    error_kind = %"config",
                    error_code = ?ErrorCode::ServerError,
                    "Request failed"
                );
                (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    ErrorCode::ServerError,
                    "Internal server error".to_string(),
                )
            }
        };

        let body = ErrorResponse {
            code,
            message,
            details: None,
        };

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

// Allow using anyhow::Error with AppError
impl From<std::io::Error> for AppError {
    fn from(err: std::io::Error) -> Self {
        AppError::Internal(err.into())
    }
}

impl From<RepositoryError> for AppError {
    fn from(err: RepositoryError) -> Self {
        match err {
            RepositoryError::Database(msg) => AppError::Database(msg),
        }
    }
}

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

    #[test]
    fn test_error_code_serialization() {
        let code = ErrorCode::InvalidCredentials;
        let json = serde_json::to_string(&code).unwrap();
        assert_eq!(json, "\"INVALID_CREDENTIALS\"");

        let code = ErrorCode::EmailExists;
        let json = serde_json::to_string(&code).unwrap();
        assert_eq!(json, "\"EMAIL_EXISTS\"");

        let code = ErrorCode::RateLimited;
        let json = serde_json::to_string(&code).unwrap();
        assert_eq!(json, "\"RATE_LIMITED\"");
    }

    #[test]
    fn test_error_response_serialization() {
        let response = ErrorResponse {
            code: ErrorCode::ValidationError,
            message: "Invalid email format".to_string(),
            details: None,
        };

        let json = serde_json::to_string(&response).unwrap();
        assert!(json.contains("\"code\":\"VALIDATION_ERROR\""));
        assert!(json.contains("\"message\":\"Invalid email format\""));
        assert!(!json.contains("details")); // None should be skipped
    }

    #[test]
    fn test_error_response_with_details() {
        let response = ErrorResponse {
            code: ErrorCode::ValidationError,
            message: "Validation failed".to_string(),
            details: Some(serde_json::json!({"field": "email", "reason": "invalid format"})),
        };

        let json = serde_json::to_string(&response).unwrap();
        assert!(json.contains("\"details\""));
        assert!(json.contains("\"field\":\"email\""));
    }

    #[test]
    fn test_app_error_display() {
        assert_eq!(
            AppError::InvalidCredentials.to_string(),
            "Invalid credentials"
        );
        assert_eq!(AppError::EmailExists.to_string(), "Email already exists");
        assert_eq!(AppError::WalletExists.to_string(), "Wallet already exists");
        assert_eq!(AppError::InvalidToken.to_string(), "Invalid token");
        assert_eq!(AppError::TokenExpired.to_string(), "Token expired");
        assert_eq!(AppError::InvalidSignature.to_string(), "Invalid signature");
        assert_eq!(AppError::ChallengeExpired.to_string(), "Challenge expired");
        assert_eq!(AppError::RateLimited.to_string(), "Rate limited");
        assert_eq!(
            AppError::Validation("test error".to_string()).to_string(),
            "Validation error: test error"
        );
    }

    #[test]
    fn test_app_error_from_io_error() {
        let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
        let app_error: AppError = io_error.into();
        assert!(matches!(app_error, AppError::Internal(_)));
    }

    #[test]
    fn test_all_error_codes_serialize() {
        // Ensure all error codes serialize correctly
        let codes = vec![
            ErrorCode::InvalidCredentials,
            ErrorCode::EmailExists,
            ErrorCode::WalletExists,
            ErrorCode::InvalidToken,
            ErrorCode::TokenExpired,
            ErrorCode::InvalidSignature,
            ErrorCode::ChallengeExpired,
            ErrorCode::ValidationError,
            ErrorCode::RateLimited,
            ErrorCode::DisposableEmailBlocked,
            ErrorCode::ServerError,
        ];

        for code in codes {
            let json = serde_json::to_string(&code).unwrap();
            assert!(json.starts_with('"') && json.ends_with('"'));
            // Should be SCREAMING_SNAKE_CASE
            let inner = &json[1..json.len() - 1];
            assert!(inner.chars().all(|c| c.is_uppercase() || c == '_'));
        }
    }

    #[tokio::test]
    async fn test_internal_errors_do_not_leak_details_to_client() {
        let response = AppError::Database("db: password=secret".to_string()).into_response();
        assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
        let body = response.into_body().collect().await.unwrap().to_bytes();
        let body_str = std::str::from_utf8(&body).unwrap();
        assert!(body_str.contains("\"code\":\"SERVER_ERROR\""));
        assert!(body_str.contains("\"message\":\"Internal server error\""));
        assert!(!body_str.contains("password=secret"));

        let response = AppError::Config("JWT_SECRET=supersecret".to_string()).into_response();
        assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
        let body = response.into_body().collect().await.unwrap().to_bytes();
        let body_str = std::str::from_utf8(&body).unwrap();
        assert!(body_str.contains("\"code\":\"SERVER_ERROR\""));
        assert!(body_str.contains("\"message\":\"Internal server error\""));
        assert!(!body_str.contains("supersecret"));
    }
}