connect.email# connect.email setup guide
This is the one-time console setup that turns connect.email from fixture mode into a live service. Do the steps in order. Nothing here is needed for local development; fixture mode runs without any of it.
What you end up with: one Google OAuth app and one Microsoft Entra app, both branded connect.email, used by every product (OpenRealtors first). People see "connect.email" on the Google and Microsoft consent screens, and the product name on connect.email's own page before that.
Redirect URIs used below (exact strings, no trailing slash):
- Google: https://connect.email/oauth/google/callback
- Google Workspace admin confirmation: https://connect.email/oauth/google/admin-confirm/callback
- Microsoft sign-in: https://connect.email/oauth/microsoft/callback
- Microsoft admin consent: https://connect.email/oauth/microsoft/admin-consent/callback
- Microsoft admin sign-in (after admin consent): https://connect.email/oauth/microsoft/admin-signin/callback
Optional staging copies use the same paths on https://staging.connect.email. Google and Microsoft do not accept wildcard redirect URIs, so per-deployment Vercel preview URLs cannot run live OAuth; use a fixed staging domain if you want one.
## 1. Domain and DNS (do this first, Google verification needs it)
1. Vercel > project connect-email > Settings > Domains > Add `connect.email`. Also add `www.connect.email` and set it to redirect to `connect.email`.
2. At the registrar for connect.email, add the records Vercel shows. Normally:
- `A` record, host `@`, value `76.76.21.21`
- `CNAME` record, host `www`, value `cname.vercel-dns.com`
- Optional staging: `CNAME` record, host `staging`, value `cname.vercel-dns.com`, then add `staging.connect.email` to the project and point it at a branch.
3. Google Search Console (https://search.google.com/search-console) > Add property > Domain > `connect.email`. Add the `TXT` record it gives you at the registrar. Google's OAuth verification checks this ownership.
4. Wait until `https://connect.email/api/health` answers 200.
## 2. Database
1. Create a Postgres database named exactly `connect_email` (the migration runner refuses any other name). Any Postgres 15+ works.
2. As the owner, create the app role:
`CREATE ROLE connect_email_app LOGIN PASSWORD '<strong password>' NOSUPERUSER NOBYPASSRLS;`
3. Run migrations as the owner from your laptop:
`MIGRATE_DATABASE_URL='postgres://<owner>:<pw>@<host>/connect_email?sslmode=require' npm run db:migrate`
4. `DATABASE_URL` for Vercel uses the app role: `postgres://connect_email_app:<pw>@<host>/connect_email?sslmode=require`. If the host has a transaction pooler, it is safe: connect.email only uses transaction-local settings.
## 3. Google Cloud project
1. https://console.cloud.google.com > project picker > New project.
- Project name: `connect-email`
- Organization: the Google Workspace organization you already use for EA AI's Google project, so the same verification contacts and billing apply.
2. APIs & Services > Library > enable **Gmail API**, **Google Calendar API** and **Admin SDK API**. Admin SDK is how connect.email checks that the person confirming a Workspace delegation is a super admin of that domain (Directory API `users.get` with `fields=isAdmin`).
## 4. OAuth consent screen (Google Auth Platform)
Console > Google Auth Platform (APIs & Services > OAuth consent screen).
1. **Branding**
- App name: `connect.email`
- User support email: a monitored address you own (for example a support alias on connect.email once mail is set up, or your ParagraphAI address)
- App logo: 120x120 PNG (required for verification)
- App home page: `https://connect.email`
- Privacy policy: `https://connect.email/privacy` (page still to be written, see "Still to build")
- Terms of service: `https://connect.email/terms` (same)
- Authorized domains: `connect.email` (and each product domain whose name appears on the consent page, for example `openrealtors.com`)
- Developer contact: your email
2. **Audience**
- User type: **External**
- While testing: Publishing status "Testing", add yourself and test accounts as test users (max 100).
3. **Data access** > Add or remove scopes. Add exactly these:
- `openid`
- `https://www.googleapis.com/auth/userinfo.email`
- `https://www.googleapis.com/auth/gmail.modify` (**restricted**)
- `https://www.googleapis.com/auth/gmail.send` (**sensitive**)
- `https://www.googleapis.com/auth/calendar.events` (**sensitive**)
- `https://www.googleapis.com/auth/admin.directory.user.readonly` (Workspace admin confirmation only; the console shows its classification when you add it)
connect.email maps them from the contract's logical scopes: `mail.read` = gmail.modify, `mail.send` = gmail.send, `calendar` = calendar.events. `admin.directory.user.readonly` is never requested from a person connecting their own mailbox: only the Workspace admin confirmation page asks for it, together with `openid email`, and connect.email revokes that token as soon as it has read the admin flag.
### Verification and the CASA security assessment
Because `gmail.modify` is a restricted scope, the app cannot go to production for the public until Google verifies it, and restricted-scope verification requires an independent security assessment (CASA, Cloud Application Security Assessment, run under the App Defense Alliance). `gmail.send` and `calendar.events` are sensitive scopes: they need verification but no assessment on their own.
What to submit (Google Auth Platform > Verification center > Prepare for verification):
- Ownership of connect.email (Search Console, step 1).
- Privacy policy that names each Google data type, what connect.email does with it, and the Google API Services User Data Policy "Limited Use" statement.
- A short unlisted YouTube video: the full connect flow from a product into connect.email, the Google consent screen with the app name and client ID visible in the URL, and each scope in use (reading mail, sending mail, a calendar event).
- One justification per scope: why gmail.modify and not gmail.readonly (marking messages read and labeling them), why send, why calendar.events, and why admin.directory.user.readonly ("before a Workspace admin's domain-wide delegation is used for a product, connect.email confirms the person approving it is a super admin of that domain by reading their own isAdmin flag once; the token is revoked immediately").
- Then the CASA assessment. Google emails a letter of assessment with the tier (restricted Gmail scopes are normally **Tier 2**). Pick an authorized lab from https://appdefensealliance.dev/casa, run their scan or questionnaire, fix findings, and the lab submits the letter of validation to Google.
Timeline and cost (plan for these, then confirm current numbers with Google and the lab):
- Brand and sensitive-scope review: a few days to a few weeks.
- CASA Tier 2: usually 2 to 6 weeks including fixes. It must be renewed every 12 months.
- Cost: Tier 2 assessments from authorized labs typically run from a few hundred to a few thousand US dollars per year. Google does not charge for verification itself.
Until verification completes, only test users can connect, and they see the "Google hasn't verified this app" screen.
Google docs:
- Restricted scope verification: https://developers.google.com/identity/protocols/oauth2/production-readiness/restricted-scope-verification
- Sensitive scope verification: https://developers.google.com/identity/protocols/oauth2/production-readiness/sensitive-scope-verification
- CASA: https://appdefensealliance.dev/casa
- User Data Policy: https://developers.google.com/terms/api-services-user-data-policy
## 5. Google OAuth client
Google Auth Platform > Clients > Create client.
- Application type: **Web application**
- Name: `connect.email web`
- Authorized JavaScript origins: none needed
- Authorized redirect URIs:
- `https://connect.email/oauth/google/callback`
- `https://connect.email/oauth/google/admin-confirm/callback`
- optional: the same two paths on `https://staging.connect.email`
Copy the client ID and secret into Vercel as `GOOGLE_CLIENT_ID` and `GOOGLE_CLIENT_SECRET` (step 9).
## 6. Service account for Workspace domain-wide delegation (optional at launch)
This lets a Google Workspace admin grant connect.email access to mailboxes in their domain without each person signing in. User sign-in works without it; the delegation endpoints answer 503 `delegation_not_configured` until it is set.
1. IAM & Admin > Service accounts > Create service account.
- Name: `connect-email-dwd`
- Grant no project roles. It needs none.
2. Open it and note the **Unique ID** (a long number, also called the OAuth 2 client ID). This is the "client ID" a Workspace admin pastes into their Admin Console. connect.email returns it automatically in `POST /v1/workspace-delegations`.
3. Keys > Add key > Create new key > JSON. Store the whole JSON in Vercel as `GOOGLE_SERVICE_ACCOUNT_JSON` (Sensitive). Delete the downloaded file afterwards.
- If the organization policy `iam.disableServiceAccountKeyCreation` blocks this, override it for the connect-email project only (IAM & Admin > Organization policies).
- A keyless setup (IAM Credentials `signJwt` through Workload Identity Federation with Vercel's OIDC token) avoids storing a key, but it is not built yet; see "Still to build".
4. Each customer Workspace admin then does two things (the product walks them through both):
- Opens the `admin_confirm_url` connect.email returns for their delegation and presses **Confirm with Google**, signing in as the admin address the product registered. connect.email requires the id_token's email to match, `email_verified`, `hd` equal to the domain, and the Directory API to answer `isAdmin: true`. Until then `verify` answers 409 `admin_not_confirmed`. This is what stops a second project from riding on a delegation the domain granted to someone else: the Admin Console grant is for one global client ID, so it cannot say which project asked.
- In their own Admin Console (connect.email gives them the values): admin.google.com > Security > Access and data control > API controls > Domain-wide delegation > Add new > client ID = the Unique ID, OAuth scopes = `https://www.googleapis.com/auth/gmail.modify,https://www.googleapis.com/auth/gmail.send,https://www.googleapis.com/auth/calendar.events`.
Delegation uses the same restricted Gmail scope, so it is covered by the same verification.
## 7. Microsoft Entra app registration
Sign in to https://entra.microsoft.com with an account in the tenant that should own the app (the ParagraphAI tenant that owns EA AI's verified app is the natural home, since publisher verification is per Partner Center account).
1. Identity > Applications > App registrations > New registration.
- Name: `connect.email`
- Supported account types: **Accounts in any organizational directory (multitenant) and personal Microsoft accounts** (needed for outlook.com and hotmail.com users)
- Redirect URI: platform **Web**, `https://connect.email/oauth/microsoft/callback`
2. Authentication > Web > Add URI: `https://connect.email/oauth/microsoft/admin-consent/callback` and `https://connect.email/oauth/microsoft/admin-signin/callback` (and the staging copies if you use staging). Leave implicit grant off.
3. API permissions > Add a permission > Microsoft Graph:
- **Delegated**: `offline_access`, `User.Read`, `Mail.ReadWrite`, `Mail.Send`, `Calendars.ReadWrite`, plus `openid`, `profile`, `email` (required: the admin sign-in after admin consent uses exactly these three)
- **Application**: `Mail.ReadWrite`, `Mail.Send`, `Calendars.ReadWrite`
- Do not click "Grant admin consent" for your own tenant unless you want to test against it. Customer admins grant through connect.email's admin consent link. That link uses `https://graph.microsoft.com/.default`, which grants exactly what is configured here, so keep this list in sync with the code.
4. **Directory roles in the id_token (required for admin consent).** Microsoft's admin consent callback only reports a tenant id in the browser's query string, which anyone can type, so connect.email signs the admin in afterwards and requires the id_token's `wids` claim to hold an admin role (Global Administrator, Privileged Role Administrator, Cloud Application Administrator or Application Administrator). Entra only puts `wids` in tokens when the app asks for directory roles. Either:
- Token configuration > Add groups claim > tick **Directory roles** > under ID, leave the default > Add. Or
- Manifest > set `"groupMembershipClaims": "DirectoryRole"` > Save. (If the app already uses group claims, `"All"` also includes directory roles.)
Without it, every admin sees "Microsoft did not send your directory roles" and nothing is approved. To check: approve a test tenant with a Global Administrator; the audit log row `entra_consent.granted` carries `role: "Global Administrator"`.
5. Certificates & secrets > New client secret. Description `vercel`, expiry 24 months. Copy the **Value** (not the ID) into Vercel as `MICROSOFT_CLIENT_SECRET`, and put a calendar reminder a month before it expires. Overview > Application (client) ID goes in `MICROSOFT_CLIENT_ID`.
6. Branding & properties:
- Home page URL `https://connect.email`, terms and privacy URLs as in step 4.
- Publisher domain: `connect.email`. Verify it with the file connect.email already serves at `https://connect.email/.well-known/microsoft-identity-association.json` (it appears once `MICROSOFT_CLIENT_ID` is set in Vercel). Alternatively add connect.email as a custom domain in the tenant with a DNS TXT record.
7. **Publisher verification**: Branding & properties > Publisher verification > Add MPN ID. It needs a Microsoft Partner Network ID (now the Partner One ID in Partner Center) from a verified Partner Center account, and your sign-in must have the right Partner Center role. Reuse ParagraphAI's Partner Center account if EA AI's app is already verified under it. Without publisher verification, users in most work tenants cannot consent to a multitenant app and see "unverified".
Note for customer IT admins: application permissions reach every mailbox in their tenant. They can limit connect.email to a group with an Exchange Online application access policy (`New-ApplicationAccessPolicy`) or RBAC for Applications. connect.email checks mailbox access when a mailbox is added and refuses it if the policy blocks it.
## 8. Product project and keys
Each product gets a project with its own key and signing secret. The script seals the signing secret with `CONNECT_EMAIL_ENCRYPTION_KEY`, so it needs the same key as production.
The key the build put in Vercel is a write-only Secret: nobody can read it back, so you cannot run the script with it. Before the first project exists (nothing is encrypted yet, so this loses nothing), replace it with one you keep:
openssl rand -base64 32 # save the output in your password manager as "connect.email encryption key k1"
npx vercel@latest env add CONNECT_EMAIL_ENCRYPTION_KEY production --sensitive --force
Losing this key means every stored refresh token becomes unreadable and every account has to reconnect, so keep the saved copy.
Then, with the production `DATABASE_URL` (owner or app role) and that key exported in your shell:
npm run project:create -- --slug openrealtors --name OpenRealtors --mode live \
--redirect https://openrealtors.com/api/connect-email/callback \
--webhook https://openrealtors.com/api/webhooks/connect-email
It prints the API key and signing secret once. Put them in the product's Vercel env (`CONNECT_EMAIL_API_KEY`, `CONNECT_EMAIL_SIGNING_SECRET`, `CONNECT_EMAIL_BASE_URL=https://connect.email`). Use the real redirect and webhook paths the product implements.
## 9. Vercel environment variables (project connect-email, Production)
Already set by the build (Secret type): `CONNECT_EMAIL_ENCRYPTION_KEY` (replace it as described in step 8), `CRON_SECRET`.
Set these yourself:
| Variable | Value |
| --- | --- |
| `CONNECT_EMAIL_PROVIDER_MODE` | `live` |
| `CONNECT_EMAIL_BASE_URL` | `https://connect.email` |
| `DATABASE_URL` | app-role URL from step 2 (Sensitive) |
| `GOOGLE_CLIENT_ID` | step 5 |
| `GOOGLE_CLIENT_SECRET` | step 5 (Sensitive) |
| `MICROSOFT_CLIENT_ID` | step 7 |
| `MICROSOFT_CLIENT_SECRET` | step 7 (Sensitive) |
| `GOOGLE_SERVICE_ACCOUNT_JSON` | step 6, optional (Sensitive) |
| `CONNECT_EMAIL_CRONS_ENABLED` | `1` once the database is live (webhook retries) |
Optional: `CONNECT_EMAIL_ENCRYPTION_KEY_ID` (default `k1`), `CONNECT_EMAIL_ENCRYPTION_KEYS_OLD` (rotation, format `k1:<base64>,k0:<base64>`), `GOOGLE_DELEGATION_CLIENT_ID` (override), `CONNECT_EMAIL_DB_POOL_MAX` (default 5).
Never set `CONNECT_EMAIL_PROVIDER_MODE=fixture` in Vercel. It is ignored there anyway: any Vercel environment refuses fixture mode and every API route answers 503.
Redeploy after setting variables. `https://connect.email/api/health` should then show `"mode":"live","ready":true`. If anything is missing it lists the variable names.
## 10. Smoke test with your own accounts
1. Create a `test` mode key for yourself with `npm run project:create -- ... --mode test` (or use a live key on a throwaway project).
2. `POST /v1/connect-sessions` with your Gmail address as `login_hint`, open the returned URL, finish Google sign-in, and check the redirect has `status=connected` and a valid `sig`.
3. `POST /v1/connections/<id>/access-token` and call `https://gmail.googleapis.com/gmail/v1/users/me/profile` with the token.
4. Repeat with an Outlook or Microsoft 365 account against `https://graph.microsoft.com/v1.0/me`.
5. `DELETE /v1/connections/<id>` and confirm the webhook arrived.
6. Microsoft admin consent: `POST /v1/entra-consents`, open the URL as a Global Administrator of a test tenant, accept, then sign in when connect.email sends you to Microsoft again. The redirect has `status=granted`. Then try once with a non-admin user of the same tenant: you get "This account is not a Microsoft 365 admin" and `GET /v1/entra-consents/<id>` stays `pending`.
7. Workspace delegation: `POST /v1/workspace-delegations` for a test domain, open `admin_confirm_url`, press Confirm with Google as the super admin, then add the client ID in Admin Console and `POST .../verify`. Before the confirmation, verify answers 409 `admin_not_confirmed`.
## Still to build before public launch
- `/privacy` and `/terms` pages (Google and Microsoft both require them; write them with the Limited Use statement).
- Keyless service account signing (Workload Identity Federation) if you do not want a JSON key in Vercel.
- An admin screen for projects and keys. Today they are created with `npm run project:create`.