diff --git a/.changeset/server-side-sub-org-lookup.md b/.changeset/server-side-sub-org-lookup.md new file mode 100644 index 0000000..8794499 --- /dev/null +++ b/.changeset/server-side-sub-org-lookup.md @@ -0,0 +1,5 @@ +--- +'@monad-inc/embed': patch +--- + +docs: look up the sub-org on the server in the session example, instead of trusting a sub-org id sent by the browser, and note session expiry diff --git a/packages/embed/USAGE.md b/packages/embed/USAGE.md index 5e0e7d9..2b8afa5 100644 --- a/packages/embed/USAGE.md +++ b/packages/embed/USAGE.md @@ -33,17 +33,30 @@ iframe using that token. Both shown below. Your backend exchanges its long-lived Monad API key for a short-lived, sub-org-scoped session token, then hands that token to your frontend. +Your backend decides which sub-org the session is for. Look it up from +the signed-in user's own session, never from anything the browser sends: +a sub-org id in the request body could be changed to another +customer's. + Example (any HTTP framework — shown here as a Next.js Route Handler): ```ts // app/api/monad/session/route.ts export async function POST(req: Request) { - const { subOrgId } = await req.json(); // your tenant → Monad sub-org mapping + // Your own auth: who is signed in, and which of your tenants they belong to. + const user = await getSignedInUser(req); + if (!user) return new Response(null, { status: 401 }); + + // Your server-side tenant → Monad sub-org mapping, stored when you + // created the customer's sub-org. + const subOrgId = await lookUpMonadSubOrg(user.tenantId); + if (!subOrgId) return new Response(null, { status: 404 }); const r = await fetch('https://app.monad.com/api/v3/sessions', { method: 'POST', headers: { 'Content-Type': 'application/json', + 'User-Agent': 'your-product/1.0', Authorization: `ApiKey ${process.env.MONAD_API_KEY}` }, body: JSON.stringify({ @@ -69,6 +82,15 @@ Returns: The `session_token` is what your frontend hands to `createConnectorFrame`. **Never put your long-lived `MONAD_API_KEY` in browser code.** +- **Always send `organization_id`.** A session created without it gets + the full access of the API key that created it. +- **If one user can work in several of your tenants**, the browser may + say which tenant it wants, but check on your server that the + signed-in user belongs to it before minting, and return 403 if not. +- **Sessions expire** (`ttl_seconds`, default 1800). The form takes the + token once, at mount, so keep `expires_at` and mint a new session and + remount the form before it expires. + --- ## Step 2 — Mount the iframe on your frontend @@ -76,10 +98,10 @@ The `session_token` is what your frontend hands to `createConnectorFrame`. ```ts import { createConnectorFrame } from '@monad-inc/embed/connect'; -// Fetch a fresh session from your own backend. +// Fetch a fresh session from your own backend. Send no sub-org id: +// your backend looks it up from the signed-in user. const { session_token, organizationId } = await fetch('/api/monad/session', { - method: 'POST', - body: JSON.stringify({ subOrgId: currentUser.subOrgId }) + method: 'POST' }).then((r) => r.json()); const frame = createConnectorFrame({