|
| 1 | +# Team provisioning |
| 2 | + |
| 3 | +This document describes the provisioning behavior implemented today and the |
| 4 | +product boundary it creates for embedding products such as FrontLit. It is a |
| 5 | +short orientation for maintainers; the executable contract remains |
| 6 | +`packages/api-contract/src/contract.ts`, and the implementation remains |
| 7 | +`apps/api/src/provisioning/routes.ts`. |
| 8 | + |
| 9 | +## Mental model |
| 10 | + |
| 11 | +- An **organization** is the administrative and provisioning boundary. It owns |
| 12 | + teams, organization API keys, organization ESPs, grants, policies, and |
| 13 | + organization-level audit events. |
| 14 | +- A **team** is the email-data boundary. Contacts, segments, templates, |
| 15 | + broadcasts, sequences, transactional email, team ESPs, settings, and team |
| 16 | + API keys belong to one team. |
| 17 | +- Every team has one required `organizationId`. A team cannot belong to or be |
| 18 | + shared by multiple organizations. There is currently no public team-transfer |
| 19 | + API. |
| 20 | +- Human organization membership and human team membership are independent. |
| 21 | + Organization membership does not grant access to team data, and team |
| 22 | + membership does not grant organization administration. |
| 23 | +- Organization keys and team keys are different principals. An organization |
| 24 | + key provisions and manages resources within one organization according to |
| 25 | + its scopes. A team key accesses one fixed team's product APIs. |
| 26 | + |
| 27 | +## What provisioning does |
| 28 | + |
| 29 | +Provisioning is a server-to-server API for a multi-tenant product to create |
| 30 | +one SendLit team per external tenant inside an existing SendLit organization. |
| 31 | +The organization is derived exclusively from the organization API key; it |
| 32 | +cannot be selected in the request. |
| 33 | + |
| 34 | +```http |
| 35 | +POST /provisioning/teams |
| 36 | +Authorization: Bearer sl_org_live_... |
| 37 | +Content-Type: application/json |
| 38 | +
|
| 39 | +{ |
| 40 | + "externalId": "fl_team_...", |
| 41 | + "name": "Acme", |
| 42 | + "sender": { |
| 43 | + "fromName": "Acme", |
| 44 | + "replyTo": "hello@acme.example" |
| 45 | + }, |
| 46 | + "mailingAddress": "...", |
| 47 | + "delivery": { |
| 48 | + "useOrganizationDefault": false, |
| 49 | + "teamEspEnabled": true, |
| 50 | + "teamCanChangeDefault": true |
| 51 | + }, |
| 52 | + "quota": { |
| 53 | + "dailyLimit": 500, |
| 54 | + "monthlyLimit": 10000 |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +The call requires an organization key with `teams:provision`. It creates the |
| 60 | +team and its general settings, delivery settings, optional organization ESP |
| 61 | +grant/quota, and initial team API key. The organization always comes from the |
| 62 | +authenticated key, never from `organizationId` in a body or header. |
| 63 | + |
| 64 | +Provisioning does **not**: |
| 65 | + |
| 66 | +- create an organization; |
| 67 | +- create a Better Auth user/account; |
| 68 | +- interpret an email address as a SendLit identity; |
| 69 | +- create organization or team membership; or |
| 70 | +- give the organization key implicit access to the team's contacts or other |
| 71 | + team data. |
| 72 | + |
| 73 | +There is no deployment-wide provisioning secret or special provisioning |
| 74 | +header. The former global-secret mechanism has been removed; do not |
| 75 | +reintroduce it in code, configuration, OpenAPI, or consumer integrations. |
| 76 | + |
| 77 | +## Idempotency and key handling |
| 78 | + |
| 79 | +Idempotency is scoped by `(organizationId, externalId)`. Consequently, two |
| 80 | +organizations may use the same external ID, but one organization cannot create |
| 81 | +two provisioned teams with the same external ID. |
| 82 | + |
| 83 | +The implementation stores a SHA-256 hash of `JSON.stringify(body)` as the |
| 84 | +creation-request hash: |
| 85 | + |
| 86 | +- the first request creates the team and returns `created: true` with the only |
| 87 | + plaintext copy of the initial team API key; |
| 88 | +- an identical replay returns the existing team with `created: false` and |
| 89 | + `apiKey: null`; |
| 90 | +- a replay for the same external ID with a different creation payload returns |
| 91 | + `409 provisioning_conflict` and does not mutate the team; intentional |
| 92 | + changes must use the lifecycle `PATCH` endpoint; and |
| 93 | +- concurrent identical requests converge on one team through the database |
| 94 | + uniqueness constraint and race recovery in the query layer. |
| 95 | + |
| 96 | +Consumers must persist the initial key immediately and encrypted at rest. |
| 97 | +SendLit stores only its hash and cannot recover the plaintext. If the key is |
| 98 | +lost, `POST /provisioning/teams/:teamId/keys` creates a replacement and revokes |
| 99 | +the active keys previously created by organization-key provisioning. |
| 100 | + |
| 101 | +## Lifecycle API and scopes |
| 102 | + |
| 103 | +All provisioning lifecycle routes resolve the team inside the organization |
| 104 | +identified by the caller's key. A team from another organization is not |
| 105 | +addressable through that key. |
| 106 | + |
| 107 | +| Operation | Required scope | Behavior | |
| 108 | +| ------------------------------------------ | ----------------- | ----------------------------------------------------------------- | |
| 109 | +| `POST /provisioning/teams` | `teams:provision` | Idempotently create a team and its initial key | |
| 110 | +| `GET /provisioning/teams/:teamId` | `teams:read` | Read the provisioned team view | |
| 111 | +| `PATCH /provisioning/teams/:teamId` | `teams:manage` | Update name, sender, mailing address, delivery controls, or quota | |
| 112 | +| `POST /provisioning/teams/:teamId/keys` | `teams:keys` | Replace organization-created integration keys | |
| 113 | +| `POST /provisioning/teams/:teamId/suspend` | `teams:manage` | Stop new sends | |
| 114 | +| `POST /provisioning/teams/:teamId/resume` | `teams:manage` | Resume new sends | |
| 115 | +| `DELETE /provisioning/teams/:teamId` | `teams:manage` | Soft-archive the team | |
| 116 | +| `GET /provisioning/teams/:teamId/usage` | `usage:read` | Read daily and monthly quota windows | |
| 117 | + |
| 118 | +Provision, update, key rotation, suspension, resumption, and archival are |
| 119 | +written to the organization audit log. The `/provisioning` router is currently |
| 120 | +rate-limited to 30 requests per minute per source IP. |
| 121 | + |
| 122 | +## FrontLit integration |
| 123 | + |
| 124 | +FrontLit uses one dedicated SendLit organization and stores a scoped |
| 125 | +organization key as `SENDLIT_ORGANIZATION_API_KEY`. Every FrontLit team is |
| 126 | +eagerly mapped 1:1 to a SendLit team using the FrontLit team's public ID as |
| 127 | +`externalId`. |
| 128 | + |
| 129 | +```text |
| 130 | +FrontLit SendLit organization |
| 131 | +├── FrontLit team A -> SendLit team A -> contacts, newsletters, email data |
| 132 | +└── FrontLit team B -> SendLit team B -> contacts, newsletters, email data |
| 133 | +``` |
| 134 | + |
| 135 | +FrontLit encrypts the returned team API key in its per-team `integrations` |
| 136 | +record and uses it for all subsequent calls. Browsers never receive this key. |
| 137 | +FrontLit is a thin proxy for newsletter functionality, while SendLit remains |
| 138 | +the system of record for contacts and email data. Therefore, exposing the same |
| 139 | +underlying team in SendLit's own UI requires no contact migration or copying. |
| 140 | + |
| 141 | +FrontLit currently carries `ownerEmail` in its local provisioning retry job, |
| 142 | +but it does not send that value in the SendLit provisioning request. It has no |
| 143 | +identity or membership effect in SendLit. |
| 144 | + |
| 145 | +## Direct SendLit access for an embedded customer |
| 146 | + |
| 147 | +This user-facing handoff is **not implemented today**. Signing up for SendLit |
| 148 | +with the same email used in FrontLit does not grant access to the provisioned |
| 149 | +team. A new SendLit user receives a personal default organization, while the |
| 150 | +FrontLit-provisioned team still has no human membership. |
| 151 | + |
| 152 | +Do not solve this by adding the customer to FrontLit's SendLit organization. |
| 153 | +Any organization member can enumerate that organization's teams, which would |
| 154 | +expose other FrontLit customer workspaces. The intended boundary is direct |
| 155 | +membership in only the relevant team. |
| 156 | + |
| 157 | +The recommended future flow is: |
| 158 | + |
| 159 | +1. An authenticated FrontLit user selects **Open in SendLit**. |
| 160 | +2. FrontLit requests a short-lived, single-use invitation/handoff for the |
| 161 | + exact provisioned team using its organization credential. |
| 162 | +3. SendLit authenticates the person, preferably through **Continue with |
| 163 | + FrontLit** SSO or otherwise through a verified email flow. |
| 164 | +4. Accepting the handoff creates an `admin` (or explicitly chosen) membership |
| 165 | + in that team only. It creates no organization membership. |
| 166 | +5. SendLit selects that team and opens its dashboard. Existing contacts, |
| 167 | + broadcasts, and settings are immediately present because it is the same |
| 168 | + team, not an imported copy. |
| 169 | + |
| 170 | +The handoff must be bound to the exact team and intended identity, expire |
| 171 | +quickly, be single-use, and be audited. Same-email string matching alone is |
| 172 | +not sufficient authorization, particularly for shared addresses such as |
| 173 | +`hi@example.com`. FrontLit must never expose the stored team API key to achieve |
| 174 | +this flow. |
| 175 | + |
| 176 | +Useful API work for this flow would include team invitation creation and |
| 177 | +acceptance, team-member listing/revocation, and possibly a dedicated |
| 178 | +least-privilege organization-key scope for membership management. The schema |
| 179 | +already supports team membership, but the public team invitation/claim |
| 180 | +workflow does not yet exist. |
| 181 | + |
| 182 | +## Ownership decision |
| 183 | + |
| 184 | +The current architecture supports an **embedded/add-on** model: FrontLit owns |
| 185 | +the SendLit organization, and its customer receives access to one or more |
| 186 | +specific teams. This is the smallest and safest path to offering SendLit's |
| 187 | +advanced sequence and transactional-email UI over the customer's existing |
| 188 | +newsletter data. |
| 189 | + |
| 190 | +It does not yet support converting that workspace into an independently owned |
| 191 | +SendLit organization. If customers must retain the workspace after leaving |
| 192 | +FrontLit, the product needs an explicit, audited team-transfer capability or a |
| 193 | +different model in which each customer organization is created independently |
| 194 | +from the beginning. Provisioning alone cannot provide that ownership change. |
0 commit comments