normogen/backend/src/models/profile.rs
goose 149ce37654 feat: zero-knowledge encryption Phase 1 — backend opaque + crypto module (WIP)
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.
2026-06-28 20:08:38 -03:00

84 lines
2.4 KiB
Rust

use mongodb::{
bson::{doc, oid::ObjectId, DateTime},
Collection,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Profile {
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
pub id: Option<ObjectId>,
#[serde(rename = "profileId")]
pub profile_id: String,
#[serde(rename = "userId")]
pub user_id: String,
#[serde(rename = "familyId")]
pub family_id: Option<String>,
#[serde(rename = "name")]
pub name: String,
#[serde(rename = "nameIv")]
pub name_iv: String,
#[serde(rename = "nameAuthTag")]
pub name_auth_tag: String,
#[serde(rename = "role")]
pub role: String,
#[serde(rename = "permissions")]
pub permissions: Vec<String>,
#[serde(rename = "createdAt")]
pub created_at: DateTime,
#[serde(rename = "updatedAt")]
pub updated_at: DateTime,
}
pub struct ProfileRepository {
collection: Collection<Profile>,
}
impl ProfileRepository {
pub fn new(collection: Collection<Profile>) -> Self {
Self { collection }
}
pub async fn create(&self, profile: &Profile) -> mongodb::error::Result<()> {
self.collection.insert_one(profile, None).await?;
Ok(())
}
pub async fn find_by_profile_id(
&self,
profile_id: &str,
) -> mongodb::error::Result<Option<Profile>> {
self.collection
.find_one(doc! { "profileId": profile_id }, None)
.await
}
/// Look up a profile by its owning user id.
pub async fn find_by_user_id(&self, user_id: &str) -> mongodb::error::Result<Option<Profile>> {
self.collection
.find_one(doc! { "userId": user_id }, None)
.await
}
/// Update the profile's display name (opaque client-encrypted blob).
pub async fn update_encrypted_name(
&self,
user_id: &str,
name_data: &str,
name_iv: &str,
name_auth_tag: &str,
) -> mongodb::error::Result<Option<Profile>> {
self.collection
.find_one_and_update(
doc! { "userId": user_id },
doc! { "$set": {
"name": name_data,
"nameIv": name_iv,
"nameAuthTag": name_auth_tag,
"updatedAt": DateTime::now()
}},
None,
)
.await
}
}