Skip to content

feat(phase-5): Meta Account Connection & Unified Login - #5

Merged
arz282-ctrl merged 1 commit into
mainfrom
feat/phase-5-meta-account-connection
Nov 27, 2026
Merged

arz282-ctrl merged 1 commit into
mainfrom
feat/phase-5-meta-account-connection

Conversation

@arz282-ctrl

Copy link
Copy Markdown
Owner

Phase 5 Completion Report — Meta Account Connection

Branch: feat/phase-5-meta-account-connection (targeting main via PR #5)
Spec Reference: implementation (1).md (Sections 2, 8, 10, 11, 13, 15, 16, 17) & docs/operations/meta-app-setup.md
Cloud Budget Impact: $0/month (META_CONNECTIONS_MODE=mock by default until a real Meta App & Business Portfolio are configured; Supabase Free + Render Free + Cloudflare Pages Free)


1. Delivered Capabilities

1.1 Database Schema & Tenant Isolation (database/migrations/versions/0007_phase5_meta_connections.py)

  • Additive, cleanly reversible Alembic migration (0006_phase4_cloud_editing_and_approvals -> 0007_phase5_meta_connections) applied to live Supabase PostgreSQL 17.12:
    • meta_connections: stores opaque token_secret_ref with DB-level CHECK (token_secret_ref LIKE 'secret://%') so raw Meta tokens can never be persisted in ordinary database columns, plus connection_type, meta_business_id, config_id, granted_scopes, instagram_account_ids, page_ids, ad_account_ids, computed capabilities, and reconnect_required_at / reconnect_reason.
    • meta_connected_assets: stores only selected & eligible Instagram Professional (business / creator), Facebook Page, and optional Ad Account assets (enforces CHECK (instagram_account_type IN ('business', 'creator'))).
    • meta_webhook_events: signature-verified, deduplicated inbound webhook log (uq_meta_webhook_events_ws_event_id).
    • ENABLE + FORCE ROW LEVEL SECURITY and least-privilege metuse_app CRUD grants on all 3 tables.

1.2 Meta Integration Layer (packages/meta_integrations/)

  • AES-256-GCM Secret Store (secret_store.py): Server-side SecretStore protocol + EncryptedSecretStore issuing opaque secret://meta/{workspace_id}/{uuid} refs and scrubbing raw tokens from API views and logs (strictly separate from Meta Muse keys).
  • Permission Matrix & Capability Gating (capabilities.py): Section 2.3 minimal permission matrix, Section 8.4 compute_capabilities(...) requiring BOTH granted scope AND eligible connected asset, and backend require_meta_capability(...) enforcing 403 META_PERMISSION_MISSING with reconnect guidance (Acceptance #6).
  • Mock & Live Meta Graph Clients (client.py): $0 deterministic MockMetaGraphClient covering Creator Pro (#4), Agency Business portfolio with unselected-asset exclusion (#5), Personal IG blocking (#3), and missing-scope gating (#6), plus LiveMetaGraphClient gated on META_APP_ID + META_APP_SECRET.
  • Unified Login & Webhook Service (service.py): Exact redirect URI allowlist validation, HS256 CSRF-bound state tied to (workspace_id, actor_user_id), server-side code exchange, personal IG blocking with conversion steps and zero stored connection, webhook GET challenge + POST X-Hub-Signature-256 HMAC-SHA256 verification, revocation handling (reconnect_required + vault secret deletion), and clean disconnect.

1.3 FastAPI Routes, Meta App Setup Docs & Connection UI

  • Meta API (apps/api/routes/meta.py):
    • GET /api/v1/workspaces/{workspace_id}/meta/permission-matrix
    • GET /api/v1/workspaces/{workspace_id}/meta/connections
    • POST /api/v1/workspaces/{workspace_id}/meta/connections/unified-login/start
    • GET/POST /api/v1/workspaces/{workspace_id}/meta/connections/unified-login/callback
    • POST /api/v1/workspaces/{workspace_id}/meta/connections/{connection_id}/reconnect
    • DELETE /api/v1/workspaces/{workspace_id}/meta/connections/{connection_id}
    • GET /api/v1/workspaces/{workspace_id}/meta/connections/{connection_id}/assets
    • GET /api/v1/workspaces/{workspace_id}/meta/connections/{connection_id}/capabilities
    • GET/POST /api/v1/workspaces/{workspace_id}/meta/webhooks
  • Setup & App Review Guide (docs/operations/meta-app-setup.md): Documents Meta Business Portfolio ownership, Unified Login Config ID, Standard vs Advanced Access App Review checklist, and permission matrix.
  • Web UI (apps/web/src/app/(app)/connections/page.tsx): Static-export /connections page with Creator Pro & Agency Business onboarding paths, personal IG eligibility test, connected account capability chips, disconnect action, and visible permission matrix (never asks users for passwords or raw tokens).

2. Verification Summary

Check Command Result
Python formatting ruff format --check . Passed (136 files)
Python linting ruff check . Passed (0 errors)
Python static typing mypy apps packages database workers tests Passed (109 source files, 0 issues)
Unit tests (Phase 0–5) pytest tests/unit -q Passed (55 passed in 1.49s)
Alembic migration head alembic heads 0007_phase5_meta_connections (head)
Live Supabase migration alembic upgrade head Applied 0006 -> 0007 cleanly
Live Supabase Phase 5 FORCE RLS & E2E tests pytest tests/integration/test_phase5_meta_rls.py -v Passed (2 passed in 18.76s)
Frontend lint & typecheck pnpm --dir apps/web lint && pnpm --dir apps/web typecheck Passed (0 errors)
Frontend static export NEXT_PUBLIC_API_URL=https://metuse-ai-api-preview.onrender.com pnpm --dir apps/web build Passed (14/14 static pages including /connections)
Secret hygiene scan grep across Phase 5 files Clean (0 leaks)
Acceptance criteria covered #3, #4, #5, #6, #18 Verified in unit + live Supabase integration suites

3. Deployment & Next Phase

  • Cloudflare Pages Preview: Deployed updated apps/web/out (including /connections) to https://develop.metuse-ai-web.pages.dev/connections.
  • Render Preview API: Automatically deploys Phase 5 /meta/* endpoints once PR feat(phase-5): Meta Account Connection & Unified Login #5 is merged into main.
  • Recommended Next Phase: Phase 6 — Publishing & Insights (content calendar, scheduling, idempotent Instagram container publishing state machine, remote-state verification preventing duplicate posts on ambiguous retries, and workspace/client-attributed insights sync — Acceptance #14, #15).

@arz282-ctrl
arz282-ctrl merged commit a47e5c3 into main Nov 27, 2026
0 of 6 checks passed
@arz282-ctrl
arz282-ctrl deleted the feat/phase-5-meta-account-connection branch November 28, 2026 01:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant