@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / b5eac0d
Fork 0

سامانه صدور فاکتور و حسابداری

Mirrored from https://github.com/davoodmehraban89/Factor-easy.git

Synced Dec 4, 10:30 UTC

docs(otp): safe staged activation, existing identity linking and external-cost gates

davoodmehraban89 committed Oct 12, 2026 at 22:43 UTC b5eac0da5d148722edaad9a6500aa915824fe711
1 file changed +38
docs/FINORA_KAVENEGAR_OTP_PREPARATION_2026-10-12.md new
+38
@@ -0,0 +1,38 @@
1 +# Finora — Kavenegar SMS OTP integration preparation (2026-10-12)
2 +
3 +**Status: OFFLINE PREPARATION ONLY; NO LIVE SMS, NO AUTH CONFIG CHANGE, NO LIVE EDGE DEPLOY.**
4 +
5 +## Authority / current factual baseline
6 +Owner has **approved preparation** of mobile OTP integration, **not claimed that Kavenegar approved** the template. Last Kavenegar screenshot of `FinoraLogin` displayed `در حال بررسی`. Do not infer approved from chat shorthand. Sender/real delivery/provider credit have NOT been verified.
7 +
8 +The existing application uses Supabase Auth email OTP / password / Google; `public.profiles.phone` and unique non-null phone index exist; prior live migration `phone_identity_profile` was recorded, but that does **not** prove matching `auth.users.phone` or securely linked identities. Historic phone PR #58 is open/stale; do not merge it or reapply its migration without reconciliation. Supabase currently has **no deployed** `finora-kavenegar-send-sms` Edge Function.
9 +
10 +An authorized Remote Desktop Commander device `DESKTOP-71SJ63M` is online. Local checkout `C:\Temp\Factor-easy` was read-only checked: branch `main`, SHA `b1a35fdc1df71c46c287bec47be54fa6e8641428`, 0 tracked changes and 2189 untracked files. **It is stale and has many untracked files**: do not reset, clean, overwrite or sync it without separate review. This preparation is authored on a GitHub feature branch.
11 +
12 +## Proposed integration
13 +`Supabase Auth` creates and validates OTP; it calls its **Send SMS HTTP Hook** with a signed Standard Webhooks event (`user.phone`, `sms.otp`). Server-side Deno Edge Function verifies **raw-body signature and timestamp**, validates E.164 `+989xxxxxxxxx` phone and 6–10 digit OTP, converts phone to local `09...` and posts `application/x-www-form-urlencoded` to Kavenegar `/v1/<API_KEY>/verify/lookup.json` with fixed `template=FinoraLogin`. Supabase alone issues Auth JWT/session and existing Finora membership/license/RLS rules remain authoritative.
14 +
15 +Function files: `supabase/functions/finora-kavenegar-send-sms/{index.ts,handler.mjs}`. It rejects all requests unless `FINORA_KAVENEGAR_OTP_ENABLED=true`, and even then rejects unsigned, expired, malformed or missing-secret events. It **never** receives a browser call or generates, stores or verifies the OTP. No customer identity search, tenant data access, service-role key, SQL or business record write is required.
16 +
17 +## Activation gates (do not perform in preparation phase)
18 +1. Independently see `FinoraLogin` in Kavenegar state **تأیید شده**. Confirm verify/lookup is allowed on this tariff, short-SMS unit cost/credit and live sender deliverability.
19 +2. Set `KAVENEGAR_API_KEY` and `SEND_SMS_HOOK_SECRET` **only in Supabase server-side encrypted Edge secrets**, not in messages, public GitHub, logs, browser, desktop env files or screenshots. Generate hook signing secret through Supabase Auth Hooks (use actual value, not the sample). Configure Edge Function for `verify_jwt=false` **only at activation**, because incoming signed Auth hook requests have no end-user JWT; the handler enforces signature verification. This setting is sensitive; require owner confirmation.
20 +3. Deploy the Edge Function under owner-approved rollout, while `FINORA_KAVENEGAR_OTP_ENABLED` remains unset/false; configure Auth's `Send SMS Hook` to its HTTPS URL and enable the phone provider only in a controlled window after explicit owner confirmation. **Never** configure a fail-closed disabled hook as the active route for real users.
21 +4. Stage one real SMS to an explicitly approved test number; external messaging **and cost** require owner authorization. A Kavenegar API status 200 means *accepted by provider*, not verified delivery. Verify real handset receipt and OTP expiry/attempt limit, no 1-to-many runaway resends.
22 +5. **Before real login rollout:** authenticated owner must securely link a verified phone to **the existing `auth.users.id`**, reconcile any conflicting existing phone identity and preserve memberships, licenses and all tenant/financial records. The existing profile's phone text is not, by itself, a validated Auth login method. Do not directly mutate Auth or RLS without explicit authorization. Keep `shouldCreateUser:false` for existing-user sign-in; do not auto-create anonymous phone users.
23 +6. Implement the mobile-first login form in a separately gated PR **after** provider, identity linking and server hooks pass. Preserve email/password and Google fallback. Verify browser CSP handlers, retry/rate limiting, CAPTCHA, account lockout, RTL mobile UX, session restore, +98 normalization, masking/error states, and correct org isolation; validate on owner's affected ISP.
24 +7. Merge/release only after full static/Chromium/Golden/3 PostgreSQL/Security checks, gated Pages and owner test. F0-01 and Phase 0 remain open until real acceptance. Rollback via removing SMS entry point and restoring existing Auth settings after coordinated owner approval.
25 +
26 +## Offline acceptance and limitations
27 +- Run `node scripts/kavenegar-otp-preflight-test.mjs` (no internet, no SMS, synthetic token / hook signing secret); CI also executes it.
28 +- No secrets or credential data in repo. Never log the webhook body/provider URL, phone, token or upstream response JSON.
29 +- The hook enforces a 5-minute signed-message timestamp window but does **not** persist event-id replay deduplication. Supabase Auth rate limits and a later approved cost/sending control remain required before production.
30 +- Provider timeout is 3.5 seconds to fit Supabase Auth HTTP hook timeout (~5 seconds). Invalid provider response fails closed and returns non-200; it never falsely acknowledges SMS delivery.
31 +- **Not verified:** Kavenegar approval, actual SMS/charges, Supabase Auth phone setting, provider SMS hook wiring, live sender, actual phone-to-existing-user secure linking, site UI and mobile sign-in. Do not mark any as complete from passing synthetic tests.
32 +
33 +## Public technical references
34 +- Supabase: https://supabase.com/docs/guides/auth/auth-hooks/send-sms-hook
35 +- Supabase Auth Hook security: https://supabase.com/docs/guides/auth/auth-hooks
36 +- Standard Webhooks spec: https://github.com/standard-webhooks/standard-webhooks/blob/main/spec/standard-webhooks.md
37 +- Kavenegar SDK verification example: https://github.com/KaveNegar/kavenegar-node
38 +