Implement Field Encryption Utilities
epic-contact-detail-and-edit-main-screen-task-004 — Build the field encryption utility layer for Blindeforbundet's sensitive contact data (name, address, epikrise references). Implement AES-256 envelope encryption with Supabase Vault key management, decryption on authorized access, and a read-receipt hook that records when a field was first decrypted by which user. Must handle key rotation gracefully and expose a clean async API.
Acceptance Criteria
Technical Requirements
Implementation Notes
Use the envelope encryption pattern: generate a random DEK per contact record (or per field group), encrypt the DEK with the Vault master key (Vault stores and manages the master key — you never see it), store the encrypted DEK alongside the ciphertext. On decryption, send the encrypted DEK to Vault for unwrapping, then use the unwrapped DEK locally for AES-GCM decryption. Use pointycastle's GCMBlockCipher for AES-256-GCM. Cache the unwrapped DEK in a private in-memory map keyed by contact_id, invalidated on Flutter AppLifecycleState.paused.
The read-receipt hook should be a separate FieldReadReceiptService that is called from the decryption utility via a callback or post-decryption hook — keep it decoupled so it can be mocked independently. For key rotation, add a key_version field to the encrypted payload DTO; the decrypt path reads this version and fetches the appropriate Vault key by version ID. Consider a Supabase RPC function for read-receipt writes to enforce server-side validation (prevents clients from spoofing user IDs).
Testing Requirements
Unit tests for the AES-GCM encrypt/decrypt cycle using pointycastle directly (no Vault dependency). Integration tests against Supabase Vault staging: verify encrypt → store → retrieve → decrypt round-trip, verify tampered ciphertext is rejected (GCM tag failure), verify key rotation (encrypt with v1, rotate to v2, verify v1 ciphertext still decrypts, new encryptions use v2). Test read-receipt writes: mock Supabase client and verify fire-and-forget call is made after successful decryption. Test that read-receipt failure does NOT throw.
Test DEK cache invalidation on simulated app background event. Run all sensitive data tests in an isolated test environment with no real PII.
The Peer Mentor Profile tab on the contact detail screen depends on the peer-mentor-detail-screen-widget being delivered by the separate Peer Mentor Detail feature. If that feature is delayed, the navigation affordance will be present but lead to a stub screen, which may confuse coordinators in the TestFlight pilot.
Mitigation & Contingency
Mitigation: Implement the peer mentor tab with a feature flag guard. When the Peer Mentor Detail feature is incomplete, the flag disables the tab. Coordinate delivery timelines with the team responsible for Peer Mentor Detail to align TestFlight releases.
Contingency: If the Peer Mentor Detail feature is significantly delayed, ship the contact detail screen without the peer mentor tab in the first TestFlight build and add it as an incremental update once the dependent screen is ready.
The contact detail screen must adapt its layout significantly based on organisation context: NHF shows affiliation chips, Blindeforbundet shows encrypted fields and assignment status, standard contacts show neither. Managing this conditional rendering without introducing bugs in each variant is complex and increases the risk of organisation-specific regressions.
Mitigation & Contingency
Mitigation: Define a ContactDetailViewModel that resolves all org-specific flags (showEncryptedFields, showAssignmentStatus, showMultiChapterChips) from the organisation config before the widget tree renders. Widget tests must cover all three organisation variants as separate test cases to catch regressions.
Contingency: If conditional rendering logic grows unwieldy, refactor into separate composable section widgets (ProfileHeaderSection, AffiliationSection, EncryptedFieldsSection) that are conditionally included by the parent screen, isolating org-specific logic to individual components.