SPB Git

Repository Public

README.md 11.4 KB 205 lines markdown

1# Integrations23Two small, independent third-party applications that integrate **MacLustr Identity**, plus the scripts that4prove the integration end to end. They exist to show that several applications can use the same identity5service without ever seeing each other's data.67| | `demo-marketplace` ("Brocante") | `demo-wallet` ("Northbank Wallet") |8|---|---|---|9| Stack | Node 18+, `node:http`, no framework | Python 3.10+, FastAPI |10| SDK | `@maclustr/identity` (file dependency on `sdk/typescript`) | `maclustr_identity` (installed from `sdk/python`), async client |11| Default port | 8881 | 8882 |12| Gated action | publish a listing | raise the transfer limit |13| How the flow opens | modal (`openVerification` from the browser SDK) | full-page redirect with `return_url` |14| Draft before verify | the listing is saved as a draft | the limit request is saved |15| Public badge | on each published listing | on the public profile `/u/{handle}` |1617> **Secret keys stay on the server.** In both demos the key (`mlid_sk_test_…`) and the webhook secret18> (`whsec_…`) are read from `.env.local` by the server process only. The browser receives a session URL and19> nothing else. `e2e.mjs` checks that no secret appears in anything served to the browser.2021## Run2223A local backend must answer on `http://localhost:8870` with its webhook dispatcher (see the repository README;24`e2e.sh` starts both when nothing answers).2526```bash27./demo-marketplace/setup.sh      # application + sandbox key + webhook on the local backend, writes .env.local28./demo-wallet/setup.sh29(cd demo-marketplace && npm start)                  # http://localhost:888130(cd demo-wallet && .venv/bin/python app.py)         # http://localhost:888231```3233`PORT=8890 ./demo-marketplace/setup.sh` then `PORT=8890 npm start` uses another port (the allowed origin, the34webhook URL and the return URL are provisioned for that port). `setup.sh` issues a new key on every run.3536The setup goes through the backend CLI (`app-create`, `key-create`) and the public API (`/v1/webhooks`).37The allowed origin (needed to frame the hosted page) and the allowed return URL are set by calling the38registry function behind the operator console, because the CLI has no command for them yet.3940## Automated proof4142```bash43./e2e.sh44```4546It provisions both applications, boots both demos with empty state files, then `e2e.mjs` drives everything47over HTTP. The person is simulated with the sandbox flow endpoints (consent, then a scenario), using only the48session client secret. It asserts, among others:4950- the gated action is refused server-side (403), a draft is kept, then the action is allowed after verification;51- the status change comes from the signed webhook;52- `rejected` and `manual_review` outcomes never open the gate;53- isolation: each key gets 404 on the other application's session and verification, cannot revoke or erase them,54  does not see the other's users, and neither webhook ever receives an event of the other application;55- webhooks: unsigned, stale, or signed-with-the-other-secret deliveries are rejected, and an event id is processed once;56- fail closed: a second instance of each demo pointed at an unreachable identity service refuses the gated57  action (503) even for a user whose local record says "verified";58- a revocation closes the gate again and removes the badge.5960When ports 8881/8882 are taken, `e2e.sh` uses the next free ones and says so. Everything it starts is stopped61by PID on exit. `KEEP_LOGS=1 ./e2e.sh` keeps the logs.6263`browser-check.mjs` is an optional real-browser run (Playwright) of the modal flow and of the redirect flow,64including the hosted page itself; see the header of the file.6566---6768# Integrate MacLustr Identity into any application6970The same seven steps apply whatever the stack. The two demos are complete examples of each step71(`demo-marketplace/server.mjs`, `demo-wallet/app.py`).7273## 0. Get credentials7475An operator creates your application and gives you, per mode (sandbox / live):7677- a secret key `mlid_sk_test_…` / `mlid_sk_live_…` (server-side only, in a secret store or environment variable);78- your allowed origins (to embed the hosted page in a modal) and allowed return URLs (redirect flow).7980Create your webhook endpoint with the API (`webhooks.create`) and store its secret `whsec_…` next to the key.8182## 1. Decide what is gated, and gate it on the server8384Pick the sensitive actions (publishing, payouts, higher limits...). Enforce the rule in the request handler of85that action, never only in the interface:8687```88handler(action):89    record = db.identity[user]                       # your own table90    if record is None or record.status != "verified" or expired(record): refuse 40391    try:    live = identity.verifications.status(record.verification_id)   # still valid right now?92    except: refuse 503                               # fail closed93    if not live.valid: refuse 40394    perform(action)95```9697- **Fail closed.** If the identity service cannot be reached, the action stays refused. Never treat an error or98  a timeout as "verified".99- Only `verified` opens the gate. `manual_review` means "wait"; `inconclusive`, `rejected`, `revoked`,100  `expired`, `cancelled` mean "not verified". After anything but `verified` and `manual_review` the user may101  simply start a new session: a cancelled or expired session, and an `inconclusive` result (for example reason102  `review_not_completed`, when the review by a person was not completed before its deadline), are not refusals.103- Statuses, reason codes and event types grow over time: treat an unknown value as "not verified", never as an104  error.105- The live check is optional when your webhook handling is reliable; it closes the window between a revocation106  and the delivery of its webhook.107108## 2. Draft before verify109110Let the user prepare the gated thing first (draft listing, saved request) and ask for verification at the111moment it is needed. Nothing is lost if the verification takes time or goes to manual review, and the user112understands why they are being asked.113114## 3. Create the session on your server115116```ts117const session = await identity.verificationSessions.create({118  purpose: "Publish listings",          // shown to the person119  external_user_id: user.id,            // opaque id: never an e-mail, a name or a phone number120  return_url: "https://app.example/verify/return",   // redirect flow only121});122// send session.url to the browser; keep session.id with the user123```124125The request is authenticated by your secret key, so it can only come from your server. The `url` embeds a126client secret scoped to that single session; handing it to that user's browser is the intended use.127128## 4. Open the hosted flow129130- **Modal** (web): `openVerification({ url, mode: "modal", onComplete, onCancel })` from131  `@maclustr/identity/browser`. Your page origin must be in the application's allowed origins.132- **Redirect** (web, simplest): send the browser to `session.url`; the hosted page sends the person back to133  `return_url`. Do not trust anything in the return URL: look the result up on your server.134- **Native apps**: use the Swift / Kotlin SDKs (`sdk/swift`, `sdk/kotlin`) with the same server-created session.135136Your application never asks for, receives or stores identity documents, selfies or extracted fields. Do not137add an upload field "to speed things up".138139## 5. Receive the webhook140141```142POST /webhooks/identity143    raw = request raw body (bytes, before any JSON parsing)144    event = constructEvent(raw, header "MLID-Signature", webhook secret)   # reject with 400 on failure145    if seen(event.id): return 200                                          # at-least-once delivery146    if event carries a verification: upsert db.identity[user] from it      # verified, manual_review, rejected, inconclusive, revoked147    if event is verification.cancelled or .expired:                        # carries the session: nothing was decided148        forget the pending session; close a record still "manual_review" for that session149    remember(event.id)                                                     # last: a failure above is retried, not skipped150    return 200 quickly                                                     # also for an event type you do not know151```152153- Verify the signature on the **raw body**. Reject anything that fails; do not "fall back" to trusting it.154- Deduplicate on `event.id` (unique constraint). Events can arrive more than once and out of order: do not let155  an older event overwrite a newer state. The demos apply three rules that do not depend on your server's clock:156  `revoked` is final for a given verification; a decided verification never goes back to `manual_review`; an157  event about an older verification (`created_at`) never replaces a newer one.158- Find your user with `external_user_id`. Subscribe to `verification.verified`, `.manual_review`,159  `.inconclusive`, `.rejected`, `.revoked`, `.cancelled`, `.expired` (or `*`).160- `verification.cancelled` is sent when your server cancels a session and when the person withdraws consent,161  including during a manual review. In that last case no decision event follows: the session object carries the162  `verification_id` of the review that will never conclude.163- Once the signature is valid, answer 2xx even for an event type or an object you do not know yet: an error164  only makes the service deliver it again.165- Answer 2xx within 10 seconds; failed deliveries are retried with a growing delay.166167## 6. Store only the result168169Per user, keep exactly:170171| Field | Use |172|---|---|173| `verification_id` | to query, revoke, or request erasure later |174| `status` | `verified`, `manual_review`, `inconclusive`, `rejected`, `revoked` |175| `assurance_level` | how strong the check was (for example `document_check`, `document_face_liveness_check`, `document_manual_review`) |176| `expires_at` | a verification is valid for a limited time; treat a past date as not verified |177178No name, document number, date of birth, address, image or biometric data: your application does not receive179them, and must not collect them on the side. When a user deletes their account, call180`verifications.deletePersonalData(verification_id)`.181182## 7. Show the status honestly183184A public "Identity verified" badge must come with an explanation that is one click away:185186- **what it certifies**: this person completed an identity check (document examined, and depending on the187  method compared with the person presenting it), valid for a limited time, revocable;188- **what it does not certify**: it never proves ownership of a good or a property, nor the right to sell, rent189  or advertise it; it says nothing about honesty, solvency or the accuracy of a listing; it is not an endorsement;190- **what is never shown**: no personal detail, because the application does not have any.191192Remove the badge as soon as the verification is revoked or expired.193194## Checklist before going live195196- [ ] The secret key and the webhook secret exist only in server-side configuration; nothing starting with197      `mlid_sk_` or `whsec_` is in the web bundle, the mobile app or the repository.198- [ ] The gated action is refused by the server for an unverified user, with a test that proves it.199- [ ] The action is refused when the identity service is unreachable (test with a wrong base URL).200- [ ] The webhook rejects an unsigned or badly signed request and ignores a repeated `event.id`.201- [ ] A revocation closes the gate and removes the badge.202- [ ] Sandbox and live keys are in separate environments; sandbox results (`simulated: true`) never unlock203      anything in production.204- [ ] The badge explanation is visible to the public.205
Files main
Menu

Theme