# Google and Microsoft OAuth

Register Google and Microsoft OAuth apps for sign-in and mailbox connections on your own instance.

Self-hosted instances use their own OAuth apps. One app per provider covers **sign-in**, **mailbox connections** and members' own **[mail accounts](#mail-accounts)**. Without them, password sign-in, forwarding, SMTP and IMAP mailboxes, and IMAP mail accounts still work.

Gmail mailbox connections can also use a separate Google client, for example in its own Google Cloud project while its Gmail scope is reviewed: set `GOOGLE_MAILBOX_CLIENT_ID` and `GOOGLE_MAILBOX_CLIENT_SECRET`, and register only the mailbox redirect URI on it. Microsoft mailbox connections can likewise use their own Entra app with `MICROSOFT_MAILBOX_CLIENT_ID` and `MICROSOFT_MAILBOX_CLIENT_SECRET`; Microsoft sign-in then stays off unless `MICROSOFT_CLIENT_ID` is set too.

## Redirect URIs

Register these, with `API_URL` exactly as configured (on a single domain, your site URL):

```
API_URL/v1/auth/oauth/{provider}/callback              sign-in
API_URL/v1/mailboxes/oauth/{provider}/callback         mailbox connections
API_URL/v1/collab/accounts/oauth/{provider}/callback   mail accounts
```

For `API_URL=https://desk.example.com`:

| Provider | Redirect URIs |
| --- | --- |
| Google | `https://desk.example.com/v1/auth/oauth/google/callback`, `https://desk.example.com/v1/mailboxes/oauth/google/callback`, `https://desk.example.com/v1/collab/accounts/oauth/google/callback` |
| Microsoft | `https://desk.example.com/v1/auth/oauth/microsoft/callback`, `https://desk.example.com/v1/mailboxes/oauth/microsoft/callback`, `https://desk.example.com/v1/collab/accounts/oauth/microsoft/callback` |

Leave out the mail accounts URI if no one on your instance connects their own Google or Microsoft account.

## Scopes

A mailbox connection sends replies from the mailbox, and its mail comes in by forwarding. With [receiving over IMAP](/self-hosting/configuration/#receiving-over-imap) on (`MAILBOX_IMAP`, the default), a connection can instead sync the mailbox, which asks for more.

| Provider | Sign-in | Mailbox, send-only | Mailbox, sync (IMAP) |
| --- | --- | --- | --- |
| Google | `openid`, `email`, `profile` | `openid`, `email`, `https://www.googleapis.com/auth/gmail.send` | `openid`, `email`, `https://mail.google.com/` |
| Microsoft | `openid`, `email`, `profile` | `openid`, `email`, `offline_access`, `https://graph.microsoft.com/Mail.Send`, `https://graph.microsoft.com/User.Read` | the same, plus `https://outlook.office.com/IMAP.AccessAsUser.All` |

Mailbox connections use PKCE and request offline access, so opendesk keeps working after the person who connected closes the browser. If no one on your instance syncs mailboxes, register only the send-only scopes.

## Google

1. Open the [Google Cloud console](https://console.cloud.google.com) and create (or pick) a project.

2. Go to **APIs & Services → Library**, search for **Gmail API** and click **Enable**. (Only needed for mailbox connections: replies are sent through it.)

3. Go to **Google Auth Platform → Branding** (formerly **OAuth consent screen**). Enter the app name, support email, your domain under **Authorized domains**, and links to your privacy policy and terms.

4. Under **Audience**, choose **Internal** if everyone who connects a mailbox is in your Google Workspace organization. Choose **External** otherwise.

5. Under **Data Access**, add the scopes `openid`, `.../auth/userinfo.email`, `.../auth/userinfo.profile` and, for mailboxes, `.../auth/gmail.send`. Add `https://mail.google.com/` only if mailboxes will sync over IMAP, and `.../auth/gmail.modify` and `.../auth/gmail.settings.basic` only for [mail accounts](#mail-accounts).

6. Go to **Clients → Create client**. Choose **Web application**, give it a name, and add the Google redirect URIs above under **Authorized redirect URIs**. Click **Create**.

7. Copy the client id and secret into `.env` and restart opendesk:

   ```sh
   GOOGLE_CLIENT_ID=1234567890-abc.apps.googleusercontent.com
   GOOGLE_CLIENT_SECRET=GOCSPX-...
   ```

<Aside type="note" title="Google verification">
`gmail.send` is a **sensitive** scope. An **Internal** app in your own Workspace can use it immediately. An **External** app in production needs Google's app verification once before people outside your test users can connect.

`https://mail.google.com/`, for syncing, is a **restricted** scope. An **External** app in production needs Google's verification and a yearly security assessment (CASA) for it. An **Internal** app can use it immediately.

While an app is in **Testing**, add each Google account under **Audience → Test users**; refresh tokens for test apps expire after 7 days.
</Aside>

## Microsoft

1. Open the [Microsoft Entra admin center](https://entra.microsoft.com) → **Identity → Applications → App registrations → New registration**.

2. Enter a name. Under **Supported account types**, choose who may sign in:
   - **Accounts in this organizational directory only** for one tenant (set `MICROSOFT_TENANT` to your tenant id)
   - **Accounts in any organizational directory and personal Microsoft accounts** for everyone (keep `MICROSOFT_TENANT=common`)

3. Under **Redirect URI**, choose **Web** and enter the sign-in redirect URI. Click **Register**. Then open **Authentication** and add the mailbox redirect URI, and the mail accounts one if you use it, as further **Web** URIs.

4. Open **Certificates & secrets → New client secret**. Copy the secret **value** (not its id) now; it is shown once. Note its expiry and rotate it before then.

5. Open **API permissions → Add a permission**:
   - **Microsoft Graph → Delegated**: `openid`, `email`, `profile`, `offline_access`, `User.Read`, `Mail.Send`
   - Only if mailboxes will sync over IMAP: **APIs my organization uses →** search **Office 365 Exchange Online → Delegated**: `IMAP.AccessAsUser.All`
   - Only for [mail accounts](#mail-accounts): **Microsoft Graph → Delegated**: `Mail.ReadWrite`, `MailboxSettings.ReadWrite`

   For a single organization, click **Grant admin consent** so members are not asked individually.

6. Copy the **Application (client) ID** from **Overview** and the secret into `.env`, then restart opendesk:

   ```sh
   MICROSOFT_CLIENT_ID=00000000-0000-0000-0000-000000000000
   MICROSOFT_CLIENT_SECRET=...
   MICROSOFT_TENANT=common
   ```

A Microsoft app used by people outside your organization should have a verified publisher: under **Branding & properties**, set the publisher domain and link a Microsoft partner account, so the consent screen shows your name as verified.

Mailboxes that sync through Microsoft read mail over IMAP, so IMAP must be enabled for each of them in Microsoft 365. See [Microsoft 365 and Outlook](/guides/mailboxes/microsoft/#imap-must-be-enabled).

## Mail accounts

[Collab](/guides/collab/connect-an-account/) lets members connect their own Google and Microsoft accounts, private or shared with the team. **Connect a mail account** offers **Google** and **Microsoft** only once the instance has a client for them; until then it offers **Other (IMAP)** alone (while `COLLAB_IMAP` is on).

Mail accounts use the mailbox clients (`GOOGLE_MAILBOX_CLIENT_ID`, `MICROSOFT_MAILBOX_CLIENT_ID`, which default to the sign-in clients) unless you give them their own, for example a Google project of its own while its restricted scope is reviewed:

```sh
COLLAB_GOOGLE_CLIENT_ID=1234567890-def.apps.googleusercontent.com
COLLAB_GOOGLE_CLIENT_SECRET=...
COLLAB_MICROSOFT_CLIENT_ID=00000000-0000-0000-0000-000000000000
COLLAB_MICROSOFT_CLIENT_SECRET=...
```

Whichever client they use needs the mail accounts redirect URI above and these scopes:

| Provider | Mail accounts |
| --- | --- |
| Google | `openid`, `email`, `profile`, `https://www.googleapis.com/auth/gmail.modify`, `https://www.googleapis.com/auth/gmail.settings.basic`, with the **Gmail API** enabled |
| Microsoft | `openid`, `email`, `offline_access`, `https://graph.microsoft.com/Mail.ReadWrite`, `https://graph.microsoft.com/Mail.Send`, `https://graph.microsoft.com/User.Read`, `https://graph.microsoft.com/MailboxSettings.ReadWrite` |

`gmail.settings.basic` and `MailboxSettings.ReadWrite` keep each account's signature, From name and automatic replies in sync; an account connected without them still syncs its mail. When the Collab clients differ from the mailbox clients, also register the mailbox redirect URI on them: support mailboxes that read their inbox directly sign in with the Collab clients.

<Aside type="note" title="Google verification">
`gmail.modify` is a **restricted** scope and `gmail.settings.basic` a **sensitive** one. An **Internal** app in your own Workspace can use them immediately. An **External** app in production needs Google's verification, and a yearly security assessment for the restricted scope, before people outside your test users can connect.
</Aside>

See [Collab](/self-hosting/configuration/#collab-your-own-mail-accounts) for `COLLAB` and `COLLAB_IMAP`.

## GitHub, X and OIDC sign-in

These are for sign-in only.

- **GitHub:** create an OAuth app under **Settings → Developer settings → OAuth Apps**. Use `API_URL/v1/auth/oauth/github/callback` as the callback and enter its client ID and secret in **Platform settings → Third-party sign-in**. The deployment variables are `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET`. It requests `read:user` and `user:email`.
- **X:** create a confidential web app with OAuth 2.0 and access to the user's confirmed email. Copy the callback from **Platform settings → Third-party sign-in** into the X Developer Console, then enter the client ID and secret in the panel. [X OAuth 2.0](https://docs.x.com/fundamentals/authentication/oauth-2-0/authorization-code)
- **OIDC:** register a web client with your identity provider with the redirect URI `API_URL/v1/auth/oauth/oidc/callback` and scopes `openid email profile`, then set `OIDC_ISSUER` (the issuer your provider advertises; a trailing slash may be left on or off), `OIDC_CLIENT_ID`, `OIDC_CLIENT_SECRET` and optionally `OIDC_NAME` for the button label.

Federated identities are bound by issuer and subject. If someone signs in with a provider using the email of an existing password account, the accounts are linked only after the password is entered.
