# Single sign-on (SSO) for organizations

Source: [Single sign-on (SSO) for organizations](<https://docs.gptunnel.ru/en/business/sso/>)

<a id="_top"></a>
With single sign-on (SSO), employees sign in to GPTunneL with their work email through your corporate identity provider (IdP). They use OpenID Connect (OIDC) and do not need a separate password. GPTunneL does not support SAML.

You can use an OIDC provider such as Keycloak, ADFS, Microsoft Entra ID, Yandex ID for Business, or Okta.

<a id="before-you-start"></a>
## Before you start

- Get administrator access to your IdP to register an application.
- Get access to DNS for your email domain.
- Sign in as an owner or administrator of the GPTunneL organization.
- Ask your GPTunneL account manager to enable SSO for the organization. The **SSO** tab appears in the business dashboard when the feature is enabled.

<a id="step-1-verify-the-domain"></a>
## Step 1. Verify the domain

1. In the business dashboard, open **SSO** and find **Email domains**. Add your domain, such as `company.ru`.
2. Add the DNS TXT record shown in the dashboard. 
   
   - Name: `_gptunnel-sso.company.ru`
   - Value: `gptunnel-sso-verify=<token>`
3. Select **Verify**. The domain can be verified once the record is visible in DNS.

Only employees with an email address on a verified domain can use SSO. An organization can verify several domains. Each domain can be verified by only one organization.

<a id="step-2-register-an-application-with-the-idp"></a>
## Step 2. Register an application with the IdP

Create a **confidential** client in your IdP that uses the Authorization Code flow.

| Setting | Value |
| --- | --- |
| Redirect URI | Copy the value shown under **Provider** in the dashboard, such as `https://gptunnel.ru/`. |
| Scopes | `openid email profile` |
| PKCE | Enabled, with the `S256` method. |

Copy the redirect URI exactly. Use the root URL employees use to open GPTunneL, including the trailing slash. Leave out `/callback` and any other path. The IdP checks for an exact match. If employees use several addresses, register each one.

The IdP must return these claims:

- `email` with the employee’s email address. If your IdP uses a different claim such as `upn`, specify it in step 3.
- `email_verified` set to `true` or omitted. A value of `false` rejects sign-in.
- `sub`, a stable identifier for the employee.

GPTunneL checks the ID token signature against the keys at `jwks_uri`. Supported algorithms are RS, PS, and ES (256, 384, and 512). The IdP endpoints must be publicly reachable over HTTPS.

If the email claim is absent from the ID token, GPTunneL reads it from the UserInfo endpoint. This covers IdPs such as ADFS that may return email only through UserInfo.

<a id="step-3-complete-the-provider-settings"></a>
## Step 3. Complete the provider settings

Fill in the **Provider** section of the business dashboard.

| Field | What to enter |
| --- | --- |
| Issuer | The issuer URL, which must match `iss` in the token. |
| Client ID | Your application’s identifier. |
| Client Secret | Your application’s secret. It is encrypted at rest and shown only when entered. |
| Discovery URL | Set this only if your metadata URL differs from the default, `<Issuer>/.well-known/openid-configuration`. |
| Scopes | Defaults to `openid email profile`. |
| Email claim | The claim containing the email address. Defaults to `email`. |
| PKCE | Enabled by default. |

Select **Test connection**. GPTunneL fetches the IdP metadata and reports an error if it is unavailable or invalid.

<a id="step-4-map-idp-roles-optional"></a>
## Step 4. Map IdP roles (optional)

When you configure a claim under **Provider roles**, GPTunneL updates employee roles at each sign-in.

- **Roles claim** is the dot-separated path to the list of roles in the token. For example, Keycloak can use `resource_access.<client_id>.roles`. In Keycloak, enable **Add to ID token** in the role mapper.
- **Administrator roles** grant an employee the organization administrator role. If the mapped role is removed, an administrator becomes an employee on their next sign-in. Organization owners keep their role.
- **Access roles** map IdP roles to roles that control model access in GPTunneL.

Role changes take effect at the next sign-in. If the claim is configured but missing from the token, GPTunneL keeps the existing roles.

If you leave the role claim unconfigured, employees get the **Employee** role. You can promote them manually in the dashboard.

<a id="step-5-turn-on-and-test-sso"></a>
## Step 5. Turn on and test SSO

1. Turn on **Enable SSO**. The switch is available when you have verified a domain, set the Issuer, Client ID, and Client Secret, and the IdP responds.
2. Open the employee sign-in link shown in the dashboard, such as `https://gptunnel.ru/?sso=company.ru`. You can also enter a work email address on the sign-in screen.
3. Sign in with a test account. GPTunneL redirects you to the IdP and returns you to GPTunneL after sign-in.

Use the same switch to turn off SSO at any time.

<a id="existing-gptunnel-accounts"></a>
## Existing GPTunneL accounts

- The first SSO sign-in links an existing GPTunneL account with the same email address to the user’s IdP account. The user’s balance, history, and settings are kept. No extra confirmation is required.
- If the account does not exist, GPTunneL creates it on first sign-in and adds the user to the organization as an employee.
- Accounts created by another organization cannot be linked.
- Later sign-ins use the IdP’s `sub` identifier. Changing the email address in the IdP does not create another account.
- To limit SSO to employees already added, turn off automatic account creation under **Provider**.

> **Note: Linking by email**
>
> GPTunneL links accounts by email address. Keep the email domain and IdP accounts under your control. Employees must be unable to change their IdP email to someone else’s address.

<a id="information-to-send-to-your-gptunnel-account-manager"></a>
## Information to send to your GPTunneL account manager

If you want your account manager to configure SSO, send them:

- The email domains.
- The Issuer and Client ID.
- The Client Secret through a secure channel.
- The email claim (`email`, `upn`, or another claim).
- The role claim and role mappings, if needed.
- The URL employees use to open GPTunneL.

Your account manager will provide the redirect URI. Register it with the IdP before testing sign-in.

<a id="limitations"></a>
## Limitations

- GPTunneL supports OIDC only. SAML is unavailable.
- Signing out of GPTunneL does not end the IdP session. Removing access in the IdP takes effect at the next sign-in and does not end an existing GPTunneL session.
- Each organization can connect one IdP.

<a id="troubleshooting"></a>
## Troubleshooting

| Symptom | What to check |
| --- | --- |
| The **Enable SSO** switch is disabled | Verify the domain and fill in the Issuer, Client ID, and Client Secret. |
| The IdP reports a redirect URI error | Check the domain and trailing slash. The IdP URI must match the dashboard value. |
| SSO sign-in fails after returning from the IdP | Check that the Issuer matches `iss`, the Client Secret is correct, and the token or UserInfo provides the email claim. |
| SSO is unavailable for this email | Verify the email domain and check that SSO is enabled. |
| Account not found | Automatic account creation may be disabled and the employee may be missing from the organization. |
| Roles were not applied | Check the claim path, token contents, and role mappings. |
