SPB Git

Repository Public

README.md 11.4 KB

# Integrations

Two small, independent third-party applications that integrate MacLustr Identity, plus the scripts that prove the integration end to end. They exist to show that several applications can use the same identity service without ever seeing each other's data.

demo-marketplace ("Brocante") demo-wallet ("Northbank Wallet")
Stack Node 18+, node:http, no framework Python 3.10+, FastAPI
SDK @maclustr/identity (file dependency on sdk/typescript) maclustr_identity (installed from sdk/python), async client
Default port 8881 8882
Gated action publish a listing raise the transfer limit
How the flow opens modal (openVerification from the browser SDK) full-page redirect with return_url
Draft before verify the listing is saved as a draft the limit request is saved
Public badge on each published listing on the public profile /u/{handle}

Secret keys stay on the server. In both demos the key (mlid_sk_test_…) and the webhook secret (whsec_…) are read from .env.local by the server process only. The browser receives a session URL and nothing else. e2e.mjs checks that no secret appears in anything served to the browser.

# Run

A local backend must answer on http://localhost:8870 with its webhook dispatcher (see the repository README; e2e.sh starts both when nothing answers).

bash
./demo-marketplace/setup.sh      # application + sandbox key + webhook on the local backend, writes .env.local
./demo-wallet/setup.sh
(cd demo-marketplace && npm start)                  # http://localhost:8881
(cd demo-wallet && .venv/bin/python app.py)         # http://localhost:8882

PORT=8890 ./demo-marketplace/setup.sh then PORT=8890 npm start uses another port (the allowed origin, the webhook URL and the return URL are provisioned for that port). setup.sh issues a new key on every run.

The setup goes through the backend CLI (app-create, key-create) and the public API (/v1/webhooks). The allowed origin (needed to frame the hosted page) and the allowed return URL are set by calling the registry function behind the operator console, because the CLI has no command for them yet.

# Automated proof

bash
./e2e.sh

It provisions both applications, boots both demos with empty state files, then e2e.mjs drives everything over HTTP. The person is simulated with the sandbox flow endpoints (consent, then a scenario), using only the session client secret. It asserts, among others:

  • the gated action is refused server-side (403), a draft is kept, then the action is allowed after verification;
  • the status change comes from the signed webhook;
  • rejected and manual_review outcomes never open the gate;
  • isolation: each key gets 404 on the other application's session and verification, cannot revoke or erase them, does not see the other's users, and neither webhook ever receives an event of the other application;
  • webhooks: unsigned, stale, or signed-with-the-other-secret deliveries are rejected, and an event id is processed once;
  • fail closed: a second instance of each demo pointed at an unreachable identity service refuses the gated action (503) even for a user whose local record says "verified";
  • a revocation closes the gate again and removes the badge.

When ports 8881/8882 are taken, e2e.sh uses the next free ones and says so. Everything it starts is stopped by PID on exit. KEEP_LOGS=1 ./e2e.sh keeps the logs.

browser-check.mjs is an optional real-browser run (Playwright) of the modal flow and of the redirect flow, including the hosted page itself; see the header of the file.


# Integrate MacLustr Identity into any application

The same seven steps apply whatever the stack. The two demos are complete examples of each step (demo-marketplace/server.mjs, demo-wallet/app.py).

# 0. Get credentials

An operator creates your application and gives you, per mode (sandbox / live):

  • a secret key mlid_sk_test_… / mlid_sk_live_… (server-side only, in a secret store or environment variable);
  • your allowed origins (to embed the hosted page in a modal) and allowed return URLs (redirect flow).

Create your webhook endpoint with the API (webhooks.create) and store its secret whsec_… next to the key.

# 1. Decide what is gated, and gate it on the server

Pick the sensitive actions (publishing, payouts, higher limits...). Enforce the rule in the request handler of that action, never only in the interface:

text
handler(action):
    record = db.identity[user]                       # your own table
    if record is None or record.status != "verified" or expired(record): refuse 403
    try:    live = identity.verifications.status(record.verification_id)   # still valid right now?
    except: refuse 503                               # fail closed
    if not live.valid: refuse 403
    perform(action)
  • Fail closed. If the identity service cannot be reached, the action stays refused. Never treat an error or a timeout as "verified".
  • Only verified opens the gate. manual_review means "wait"; inconclusive, rejected, revoked, expired, cancelled mean "not verified". After anything but verified and manual_review the user may simply start a new session: a cancelled or expired session, and an inconclusive result (for example reason review_not_completed, when the review by a person was not completed before its deadline), are not refusals.
  • Statuses, reason codes and event types grow over time: treat an unknown value as "not verified", never as an error.
  • The live check is optional when your webhook handling is reliable; it closes the window between a revocation and the delivery of its webhook.

# 2. Draft before verify

Let the user prepare the gated thing first (draft listing, saved request) and ask for verification at the moment it is needed. Nothing is lost if the verification takes time or goes to manual review, and the user understands why they are being asked.

# 3. Create the session on your server

ts
const session = await identity.verificationSessions.create({
  purpose: "Publish listings",          // shown to the person
  external_user_id: user.id,            // opaque id: never an e-mail, a name or a phone number
  return_url: "https://app.example/verify/return",   // redirect flow only
});
// send session.url to the browser; keep session.id with the user

The request is authenticated by your secret key, so it can only come from your server. The url embeds a client secret scoped to that single session; handing it to that user's browser is the intended use.

# 4. Open the hosted flow

  • Modal (web): openVerification({ url, mode: "modal", onComplete, onCancel }) from @maclustr/identity/browser. Your page origin must be in the application's allowed origins.
  • Redirect (web, simplest): send the browser to session.url; the hosted page sends the person back to return_url. Do not trust anything in the return URL: look the result up on your server.
  • Native apps: use the Swift / Kotlin SDKs (sdk/swift, sdk/kotlin) with the same server-created session.

Your application never asks for, receives or stores identity documents, selfies or extracted fields. Do not add an upload field "to speed things up".

# 5. Receive the webhook

text
POST /webhooks/identity
    raw = request raw body (bytes, before any JSON parsing)
    event = constructEvent(raw, header "MLID-Signature", webhook secret)   # reject with 400 on failure
    if seen(event.id): return 200                                          # at-least-once delivery
    if event carries a verification: upsert db.identity[user] from it      # verified, manual_review, rejected, inconclusive, revoked
    if event is verification.cancelled or .expired:                        # carries the session: nothing was decided
        forget the pending session; close a record still "manual_review" for that session
    remember(event.id)                                                     # last: a failure above is retried, not skipped
    return 200 quickly                                                     # also for an event type you do not know
  • Verify the signature on the raw body. Reject anything that fails; do not "fall back" to trusting it.
  • Deduplicate on event.id (unique constraint). Events can arrive more than once and out of order: do not let an older event overwrite a newer state. The demos apply three rules that do not depend on your server's clock: revoked is final for a given verification; a decided verification never goes back to manual_review; an event about an older verification (created_at) never replaces a newer one.
  • Find your user with external_user_id. Subscribe to verification.verified, .manual_review, .inconclusive, .rejected, .revoked, .cancelled, .expired (or *).
  • verification.cancelled is sent when your server cancels a session and when the person withdraws consent, including during a manual review. In that last case no decision event follows: the session object carries the verification_id of the review that will never conclude.
  • Once the signature is valid, answer 2xx even for an event type or an object you do not know yet: an error only makes the service deliver it again.
  • Answer 2xx within 10 seconds; failed deliveries are retried with a growing delay.

# 6. Store only the result

Per user, keep exactly:

Field Use
verification_id to query, revoke, or request erasure later
status verified, manual_review, inconclusive, rejected, revoked
assurance_level how strong the check was (for example document_check, document_face_liveness_check, document_manual_review)
expires_at a verification is valid for a limited time; treat a past date as not verified

No name, document number, date of birth, address, image or biometric data: your application does not receive them, and must not collect them on the side. When a user deletes their account, call verifications.deletePersonalData(verification_id).

# 7. Show the status honestly

A public "Identity verified" badge must come with an explanation that is one click away:

  • what it certifies: this person completed an identity check (document examined, and depending on the method compared with the person presenting it), valid for a limited time, revocable;
  • what it does not certify: it never proves ownership of a good or a property, nor the right to sell, rent or advertise it; it says nothing about honesty, solvency or the accuracy of a listing; it is not an endorsement;
  • what is never shown: no personal detail, because the application does not have any.

Remove the badge as soon as the verification is revoked or expired.

# Checklist before going live

  • The secret key and the webhook secret exist only in server-side configuration; nothing starting with mlid_sk_ or whsec_ is in the web bundle, the mobile app or the repository.
  • The gated action is refused by the server for an unverified user, with a test that proves it.
  • The action is refused when the identity service is unreachable (test with a wrong base URL).
  • The webhook rejects an unsigned or badly signed request and ignores a repeated event.id.
  • A revocation closes the gate and removes the badge.
  • Sandbox and live keys are in separate environments; sandbox results (simulated: true) never unlock anything in production.
  • The badge explanation is visible to the public.
Files main
Menu

Theme