dbrest-core 0.8.6

Database-agnostic core for the dbrest REST API
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
//! Axum auth middleware
//!
//! Extracts the `Authorization: Bearer <token>` header, validates the JWT,
//! caches the result, and inserts an [`AuthResult`] into the request
//! extensions for downstream handlers.
//!
//! # Flow
//!
//! 1. Extract token from `Authorization` header.
//! 2. If no token and anonymous role is configured → anonymous access.
//! 3. If no token and no anonymous role → 401 (`DBRST302`).
//! 4. Check the JWT cache for a previous validation result.
//! 5. On cache miss, validate via [`jwt::parse_and_validate`].
//! 6. Store the result in the cache and attach it to the request extensions.
//!
//! # Error Response
//!
//! JWT errors produce a JSON error body with the appropriate DBRST error
//! code and a `WWW-Authenticate` header when the status is 401.

use std::sync::Arc;

use arc_swap::ArcSwap;
use axum::{
    extract::Request,
    middleware::Next,
    response::{IntoResponse, Response},
};
use http::header;

use crate::config::AppConfig;
use crate::error::response::ErrorResponse;

use super::cache::JwtCache;
use super::error::JwtError;
use super::jwt;
use super::types::AuthResult;

/// Shared authentication state passed to the middleware via axum `State`.
///
/// Contains the config and JWT cache. Cloned per-request (cheap — all
/// fields are `Arc` or `Clone`-cheap).
///
/// The `config` field is an `ArcSwap` so that live config reloads
/// (triggered via `NOTIFY dbrst, 'reload config'`) are visible to the
/// auth middleware without restarting the server.
#[derive(Debug, Clone)]
pub struct AuthState {
    pub config: Arc<ArcSwap<AppConfig>>,
    pub cache: JwtCache,
}

impl AuthState {
    /// Create a new `AuthState` wrapping the given config snapshot.
    ///
    /// The config is placed inside a fresh `ArcSwap`. Use
    /// [`with_shared_config`](Self::with_shared_config) to share the
    /// same `ArcSwap` with `AppState` for live-reload support.
    pub fn new(config: Arc<AppConfig>) -> Self {
        let max_entries = config.jwt_cache_max_entries;
        Self {
            config: Arc::new(ArcSwap::new(config)),
            cache: JwtCache::new(max_entries),
        }
    }

    /// Create an `AuthState` that shares an existing `ArcSwap<AppConfig>`.
    ///
    /// When the `ArcSwap` is updated (e.g. during config reload), the
    /// auth middleware automatically sees the new values.
    pub fn with_shared_config(config: Arc<ArcSwap<AppConfig>>) -> Self {
        let max_entries = config.load().jwt_cache_max_entries;
        Self {
            config,
            cache: JwtCache::new(max_entries),
        }
    }

    /// Get a snapshot of the current config.
    pub fn load_config(&self) -> arc_swap::Guard<Arc<AppConfig>> {
        self.config.load()
    }
}

/// Axum middleware function for JWT authentication.
///
/// Attach to a router via `axum::middleware::from_fn_with_state`:
///
/// ```ignore
/// use axum::{Router, middleware};
/// use dbrest::auth::middleware::{auth_middleware, AuthState};
///
/// let state = AuthState::new(config.into());
/// let app = Router::new()
///     .route("/items", get(handler))
///     .layer(middleware::from_fn_with_state(state, auth_middleware));
/// ```
pub async fn auth_middleware(
    axum::extract::State(state): axum::extract::State<AuthState>,
    mut request: Request,
    next: Next,
) -> Response {
    match authenticate(&state, &request).await {
        Ok(auth_result) => {
            request.extensions_mut().insert(auth_result);
            next.run(request).await
        }
        Err(jwt_err) => jwt_error_response(jwt_err),
    }
}

/// Core authentication logic, separated for testability.
pub async fn authenticate(state: &AuthState, request: &Request) -> Result<AuthResult, JwtError> {
    let token = extract_bearer_token(request);
    authenticate_token(state, token).await
}

/// Authenticate with an already-extracted token string.
///
/// This variant avoids borrowing the `Request` across await points, making
/// it usable in contexts where the `Request` body is not `Sync`.
pub async fn authenticate_token(
    state: &AuthState,
    token: Option<&str>,
) -> Result<AuthResult, JwtError> {
    let config = state.load_config();
    match token {
        Some(token) => {
            // Check cache first
            if let Some(cached) = state.cache.get(token).await {
                return Ok((*cached).clone());
            }

            // Validate
            let result = jwt::parse_and_validate(token, &config)?;

            // Cache the result
            state.cache.insert(token, result.clone()).await;

            Ok(result)
        }
        None => {
            // No token — check anonymous role
            if let Some(ref anon_role) = config.db_anon_role {
                Ok(AuthResult::anonymous(anon_role))
            } else {
                Err(JwtError::TokenRequired)
            }
        }
    }
}

/// Extract the Bearer token from the `Authorization` header.
///
/// Returns `None` if no `Authorization` header is present.
/// Returns `Some("")` if the header is `Bearer ` with an empty token,
/// which is then caught by `parse_and_validate` as `EmptyAuthHeader`.
fn extract_bearer_token(request: &Request) -> Option<&str> {
    let header_value = request.headers().get(header::AUTHORIZATION)?;
    let header_str = header_value.to_str().ok()?;

    if let Some(token) = header_str.strip_prefix("Bearer ") {
        Some(token)
    } else if let Some(token) = header_str.strip_prefix("bearer ") {
        Some(token)
    } else {
        // Not a Bearer token — ignore (might be Basic auth etc.)
        None
    }
}

/// Build an HTTP error response from a JWT error.
pub fn jwt_error_response(err: JwtError) -> Response {
    let status = err.status();
    let www_auth = err.www_authenticate();

    let body = ErrorResponse {
        code: err.code(),
        message: err.to_string(),
        details: err.details(),
        hint: None,
    };

    let mut response = (status, axum::Json(body)).into_response();

    if let Some(www_auth_value) = www_auth
        && let Ok(hv) = http::HeaderValue::from_str(&www_auth_value)
    {
        response.headers_mut().insert(header::WWW_AUTHENTICATE, hv);
    }

    response
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use axum::body::Body;
    use jsonwebtoken::{EncodingKey, Header as JwtHeader};

    fn test_state(secret: &str) -> AuthState {
        let mut config = AppConfig::default();
        config.jwt_secret = Some(secret.to_string());
        config.db_anon_role = Some("web_anon".to_string());
        config.jwt_cache_max_entries = 100;
        AuthState::new(Arc::new(config))
    }

    fn test_state_no_anon(secret: &str) -> AuthState {
        let mut config = AppConfig::default();
        config.jwt_secret = Some(secret.to_string());
        config.db_anon_role = None;
        config.jwt_cache_max_entries = 100;
        AuthState::new(Arc::new(config))
    }

    fn encode_token(claims: &serde_json::Value, secret: &str) -> String {
        jsonwebtoken::encode(
            &JwtHeader::default(),
            claims,
            &EncodingKey::from_secret(secret.as_bytes()),
        )
        .unwrap()
    }

    fn make_request(token: Option<&str>) -> Request {
        let mut builder = Request::builder().method("GET").uri("/items");
        if let Some(t) = token {
            builder = builder.header("Authorization", format!("Bearer {t}"));
        }
        builder.body(Body::empty()).unwrap()
    }

    #[tokio::test]
    async fn test_authenticate_valid_token() {
        let secret = "a]gq@2Yr4wLvA#_6!qnMb*X^tbP$I@av";
        let state = test_state(secret);
        let claims = serde_json::json!({
            "role": "test_author",
            "exp": chrono::Utc::now().timestamp() + 3600
        });
        let token = encode_token(&claims, secret);
        let request = make_request(Some(&token));

        let result = authenticate(&state, &request).await.unwrap();
        assert_eq!(result.role.as_str(), "test_author");
        assert!(!result.is_anonymous());
    }

    #[tokio::test]
    async fn test_authenticate_anonymous() {
        let state = test_state("secret");
        let request = make_request(None);

        let result = authenticate(&state, &request).await.unwrap();
        assert_eq!(result.role.as_str(), "web_anon");
        assert!(result.is_anonymous());
    }

    #[tokio::test]
    async fn test_authenticate_no_anon_no_token() {
        let state = test_state_no_anon("secret");
        let request = make_request(None);

        let err = authenticate(&state, &request).await.unwrap_err();
        assert!(matches!(err, JwtError::TokenRequired));
    }

    #[tokio::test]
    async fn test_authenticate_expired_token() {
        let secret = "a]gq@2Yr4wLvA#_6!qnMb*X^tbP$I@av";
        let state = test_state(secret);
        let claims = serde_json::json!({
            "role": "test_author",
            "exp": chrono::Utc::now().timestamp() - 60
        });
        let token = encode_token(&claims, secret);
        let request = make_request(Some(&token));

        let err = authenticate(&state, &request).await.unwrap_err();
        assert!(matches!(err, JwtError::Claims(_)));
    }

    #[tokio::test]
    async fn test_authenticate_wrong_secret() {
        let state = test_state("correct_secret_is_long_enough");
        let claims = serde_json::json!({
            "role": "test_author",
            "exp": chrono::Utc::now().timestamp() + 3600
        });
        let token = encode_token(&claims, "wrong_secret_value_different");
        let request = make_request(Some(&token));

        let err = authenticate(&state, &request).await.unwrap_err();
        assert!(matches!(err, JwtError::Decode(_)));
    }

    #[tokio::test]
    async fn test_authenticate_cache_hit() {
        let secret = "a]gq@2Yr4wLvA#_6!qnMb*X^tbP$I@av";
        let state = test_state(secret);
        let claims = serde_json::json!({
            "role": "cached_role",
            "exp": chrono::Utc::now().timestamp() + 3600
        });
        let token = encode_token(&claims, secret);

        // First request — cache miss
        let request = make_request(Some(&token));
        let result1 = authenticate(&state, &request).await.unwrap();
        assert_eq!(result1.role.as_str(), "cached_role");

        // Second request — cache hit
        let request = make_request(Some(&token));
        let result2 = authenticate(&state, &request).await.unwrap();
        assert_eq!(result2.role.as_str(), "cached_role");

        // Verify cache has the entry
        assert!(state.cache.get(&token).await.is_some());
    }

    #[tokio::test]
    async fn test_authenticate_empty_bearer() {
        let state = test_state("secret");
        let request = Request::builder()
            .method("GET")
            .uri("/items")
            .header("Authorization", "Bearer ")
            .body(Body::empty())
            .unwrap();

        let err = authenticate(&state, &request).await.unwrap_err();
        assert!(matches!(
            err,
            JwtError::Decode(super::super::error::JwtDecodeError::EmptyAuthHeader)
        ));
    }

    #[test]
    fn test_extract_bearer_token() {
        let req = make_request(Some("abc123"));
        assert_eq!(extract_bearer_token(&req), Some("abc123"));

        let req = make_request(None);
        assert!(extract_bearer_token(&req).is_none());

        // Case insensitive "bearer"
        let req = Request::builder()
            .method("GET")
            .uri("/")
            .header("Authorization", "bearer mytoken")
            .body(Body::empty())
            .unwrap();
        assert_eq!(extract_bearer_token(&req), Some("mytoken"));

        // Basic auth — should return None
        let req = Request::builder()
            .method("GET")
            .uri("/")
            .header("Authorization", "Basic dXNlcjpwYXNz")
            .body(Body::empty())
            .unwrap();
        assert!(extract_bearer_token(&req).is_none());
    }

    #[test]
    fn test_jwt_error_response_has_www_authenticate() {
        let err = JwtError::TokenRequired;
        let response = jwt_error_response(err);
        assert_eq!(response.status(), http::StatusCode::UNAUTHORIZED);
        assert!(response.headers().contains_key(header::WWW_AUTHENTICATE));
        assert_eq!(
            response.headers().get(header::WWW_AUTHENTICATE).unwrap(),
            "Bearer"
        );
    }

    #[test]
    fn test_jwt_error_response_decode() {
        let err = JwtError::Decode(super::super::error::JwtDecodeError::BadCrypto);
        let response = jwt_error_response(err);
        assert_eq!(response.status(), http::StatusCode::UNAUTHORIZED);
        let www = response
            .headers()
            .get(header::WWW_AUTHENTICATE)
            .unwrap()
            .to_str()
            .unwrap();
        assert!(www.contains("invalid_token"));
    }

    #[test]
    fn test_jwt_error_response_secret_missing() {
        let err = JwtError::SecretMissing;
        let response = jwt_error_response(err);
        assert_eq!(response.status(), http::StatusCode::INTERNAL_SERVER_ERROR);
        assert!(!response.headers().contains_key(header::WWW_AUTHENTICATE));
    }

    #[test]
    fn test_shared_config_swap_propagates() {
        let config = AppConfig::default();
        let swap = Arc::new(ArcSwap::new(Arc::new(config)));
        let auth = AuthState::with_shared_config(swap.clone());

        // Initial config
        assert_eq!(auth.load_config().server_port, 3000);

        // Swap in new config
        let mut new_config = AppConfig::default();
        new_config.server_port = 9999;
        swap.store(Arc::new(new_config));

        // Auth state sees the update immediately
        assert_eq!(auth.load_config().server_port, 9999);
    }

    #[test]
    fn test_new_constructor_creates_isolated_swap() {
        let config = AppConfig::default();
        let auth = AuthState::new(Arc::new(config));
        assert_eq!(auth.load_config().server_port, 3000);
    }
}