# Single sign-on

Connect your organization to one identity provider over SAML 2.0 or OpenID Connect, with step-by-step setup for Okta and Microsoft Entra ID, requiring single sign-on, certificate rotation and troubleshooting.

Single sign-on lets the people in your organization sign in to Cohorly through
your identity provider, such as Okta, Microsoft Entra ID or Google Workspace,
instead of a Cohorly password.

## What it does

- An owner connects the organization to **one** identity provider, over
  **SAML 2.0** or **OpenID Connect**.
- People whose e-mail address is on one of your **verified** domains sign in
  through that provider. On the sign-in screen they enter their e-mail and
  choose **Continue with SSO**. Someone signing in for the first time joins the
  organization as a member.
- You prove you control each domain with a DNS record before it signs anyone
  in (see [Verifying your domains](#verifying-your-domains)).
- An owner can **require** single sign-on, so that members sign in only through
  the provider.
- Owners keep every sign-in method (password and Google), even when single
  sign-on is required, so a broken identity provider never locks the
  organization out.

On Cohorly Cloud, setting up or changing single sign-on needs a plan that
includes it (a self-hosted server has every feature). If the organization later
moves to a plan without it, people keep signing in through the provider already
set up; an owner can still remove it.

## Before you start

- You need the **owner** role in the organization. Members can see the
  settings but not change them.
- If you sign in with a password, turn on **two-factor authentication** for your
  own account first (**Settings > Account**). Saving the provider is refused
  until you do. This is an extra step, not a guarantee: it makes whoever saves
  the provider pass a second factor in their session, and does not replace
  protecting owners' passwords. Owners who sign in only with Google are not
  asked.
- Decide which **e-mail domains** sign in through the provider: 1 to 20 per
  organization, like `example.com`. Domains of large public webmail providers
  are refused. You also need access to the DNS settings of each domain, to
  publish the record that verifies it. A domain can be verified by only one
  organization.
- The settings live in **Settings > Account > Single sign-on**. Choose the
  protocol your provider uses and select **Set up**. The panel that opens shows
  the values to register at your provider first, then the fields for your
  provider's details.

In the examples below, `https://cohorly.example.com` stands for the address of
your Cohorly dashboard. Copy the real values from the settings panel: each
organization has its own.

## SAML 2.0

Cohorly is the service provider. In the settings panel, under
**1. Add Cohorly to your identity provider**, you find:

| Value                 | What it is                                                                                                             |
| --------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| Entity ID (audience)  | Cohorly's identifier for your organization, like `https://cohorly.example.com/api/auth/sso/saml/metadata/42`.          |
| Reply URL (ACS)       | Where your provider sends people back after they sign in, like `https://cohorly.example.com/api/auth/sso/saml/acs/42`. |
| Download metadata XML | The same two values as a metadata file (`cohorly-saml-metadata.xml`), for providers that can import one.               |

Under **2. Connect your identity provider**, you then paste your provider's
metadata XML (**Paste metadata**), or enter its sign-in URL, entity ID and
signing certificate (**Enter manually**).

### Okta

1. In the Okta Admin Console, go to **Applications > Applications** and select
   **Create App Integration**. Choose **SAML 2.0**.
2. Give the app a name, like "Cohorly", and continue.
3. In **Configure SAML**:
   - **Single sign-on URL**: Cohorly's **Reply URL (ACS)**.
   - **Audience URI (SP Entity ID)**: Cohorly's **Entity ID (audience)**.
   - **Name ID format**: `EmailAddress`.
   - **Application username**: `Email`.
4. Finish the wizard.
5. In the app's **Assignments** tab, assign the people or groups who should
   sign in to Cohorly.
6. In the app's **Sign On** tab, copy the identity provider metadata (the XML
   behind the metadata link or in the SAML setup instructions).
7. In Cohorly, keep **Paste metadata** selected, paste the XML, list your
   e-mail domains and select **Save**.

### Microsoft Entra ID

1. In the Microsoft Entra admin center, go to **Enterprise applications** and
   select **New application**, then **Create your own application**. Choose the
   option to integrate an application you don't find in the gallery
   (non-gallery), and name it, like "Cohorly".
2. In the new application, open **Single sign-on** and choose **SAML**.
3. In **Basic SAML Configuration**, select **Edit** and set:

   - **Identifier (Entity ID)**: Cohorly's **Entity ID (audience)**.
   - **Reply URL (Assertion Consumer Service URL)**: Cohorly's
     **Reply URL (ACS)**.

   Or select **Upload metadata file** and upload the file from Cohorly's
   **Download metadata XML**.
4. Keep the default **Attributes & Claims**: the `emailaddress` claim comes from
   `user.mail`, which is what Cohorly reads.
5. In **SAML Certificates**, download **Federation Metadata XML**.
6. In **Users and groups**, assign the people or groups who should sign in to
   Cohorly.
7. In Cohorly, keep **Paste metadata** selected, paste the contents of the
   Federation Metadata XML file, list your e-mail domains and select **Save**.

### What Cohorly reads from SAML

- **The e-mail address** comes from the `NameID` when its format is
  `emailAddress`. Otherwise Cohorly reads the `email` attribute, then
  `http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress`. A
  response without an e-mail address, or with one outside your domains, does
  not sign anyone in.
- **The metadata** needs one identity provider entry with a
  `SingleSignOnService` that uses the HTTP-Redirect binding and a signing
  certificate. Cohorly reads the sign-in URL, the entity ID and the
  certificate from it.
- **Metadata is pasted, never fetched by URL.** When your provider's metadata
  changes, paste it again.
- **Manual entry** takes the same three values: the sign-in URL (it must start
  with `https`), the provider's entity ID, and one X.509 certificate in PEM or
  base64.
- Sign-in can also start at the provider (an Okta tile or the Microsoft My Apps
  portal), as well as on Cohorly's sign-in screen.

## OpenID Connect

Cohorly is an OpenID Connect client of your provider. You need:

- **Issuer URL**: your provider's issuer. It must serve
  `/.well-known/openid-configuration`, which Cohorly reads before it saves.
- **Client ID** and **client secret** of the app you create at the provider.
  The secret is stored encrypted and is never shown again. When you edit the
  provider later, leave the secret empty to keep it.
- **Redirect URI**: shown in the settings panel, like
  `https://cohorly.example.com/api/auth/sso/callback`. Register it at your
  provider.

Notes per provider:

- **Okta**: create an app integration with **OIDC - OpenID Connect** and the
  **Web Application** type. Add the redirect URI as a sign-in redirect URI. The
  issuer is your Okta domain, or the issuer of the authorization server you
  use.
- **Microsoft Entra ID**: create an **App registration** with a **Web**
  redirect URI, then create a client secret in **Certificates & secrets**. The
  issuer is `https://login.microsoftonline.com/<tenant-id>/v2.0`.
- **Google Workspace**: create an OAuth client ID of type **Web application** in
  the Google Cloud console and add the redirect URI. The issuer is
  `https://accounts.google.com`.

## Verifying your domains

Listing a domain does not make it yours. Until you prove you control it, a
domain is **Pending**: it does not route anyone to your identity provider, does
not let your provider create accounts, and does not stop another organization
from using the same name. Each domain in **Settings > Account > Single
sign-on** shows its state:

| State                 | Meaning                                                                                                                                              |
| --------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Pending**           | Added, not verified yet. People with an e-mail on it keep signing in as before.                                                                      |
| **Verified**          | Control confirmed through DNS. People with an e-mail on it sign in through your provider.                                                            |
| **Verified (legacy)** | The domain was saved before domain verification existed, so it stayed verified. It works as before, and you can verify it through DNS to confirm it. |

To verify a domain:

1. Save the domain. It appears as **Pending**, with the DNS record to publish.
2. At your DNS provider, add a **TXT** record with the **Name** and **Value**
   shown, using the copy buttons. The record looks like this, with your domain
   in place of `<domain>`:

   | Type  | Name                    | Value                                 |
   | ----- | ----------------------- | ------------------------------------- |
   | `TXT` | `_cohorly-sso.<domain>` | `cohorly-domain-verification=<token>` |

   The name is `_cohorly-sso.` followed by the domain, not the domain itself.
   The token is specific to your organization and the domain, and it stays the
   same until you remove the domain from the list. Some DNS providers add the
   domain for you, so enter only `_cohorly-sso` in their name field.
3. Select **Verify** next to the domain. Cohorly looks up the record and marks
   the domain **Verified** when its value matches exactly.

DNS can take a few minutes to update. If Cohorly does not find the record yet,
select **Verify** again later. A failed check changes nothing: a pending domain
stays pending, and a verified one stays verified. You can leave the record in
place; Cohorly does not check it again after the domain is verified.

If another organization has already verified the same domain, **Verify** is
refused. Only one organization can hold a verified domain, and the first one to
verify it keeps it.

Only owners can verify a domain. Members see each domain's state, without the
record or the button.

## People who already have an account

Someone whose e-mail is already a Cohorly account **in your organization** is
linked to the provider the first time they sign in through it, and gets an
e-mail saying so. If this was not them, they should contact an owner and change
their password.

An account with **two-factor authentication** turned on is never linked this
way, whatever its role: an identity provider's word that an e-mail address
belongs to someone is not enough to get past that person's password and second
factor. They see "An account with this e-mail already exists" and link the
provider themselves:

1. Sign in to Cohorly with the password and the authenticator code.
2. Open **Settings > Account > Single sign-on** and select **Link single
   sign-on**.
3. Sign in at your identity provider. When you come back, the provider is linked
   to the account and you can use it from then on.

The provider must return the same account that started the link, or nothing is
linked. If it fails, you come back to Settings with a message saying so.

Accounts **without** two-factor authentication, owners included, are still linked
automatically when they are in your organization, and get the e-mail above.
Turning on two-factor authentication is what protects an account from this.

With SAML, changing the **certificate** or the **Entity ID** of the provider
undoes the SAML links of accounts that have two-factor authentication on, and
so does rotating the certificate. Those people link again from their own
session, as above. Changing only the domains keeps every link.

A member with two-factor turned on, in an organization that requires single
sign-on, cannot sign in with a password and is not linked automatically. Ask an
owner to reset their two-factor authentication (**Reset two-factor**, in the
members list in Settings); their
next sign-in through the provider then links the account.

## Requiring single sign-on

Once a provider is saved and **at least one of its domains is verified**, turn
on **Require for every member** in the same section. Members then sign in only
through your identity provider; a password sign-in is refused with "Your
organization requires single sign-on." Members who were signed in are signed
out. Owners can still sign in with a password or Google.

With no verified domain, Cohorly refuses to turn it on: members would be left
without a password and without single sign-on. A **Verified (legacy)** domain
counts. For the same reason, while single sign-on is required, saving a domain
list that keeps no verified domain is refused.

Turning it off is always possible. Removing the provider turns the requirement
off too.

## Switching protocols

An organization has one identity provider. To move from OpenID Connect to SAML
2.0, or back, select **Edit**, choose the other protocol and fill it in. Cohorly
asks you to confirm before it replaces the saved provider. People sign in
through the new provider from their next sign-in. Your e-mail domains and the
single sign-on requirement stay as they are.

## Certificate rotation (SAML)

The summary in **Settings > Account > Single sign-on** shows when your
provider's signing certificate expires, and warns you 30 days before.

Sign-in keeps working after that date, until your identity provider starts
signing with a new certificate. When it does, paste the provider's new
metadata in Cohorly (**Edit**, then **Paste metadata**).

Cohorly does not keep a copy of the certificate you can reuse: every SAML save
needs the metadata, or all three manual values, again.

## Troubleshooting

These are the messages people may see on the sign-in screen:

| Message                                                                                                                                                | What to check                                                                                                                                                                                                                                                                                                  |
| ------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Single sign-on did not complete. Try again.                                                                                                            | The sign-in at the provider failed or was cancelled, or the SAML response could not be validated: check the Entity ID and Reply URL registered at the provider, that the metadata pasted in Cohorly is current, and that the response carries an e-mail address.                                               |
| Your identity provider signed you in with an e-mail outside your organization's domains.                                                               | The provider sent an e-mail on a domain that is not listed, or is listed but still **Pending**. Add the domain and verify it, or fix the e-mail the provider sends.                                                                                                                                            |
| An account with this e-mail already exists. If it is yours, sign in with your password and link single sign-on in Settings. Otherwise, ask your admin. | The e-mail already belongs to an account that single sign-on cannot link by itself: it is in another organization (single sign-on never moves an account between organizations), or it has two-factor authentication turned on. See [People who already have an account](#people-who-already-have-an-account). |
| Single sign-on is not set up correctly for your organization. Ask your admin to check it.                                                              | The saved provider cannot be used, for example the identity provider refuses the OpenID Connect client ID and secret. An owner should check the app at the provider, then edit and save it again.                                                                                                              |
| Your organization requires single sign-on. Continue with SSO instead.                                                                                  | A member tried to sign in with a password while single sign-on is required.                                                                                                                                                                                                                                    |
