Backend: server can no longer read user data. All data blobs (medication, appointment, profile name) are now opaque client-encrypted ciphertext — the server stores and returns them verbatim, never deserializing the contents. - Medication: removed MedicationData + flat MedicationResponse; new MedicationResponse echoes metadata + encrypted_data blob. Create/update accept opaque blobs (whole-blob replace). Update is no longer load-mutate- reserialize (server can't read the data). - Appointment: same opaque treatment; status moved to a top-level document field so it remains filterable without decryption. - Profile: name is now an opaque encrypted blob (name_data/name_iv). Auto- created profile on register starts with an empty name; client sets it. - EncryptedFieldWire type shared across medication/appointment. Frontend (partial): crypto module using Web Crypto API — - crypto/keys.ts: double-PBKDF2 derivation (auth secret sent to server + encryption key kept in memory); in-memory key store (set/get/clear). - crypto/cipher.ts: AES-GCM encrypt/decrypt + JSON convenience wrappers. - crypto/index.ts: re-exports. NOT YET DONE (frontend integration): auth store key derivation on login/register, stores decrypt-on-load/encrypt-on-write, types update, UI components wired, crypto round-trip tests, ADR. This commit is a verified checkpoint — backend builds clean (21 tests, 0 warnings); frontend crypto module exists but is not yet wired into the data flow.
126 lines
4 KiB
Rust
126 lines
4 KiB
Rust
use axum::{
|
|
extract::{Extension, Json, Path, Query, State},
|
|
http::StatusCode,
|
|
};
|
|
use serde::Deserialize;
|
|
use std::time::SystemTime;
|
|
|
|
use crate::{
|
|
auth::jwt::Claims,
|
|
config::AppState,
|
|
models::appointment::{
|
|
AppointmentRepository, AppointmentResponse, CreateAppointmentRequest,
|
|
UpdateAppointmentRequest,
|
|
},
|
|
models::health_data::EncryptedField,
|
|
};
|
|
use mongodb::bson::DateTime;
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct AppointmentListQuery {
|
|
/// Filter by status (upcoming/completed/cancelled). Optional.
|
|
pub status: Option<String>,
|
|
}
|
|
|
|
pub async fn create_appointment(
|
|
State(state): State<AppState>,
|
|
Extension(claims): Extension<Claims>,
|
|
Json(req): Json<CreateAppointmentRequest>,
|
|
) -> Result<Json<AppointmentResponse>, StatusCode> {
|
|
let database = state.db.get_database();
|
|
let repo = AppointmentRepository::new(database.collection("appointments"));
|
|
|
|
let now = SystemTime::now();
|
|
let appointment_id = uuid::Uuid::new_v4().to_string();
|
|
|
|
// Zero-knowledge: store the client's opaque encrypted blob verbatim.
|
|
let appointment_data = EncryptedField {
|
|
data: req.encrypted_data.data,
|
|
encrypted: true,
|
|
iv: req.encrypted_data.iv,
|
|
auth_tag: req.encrypted_data.auth_tag,
|
|
};
|
|
|
|
let appointment = crate::models::appointment::Appointment {
|
|
id: None,
|
|
appointment_id,
|
|
user_id: claims.sub,
|
|
profile_id: req.profile_id.clone(),
|
|
appointment_data,
|
|
reminders: vec![],
|
|
status: req.status,
|
|
created_at: DateTime::from_system_time(now),
|
|
updated_at: DateTime::from_system_time(now),
|
|
};
|
|
|
|
match repo.create(appointment).await {
|
|
Ok(appt) => Ok(Json(appt.into())),
|
|
Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR),
|
|
}
|
|
}
|
|
|
|
pub async fn list_appointments(
|
|
State(state): State<AppState>,
|
|
Extension(claims): Extension<Claims>,
|
|
Query(query): Query<AppointmentListQuery>,
|
|
) -> Result<Json<Vec<AppointmentResponse>>, StatusCode> {
|
|
let database = state.db.get_database();
|
|
let repo = AppointmentRepository::new(database.collection("appointments"));
|
|
|
|
match repo
|
|
.find_by_user_filtered(&claims.sub, query.status.as_deref())
|
|
.await
|
|
{
|
|
Ok(appointments) => {
|
|
let resp: Vec<AppointmentResponse> = appointments.into_iter().map(Into::into).collect();
|
|
Ok(Json(resp))
|
|
}
|
|
Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR),
|
|
}
|
|
}
|
|
|
|
pub async fn get_appointment(
|
|
State(state): State<AppState>,
|
|
Extension(_claims): Extension<Claims>,
|
|
Path(id): Path<String>,
|
|
) -> Result<Json<AppointmentResponse>, StatusCode> {
|
|
let database = state.db.get_database();
|
|
let repo = AppointmentRepository::new(database.collection("appointments"));
|
|
|
|
match repo.find_by_appointment_id(&id).await {
|
|
Ok(Some(appt)) => Ok(Json(appt.into())),
|
|
Ok(None) => Err(StatusCode::NOT_FOUND),
|
|
Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR),
|
|
}
|
|
}
|
|
|
|
pub async fn update_appointment(
|
|
State(state): State<AppState>,
|
|
Extension(_claims): Extension<Claims>,
|
|
Path(id): Path<String>,
|
|
Json(req): Json<UpdateAppointmentRequest>,
|
|
) -> Result<Json<AppointmentResponse>, StatusCode> {
|
|
let database = state.db.get_database();
|
|
let repo = AppointmentRepository::new(database.collection("appointments"));
|
|
|
|
match repo.update_by_appointment_id(&id, req).await {
|
|
Ok(Some(appt)) => Ok(Json(appt.into())),
|
|
Ok(None) => Err(StatusCode::NOT_FOUND),
|
|
Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR),
|
|
}
|
|
}
|
|
|
|
pub async fn delete_appointment(
|
|
State(state): State<AppState>,
|
|
Extension(_claims): Extension<Claims>,
|
|
Path(id): Path<String>,
|
|
) -> Result<StatusCode, StatusCode> {
|
|
let database = state.db.get_database();
|
|
let repo = AppointmentRepository::new(database.collection("appointments"));
|
|
|
|
match repo.delete_by_appointment_id(&id).await {
|
|
Ok(true) => Ok(StatusCode::NO_CONTENT),
|
|
Ok(false) => Err(StatusCode::NOT_FOUND),
|
|
Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR),
|
|
}
|
|
}
|