cadmus 0.1.4

A batteries-included web framework for Rust: models, admin site, REST API, migrations and accounts, on axum and SeaORM
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
//! Central error handling: one error type, one classification, and the three ways it is
//! shown (JSON for the API, a page for the admin, plain text elsewhere).
//!
//! Handlers return [`AdminError`] (or [`ApiError`] for JSON). [`middleware`] then makes the
//! response fit the part of the site it came from, and also covers what no handler produced:
//! unknown URLs, wrong methods and panics.

use std::any::Any;
use std::panic::AssertUnwindSafe;
use std::sync::Arc;

use axum::Json;
use axum::extract::{Request, State};
use axum::http::header::{ALLOW, CONTENT_TYPE, RETRY_AFTER, WWW_AUTHENTICATE};
use axum::http::{HeaderValue, StatusCode};
use axum::middleware::Next;
use axum::response::{IntoResponse, Response};
use futures_util::FutureExt;
use minijinja::context;
use sea_orm::SqlErr;
use serde_json::json;

use crate::AdminState;
use crate::auth::Session;

/// Everything that can go wrong while configuring or serving the app.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum AdminError {
    /// The model or record does not exist.
    #[error("not found: {0}")]
    NotFound(String),
    /// A submitted value could not be converted to the column's type.
    #[error("invalid value for `{field}`: {message}")]
    BadInput {
        /// Column name.
        field: String,
        /// Human-readable reason.
        message: String,
    },
    /// The model uses something this version cannot handle (e.g. a composite primary key).
    #[error("unsupported: {0}")]
    Unsupported(String),
    /// Missing or invalid configuration (environment variable, builder call).
    #[error("configuration error: {0}")]
    Config(String),
    /// No valid credentials were presented.
    #[error("authentication required")]
    Unauthorized,
    /// The request was authenticated but refused (e.g. CSRF token mismatch).
    #[error("forbidden: {0}")]
    Forbidden(String),
    /// Database error.
    #[error(transparent)]
    Db(#[from] sea_orm::DbErr),
    /// A migration could not be made or applied.
    #[error("migration error: {0}")]
    Migration(String),
    /// The request conflicts with the current state (e.g. an upload chunk at the wrong offset).
    #[error("conflict: {0}")]
    Conflict(String),
    /// The upload exceeds a configured size limit.
    #[error("too large: {0}")]
    TooLarge(String),
    /// Storage backend error.
    #[error(transparent)]
    Storage(#[from] object_store::Error),
    /// I/O error (binding the listener, reading a password).
    #[error(transparent)]
    Io(#[from] std::io::Error),
    /// Template rendering error.
    #[error(transparent)]
    Template(#[from] minijinja::Error),
    /// Too many login attempts; try again after the given time.
    #[error("too many attempts; try again in {retry_after_secs} seconds")]
    RateLimited {
        /// Seconds until another attempt is allowed.
        retry_after_secs: u64,
    },
    /// An error defined by your application; create it with [`AdminError::custom`].
    #[error("{message}")]
    Custom {
        /// HTTP status to answer with.
        status: StatusCode,
        /// Machine-readable code, the `error.code` of a JSON response.
        code: String,
        /// Message shown to the caller.
        message: String,
    },
}

impl AdminError {
    /// An application-specific error with its own status and code.
    ///
    /// ```
    /// use axum::http::StatusCode;
    /// use cadmus::AdminError;
    ///
    /// let error = AdminError::custom(StatusCode::CONFLICT, "out_of_stock", "no items left");
    /// assert_eq!(error.to_string(), "no items left");
    /// ```
    ///
    /// The message is sent to the caller as written, including for `5xx` statuses.
    #[must_use]
    pub fn custom(status: StatusCode, code: impl Into<String>, message: impl Into<String>) -> Self {
        Self::Custom {
            status,
            code: code.into(),
            message: message.into(),
        }
    }

    /// What the caller is told: status, code and a message without internal details.
    ///
    /// Internal errors (SQL, storage, template paths) are logged here and replaced by a
    /// generic message.
    fn describe(&self) -> (StatusCode, ErrorInfo) {
        let (status, code, message) = match self {
            Self::NotFound(_) => (StatusCode::NOT_FOUND, "not_found", self.to_string()),
            Self::BadInput { message, .. } => (
                StatusCode::UNPROCESSABLE_ENTITY,
                "invalid_field",
                message.clone(),
            ),
            Self::Unsupported(message) => (StatusCode::BAD_REQUEST, "bad_request", message.clone()),
            Self::Unauthorized => (StatusCode::UNAUTHORIZED, "unauthorized", self.to_string()),
            Self::Forbidden(message) => (StatusCode::FORBIDDEN, "forbidden", message.clone()),
            Self::Conflict(message) => (StatusCode::CONFLICT, "conflict", message.clone()),
            Self::TooLarge(message) => {
                (StatusCode::PAYLOAD_TOO_LARGE, "too_large", message.clone())
            }
            Self::RateLimited { retry_after_secs } => {
                return (
                    StatusCode::TOO_MANY_REQUESTS,
                    ErrorInfo {
                        retry_after: Some(*retry_after_secs),
                        ..ErrorInfo::new("rate_limited", &self.to_string())
                    },
                );
            }
            Self::Custom {
                status,
                code,
                message,
            } => {
                return (
                    *status,
                    ErrorInfo {
                        code: code.clone(),
                        message: message.clone(),
                        field: None,
                        retry_after: None,
                        json: false,
                    },
                );
            }
            // Constraint violations are the caller's to fix; say so without echoing SQL.
            Self::Db(db)
                if matches!(
                    db.sql_err(),
                    Some(
                        SqlErr::UniqueConstraintViolation(_)
                            | SqlErr::ForeignKeyConstraintViolation(_)
                    )
                ) =>
            {
                (
                    StatusCode::CONFLICT,
                    "conflict",
                    "the change conflicts with existing data (unique or foreign key constraint)"
                        .to_owned(),
                )
            }
            Self::Config(_)
            | Self::Db(_)
            | Self::Migration(_)
            | Self::Io(_)
            | Self::Storage(_)
            | Self::Template(_) => {
                tracing::error!(error = %self, "request failed");
                return (StatusCode::INTERNAL_SERVER_ERROR, ErrorInfo::internal());
            }
        };
        let field = match self {
            Self::BadInput { field, .. } => Some(field.clone()),
            _ => None,
        };
        (
            status,
            ErrorInfo {
                code: code.to_owned(),
                message,
                field,
                retry_after: None,
                json: false,
            },
        )
    }
}

/// Plain text by default; [`crate::Admin::handle_errors`] upgrades it to JSON under the API path and to a page
/// under the admin path.
impl IntoResponse for AdminError {
    fn into_response(self) -> Response {
        let (status, info) = self.describe();
        info.text(status)
    }
}

/// An [`AdminError`] answered as JSON: `{"error": {"code", "message", "field"}}`.
///
/// Use it as the error type of your own JSON handlers. Anything that converts into
/// [`AdminError`] (database, storage and I/O errors) converts into this with `?`:
///
/// ```ignore
/// async fn stock(State(db): State<DatabaseConnection>) -> ApiResult<Json<Stock>> {
///     let item = item::Entity::find_by_id(1).one(&db).await?
///         .ok_or_else(|| AdminError::NotFound("item".into()))?;
///     if item.left == 0 {
///         return Err(AdminError::custom(StatusCode::CONFLICT, "out_of_stock", "sold out").into());
///     }
///     Ok(Json(Stock { left: item.left }))
/// }
/// ```
#[derive(Debug)]
pub struct ApiError(AdminError);

impl ApiError {
    /// The underlying error.
    #[must_use]
    pub fn into_inner(self) -> AdminError {
        self.0
    }
}

impl<E: Into<AdminError>> From<E> for ApiError {
    fn from(error: E) -> Self {
        Self(error.into())
    }
}

impl IntoResponse for ApiError {
    fn into_response(self) -> Response {
        let (status, info) = self.0.describe();
        info.json(status)
    }
}

/// Result of a JSON handler.
pub type ApiResult<T> = Result<T, ApiError>;

/// What an error response says, kept on the response so [`middleware`] can render it again
/// in another format.
#[derive(Clone)]
struct ErrorInfo {
    code: String,
    message: String,
    field: Option<String>,
    /// Seconds for the `Retry-After` header.
    retry_after: Option<u64>,
    /// Whether the response body is already JSON.
    json: bool,
}

impl ErrorInfo {
    fn new(code: &str, message: &str) -> Self {
        Self {
            code: code.to_owned(),
            message: message.to_owned(),
            field: None,
            retry_after: None,
            json: false,
        }
    }

    /// Adds the headers that go with this error whatever its format.
    fn finish(self, mut response: Response) -> Response {
        if let Some(secs) = self.retry_after {
            response
                .headers_mut()
                .insert(RETRY_AFTER, HeaderValue::from(secs));
        }
        response.extensions_mut().insert(self);
        response
    }

    fn internal() -> Self {
        Self::new("internal", "internal error")
    }

    /// Describes a bodiless `404`/`405`, which is what axum answers when no route matched.
    fn unrouted(response: &Response) -> Option<Self> {
        if response.headers().contains_key(CONTENT_TYPE) {
            return None;
        }
        match response.status() {
            StatusCode::NOT_FOUND => Some(Self::new("not_found", "not found")),
            StatusCode::METHOD_NOT_ALLOWED => {
                Some(Self::new("method_not_allowed", "method not allowed"))
            }
            _ => None,
        }
    }

    fn text(self, status: StatusCode) -> Response {
        let response = (status, self.message.clone()).into_response();
        self.finish(response)
    }

    fn json(mut self, status: StatusCode) -> Response {
        self.json = true;
        let body = Json(json!({
            "error": { "code": self.code, "message": self.message, "field": self.field }
        }));
        let mut response = (status, body).into_response();
        if status == StatusCode::UNAUTHORIZED {
            response
                .headers_mut()
                .insert(WWW_AUTHENTICATE, HeaderValue::from_static("Bearer"));
        }
        self.finish(response)
    }

    /// The error as an admin page; plain text if the page itself cannot be rendered.
    fn page(self, status: StatusCode, state: &AdminState, session: Option<&Session>) -> Response {
        let page = context! {
            status => status.as_u16(),
            title => status.canonical_reason().unwrap_or("Error"),
            message => &self.message,
        };
        match crate::handlers::render(state, "error.html", session, page) {
            Ok(html) => {
                let mut response = (status, html).into_response();
                if let Some(secs) = self.retry_after {
                    response
                        .headers_mut()
                        .insert(RETRY_AFTER, HeaderValue::from(secs));
                }
                response
            }
            Err(error) => {
                tracing::error!(%error, "could not render the error page");
                self.text(status)
            }
        }
    }
}

#[derive(PartialEq)]
enum Area {
    Api,
    Admin,
    Other,
}

fn under(path: &str, base: &str) -> bool {
    path.strip_prefix(base)
        .is_some_and(|rest| rest.is_empty() || rest.starts_with('/'))
}

fn panic_message(panic: &(dyn Any + Send)) -> &str {
    panic
        .downcast_ref::<&str>()
        .copied()
        .or_else(|| panic.downcast_ref::<String>().map(String::as_str))
        .unwrap_or("unknown panic")
}

/// Wraps the whole app: turns panics into `500`s, makes every error response match its area,
/// and adds the protective headers to responses from the admin and the API. Under the API path errors are always JSON, under the admin path they are pages, and
/// anywhere else they are left as the handler made them.
pub(crate) async fn middleware(
    State(state): State<Arc<AdminState>>,
    request: Request,
    next: Next,
) -> Response {
    let path = request.uri().path();
    let ours = under(path, &state.api_base) || under(path, &state.base);
    let mut response = respond(&state, request, next).await;
    if ours {
        crate::server::protect(&mut response);
    }
    response
}

async fn respond(state: &Arc<AdminState>, request: Request, next: Next) -> Response {
    let path = request.uri().path().to_owned();
    let area = if under(&path, &state.api_base) {
        Area::Api
    } else if under(&path, &state.base) {
        Area::Admin
    } else {
        Area::Other
    };
    // Kept so that an error page can show who is logged in.
    let headers = (area == Area::Admin).then(|| request.headers().clone());

    let mut response = match AssertUnwindSafe(next.run(request)).catch_unwind().await {
        Ok(response) => response,
        Err(panic) => {
            tracing::error!(%path, panic = panic_message(&*panic), "handler panicked");
            ErrorInfo::internal().text(StatusCode::INTERNAL_SERVER_ERROR)
        }
    };

    let status = response.status();
    let known = response.extensions_mut().remove::<ErrorInfo>();
    let Some(info) = known.or_else(|| ErrorInfo::unrouted(&response)) else {
        return response;
    };
    let allow = response.headers().get(ALLOW).cloned();
    let mut rendered = match (area, headers) {
        (Area::Api, _) if !info.json => info.json(status),
        (Area::Admin, Some(headers)) => {
            // If the account cannot be read, the page is simply shown as to a visitor.
            let session = Session::from_cookie(&headers, state).await.ok().flatten();
            info.page(status, state, session.as_ref())
        }
        _ => return response,
    };
    if let Some(allow) = allow {
        rendered.headers_mut().insert(ALLOW, allow);
    }
    rendered
}