diff --git a/src/constant.rs b/src/constant.rs new file mode 100644 index 0000000..1f6a3e6 --- /dev/null +++ b/src/constant.rs @@ -0,0 +1,28 @@ +//! Validation limits, in one place. + +pub const MIN_USERNAME_LEN: usize = 3; +pub const MAX_USERNAME_LEN: usize = 32; + +pub const MIN_PASSWORD_LEN: usize = 6; +pub const MAX_PASSWORD_LEN: usize = 128; + +pub const MAX_NOTE_TITLE_LEN: usize = 200; +pub const MAX_NOTE_CONTENT_LEN: usize = 10_000; + +pub const MAX_EVENT_TITLE_LEN: usize = 200; +pub const MAX_EVENT_DESCRIPTION_LEN: usize = 2_000; + +pub const MAX_EXAM_TITLE_LEN: usize = 200; +pub const MAX_EXAM_DESCRIPTION_LEN: usize = 2_000; + +/// The only accepted attendance states. +pub const ATTENDANCE_STATUSES: [&str; 4] = ["present", "absent", "late", "excused"]; + +/// The only accepted exam kinds — an exam is a homework or a quiz, nothing else. +pub const EXAM_KINDS: [&str; 2] = ["homework", "quiz"]; + +/// Inclusive bounds for an exam mark. +pub const MIN_MARK: i64 = 0; +pub const MAX_MARK: i64 = 100; + +pub const SESSION_DURATION_DAYS: i64 = 7; diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..49fc18d --- /dev/null +++ b/src/error.rs @@ -0,0 +1,102 @@ +use axum::Json; +use axum::http::{StatusCode, header}; +use axum::response::{IntoResponse, Response}; +use serde::Serialize; +use serde_json::json; +use utoipa::ToSchema; + +/// Uniform error body shape (`{ "error": "..." }`) used for OpenAPI docs. +#[derive(Serialize, ToSchema)] +pub struct ErrorResponse { + /// Human-readable failure reason. + pub error: String, +} + +/// Why a newtype refused to be constructed from raw input. Always maps to `400`. +#[derive(Debug, thiserror::Error)] +pub enum ValidationError { + #[error("{0} must not be empty")] + Empty(&'static str), + #[error("{field} must be at least {min} characters (got {got})")] + TooShort { + field: &'static str, + min: usize, + got: usize, + }, + #[error("{field} must be at most {max} characters (got {got})")] + TooLong { + field: &'static str, + max: usize, + got: usize, + }, + #[error("{0} must be ASCII")] + NotAscii(&'static str), + #[error("{field}: {reason}")] + Invalid { + field: &'static str, + reason: &'static str, + }, +} + +/// The single error type every fallible operation returns. +#[derive(Debug, thiserror::Error)] +pub enum AppError { + #[error(transparent)] + Validation(#[from] ValidationError), + #[error("not found")] + NotFound, + #[error("unauthorized")] + Unauthorized, + #[error("forbidden: {0}")] + Forbidden(&'static str), + #[error("conflict: {0}")] + Conflict(&'static str), + #[error("too many requests")] + TooManyRequests { retry_after_secs: u64 }, + #[error("database error")] + Db(#[from] surrealdb::Error), + #[error("internal error: {0}")] + Internal(String), +} + +impl From for AppError { + fn from(e: argon2::password_hash::Error) -> Self { + AppError::Internal(format!("password hashing error: {e}")) + } +} + +impl IntoResponse for AppError { + fn into_response(self) -> Response { + let (status, message) = match &self { + // The one arm with an extra header, so it builds its response here. + AppError::TooManyRequests { retry_after_secs } => { + return ( + StatusCode::TOO_MANY_REQUESTS, + [(header::RETRY_AFTER, retry_after_secs.to_string())], + Json(json!({ "error": "too many requests" })), + ) + .into_response(); + } + AppError::Validation(v) => (StatusCode::BAD_REQUEST, v.to_string()), + AppError::NotFound => (StatusCode::NOT_FOUND, "not found".to_string()), + AppError::Unauthorized => (StatusCode::UNAUTHORIZED, "unauthorized".to_string()), + AppError::Forbidden(m) => (StatusCode::FORBIDDEN, m.to_string()), + AppError::Conflict(m) => (StatusCode::CONFLICT, m.to_string()), + AppError::Db(e) => { + tracing::error!("database error: {e}"); + ( + StatusCode::INTERNAL_SERVER_ERROR, + "database error".to_string(), + ) + } + AppError::Internal(m) => { + tracing::error!("internal error: {m}"); + ( + StatusCode::INTERNAL_SERVER_ERROR, + "internal server error".to_string(), + ) + } + }; + (status, Json(json!({ "error": message }))).into_response() + } +} diff --git a/src/validate.rs b/src/validate.rs new file mode 100644 index 0000000..fb82985 --- /dev/null +++ b/src/validate.rs @@ -0,0 +1,199 @@ +//! Field validators. Each newtype's `try_new` calls one of these before wrapping +//! the value, so an existing newtype is always valid (parse, don't validate). + +use crate::constant::{ + ATTENDANCE_STATUSES, EXAM_KINDS, MAX_MARK, MAX_PASSWORD_LEN, MAX_USERNAME_LEN, MIN_MARK, + MIN_PASSWORD_LEN, MIN_USERNAME_LEN, +}; +use crate::error::ValidationError; + +pub fn validate_username(value: &str) -> Result<(), ValidationError> { + // Length is measured on the trimmed value so padding can't defeat the + // minimum (e.g. "a " is a one-character name, not a three-character one). + let value = value.trim(); + if value.is_empty() { + return Err(ValidationError::Empty("username")); + } + if !value.is_ascii() { + return Err(ValidationError::NotAscii("username")); + } + let len = value.chars().count(); + if len < MIN_USERNAME_LEN { + return Err(ValidationError::TooShort { + field: "username", + min: MIN_USERNAME_LEN, + got: len, + }); + } + if len > MAX_USERNAME_LEN { + return Err(ValidationError::TooLong { + field: "username", + max: MAX_USERNAME_LEN, + got: len, + }); + } + Ok(()) +} + +pub fn validate_password(value: &str) -> Result<(), ValidationError> { + // A password may contain anything, including whitespace, so it is not + // trimmed — but length is still measured in characters, not UTF-8 bytes. + let len = value.chars().count(); + if len < MIN_PASSWORD_LEN { + return Err(ValidationError::TooShort { + field: "password", + min: MIN_PASSWORD_LEN, + got: len, + }); + } + if len > MAX_PASSWORD_LEN { + return Err(ValidationError::TooLong { + field: "password", + max: MAX_PASSWORD_LEN, + got: len, + }); + } + Ok(()) +} + +/// A required free-text field: non-blank and within `max`. +pub fn validate_required( + field: &'static str, + value: &str, + max: usize, +) -> Result<(), ValidationError> { + if value.trim().is_empty() { + return Err(ValidationError::Empty(field)); + } + if value.chars().count() > max { + return Err(ValidationError::TooLong { + field, + max, + got: value.chars().count(), + }); + } + Ok(()) +} + +/// An optional free-text field: may be empty, but within `max`. +pub fn validate_optional( + field: &'static str, + value: &str, + max: usize, +) -> Result<(), ValidationError> { + if value.chars().count() > max { + return Err(ValidationError::TooLong { + field, + max, + got: value.chars().count(), + }); + } + Ok(()) +} + +pub fn validate_status(value: &str) -> Result<(), ValidationError> { + if ATTENDANCE_STATUSES.contains(&value) { + Ok(()) + } else { + Err(ValidationError::Invalid { + field: "status", + reason: "must be one of: present, absent, late, excused", + }) + } +} + +pub fn validate_exam_kind(value: &str) -> Result<(), ValidationError> { + if EXAM_KINDS.contains(&value) { + Ok(()) + } else { + Err(ValidationError::Invalid { + field: "kind", + reason: "must be one of: homework, quiz", + }) + } +} + +pub fn validate_mark(value: i64) -> Result<(), ValidationError> { + if (MIN_MARK..=MAX_MARK).contains(&value) { + Ok(()) + } else { + Err(ValidationError::Invalid { + field: "mark", + reason: "must be between 0 and 100", + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test] + async fn username_rules() { + assert!(validate_username("ali").is_ok()); + assert!(validate_username("ab").is_err()); // too short + assert!(validate_username(" ").is_err()); // blank + assert!(validate_username(&"x".repeat(33)).is_err()); // too long + assert!(validate_username("naïve").is_err()); // non-ascii + assert!(validate_username("a ").is_err()); // padding can't defeat the minimum + } + + #[tokio::test] + async fn length_is_measured_in_characters_not_bytes() { + // "é" is two UTF-8 bytes; a 4-char password of them is under the 6-char + // minimum and must be rejected as too short, not accepted on byte count. + assert!(validate_password("éééé").is_err()); + // Exactly `max` multi-byte characters is allowed (bytes would overflow). + assert!(validate_required("title", &"é".repeat(5), 5).is_ok()); + assert!(validate_required("title", &"é".repeat(6), 5).is_err()); + assert!(validate_optional("content", &"é".repeat(5), 5).is_ok()); + assert!(validate_optional("content", &"é".repeat(6), 5).is_err()); + } + + #[tokio::test] + async fn password_rules() { + assert!(validate_password("secret1").is_ok()); + assert!(validate_password("12345").is_err()); + assert!(validate_password(&"x".repeat(129)).is_err()); + } + + #[tokio::test] + async fn required_rules() { + assert!(validate_required("title", "hi", 10).is_ok()); + assert!(validate_required("title", " ", 10).is_err()); + assert!(validate_required("title", "toolong", 3).is_err()); + } + + #[tokio::test] + async fn optional_rules() { + assert!(validate_optional("content", "", 10).is_ok()); + assert!(validate_optional("content", "toolong", 3).is_err()); + } + + #[tokio::test] + async fn status_rules() { + for status in ["present", "absent", "late", "excused"] { + assert!(validate_status(status).is_ok()); + } + assert!(validate_status("maybe").is_err()); + assert!(validate_status("").is_err()); + } + + #[tokio::test] + async fn exam_kind_rules() { + for kind in ["homework", "quiz"] { + assert!(validate_exam_kind(kind).is_ok()); + } + assert!(validate_exam_kind("final").is_err()); + assert!(validate_exam_kind("").is_err()); + } + + #[tokio::test] + async fn mark_rules() { + for mark in [0, 1, 50, 99, 100] { + assert!(validate_mark(mark).is_ok()); + } + assert!(validate_mark(-1).is_err()); + assert!(validate_mark(101).is_err()); + } +}