Design Supabase schema for user identity and BankID verification status
epic-bankid-vipps-login-foundation-task-004 — Create the user_identities table in Supabase with columns: id (uuid PK), user_id (FK to auth.users), personnummer (text, encrypted at rest), bankid_verified (boolean), bankid_verified_at (timestamptz), vipps_sub (text), created_at, updated_at. Apply Row Level Security policies so users can only read and write their own identity record. Write and test the migration script.
Acceptance Criteria
Technical Requirements
Implementation Notes
Use Supabase CLI for migration management: supabase migration new create_user_identities generates a timestamped file. For personnummer encryption: use pgcrypto's pgp_sym_encrypt / pgp_sym_decrypt with a key stored in Supabase Vault (not hardcoded in SQL). If pgcrypto is not available in the project tier, document the column as requiring application-level encryption before insert — the Flutter app must encrypt personnummer with the user's public key before writing it to this column, and the field type should remain text. For the updated_at trigger: CREATE OR REPLACE FUNCTION trigger_set_updated_at() RETURNS trigger AS $$ BEGIN NEW.updated_at = now(); RETURN NEW; END; $$ LANGUAGE plpgsql; — reuse this function across all tables that need it.
The bankid_verified_at column must only be set server-side via a Supabase Edge Function or backend service after BankID verification succeeds — do not allow client-side writes to this column. Consider a separate RLS UPDATE policy that restricts which columns the client can write (use column-level security or a separate Edge Function for markBankIdVerified).
Testing Requirements
Manual SQL verification test after applying the migration: (1) Insert a test user into auth.users, then insert a user_identities row — confirm it succeeds; (2) Attempt to insert a second user_identities row for the same user_id — confirm unique constraint violation; (3) As a different user JWT, attempt to SELECT the first user's identity row — confirm zero rows returned (RLS blocks it); (4) UPDATE the record and confirm updated_at changes automatically; (5) DELETE the auth.users row and confirm user_identities row is cascade-deleted. Document these steps in a test checklist file at supabase/tests/user_identities_rls_test.md.
Flutter Secure Storage behavior differs between iOS Keychain and Android Keystore — key accessibility attributes (kSecAttrAccessibleWhenUnlocked vs. WhenUnlockedThisDeviceOnly) may cause tokens to become inaccessible after device restart or OS upgrade, breaking session restoration for returning users.
Mitigation & Contingency
Mitigation: Define explicit Keychain accessibility attributes during implementation and write integration tests on both platforms. Follow flutter_secure_storage documentation for cross-platform accessibility configuration.
Contingency: Implement a recovery flow that detects secure storage read failures and falls back to full re-authentication rather than crashing. Add a migration utility to re-write tokens with corrected attributes if a misconfiguration is discovered post-release.
Personnummer is a legally sensitive national identifier under Norwegian GDPR implementation. If encryption-at-rest or data minimization requirements are not met before launch, the feature could be blocked by legal/compliance review from any of the four partner organizations.
Mitigation & Contingency
Mitigation: Ensure personnummer is only persisted after explicit user consent via the personnummer confirmation widget. Use Supabase column-level encryption for the personnummer field. Document the data processing basis and retention policy before the first TestFlight release.
Contingency: If legal review blocks the personnummer write-back, implement the feature as opt-in only with a deferred sync model, allowing BankID/Vipps login to proceed without storing the personnummer until compliance is confirmed.
If the VippsOrgCostConfig data is not populated in Supabase for all four partner organizations before the feature ships, users from unconfigured organizations will see no Vipps login option and may report it as broken, creating confusion and support load.
Mitigation & Contingency
Mitigation: Create a seed migration script for Vipps org configuration and include it in the deployment checklist. Implement a clear admin UI warning when an organization is missing Vipps configuration.
Contingency: Add a feature flag in VippsOrgCostConfig so individual organizations can be enabled/disabled without a code deploy, allowing rapid remediation.