# Connect one app to many business customers

> Serve every business customer from one SaaS app, each signing in to their own realm, with the app trusting each realm's issuer and telling their tokens apart.

You sell one app, `https://app.example.com`, to many businesses. Each business customer has a realm of its own
beneath your customer realm (see [A realm per business customer](../../realms/business-customer-realms/)), so their
users, administrators and sign-in settings are theirs. This guide is for the developer who connects the one app to
all of those realms, here `acme` at `foobar-acme.cvyuh.com` and `globex` at `foobar-globex.cvyuh.com`. It answers
four questions: where the app sends each user, where the app is registered, which issuers and keys it trusts, and how
it tells one business customer's tokens from another's.

**Before you start:**

- each business customer has its realm, with its host name;
- you hold `RealmOwner` in your customer realm, which reaches every realm beneath it, with your session in
  `$CVYUH_SESSION` (see [Call the API](../../../develop/api/)); or each business customer's own administrator does
  their realm's step;
- the app uses an OpenID Connect library that can hold more than one provider.

## The four answers

| Question | Answer |
|---|---|
| Where does the app send a user? | To their business customer's realm host. The app decides which, from its own records: the realm doesn't find a business customer from an email address |
| Where is the app registered? | In each business customer's realm. A client belongs to one realm, and a realm knows no client of another |
| Which issuer and keys does it trust? | Each realm's own: for acme, issuer `https://foobar-acme.cvyuh.com/oauth2`, with keys at that issuer's `jwks_uri` |
| How does it tell them apart? | By the token's `iss`. Key every user by `iss` and `sub` together |

## Connect the app

<Steps>

1. **Keep a map from your tenants to realm hosts.** In your app, each business customer you serve has a realm host:
   `acme` is `foobar-acme.cvyuh.com`. Choose the tenant the way your app already does, by a subdomain such as
   `acme.app.example.com`, a tenant picker, or the domain of the email address the user types, and look up its host.

2. **Register the app in each realm.** On each realm's own host, create the same client. The client ID can be the
   same in every realm; give each its own secret:

   ```sh
   curl "https://foobar-acme.cvyuh.com/api/v0/am/objects/v1/oauth2Client?id=example-app" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{
       "status": "active",
       "clientName": "Example App",
       "tokenEndpointAuthMethod": "client_secret_basic",
       "clientSecret": "<a secret for acme>",
       "grantTypes": ["authorization_code", "refresh_token"],
       "redirectUris": ["https://app.example.com/callback"],
       "scopes": ["openid", "profile", "email"],
       "defaultScopes": ["openid"],
       "allowedResources": ["https://app.example.com/api"],
       "defaultAudience": "https://app.example.com/api"
     }'
   ```

   The realm answers `201`. Repeat on `foobar-globex.cvyuh.com` with globex's secret. Do it as part of setting up
   each new business customer.

3. **Add each realm as a provider in the app.** Point the library at each realm's discovery document,
   `https://foobar-acme.cvyuh.com/oauth2/.well-known/openid-configuration`. It reads the issuer,
   `https://foobar-acme.cvyuh.com/oauth2`, the endpoints, and the `jwks_uri` whose keys sign that realm's tokens.
   Store the client secret for that realm beside it.

4. **Send each user to their realm.** When a user of acme chooses **Sign in**, the app sends them to acme's
   authorization endpoint, from acme's discovery document, with a PKCE challenge and a `state` that records acme. The
   user signs in with acme's settings, and sees acme's consent page.

5. **Check the answer against the realm you chose.** On the callback, find the provider from `state`, exchange the
   code at that realm's token endpoint, and check the ID token with that realm's keys. Its `iss` must be the issuer
   you sent the user to. Anything else is refused.

</Steps>

<Aside type="caution" title="Never trust an issuer the user picked">
  Check each token against the realm you sent the user to, never against whichever realm the token names. A token from
  another business customer's realm is valid there, and must not open acme's data in your app.
</Aside>

## Check it worked

Sign in as a user of acme and decode the ID token: `iss` is `https://foobar-acme.cvyuh.com/oauth2`. Sign in as a user
of globex: `iss` is `https://foobar-globex.cvyuh.com/oauth2`, and the two users' records in your app are apart, even if
their `sub` values ever matched.

## What to know

- **Each realm signs with its own keys.** Read them from each realm's `jwks_uri`, and refresh them as your library
  does; see [Signing keys](../../../platform/keys/signing-keys/).
- **Sessions and consent are per realm.** A user signed in to acme isn't signed in to globex, and consent given in one
  realm means nothing in another.
- **One person in two business customers has two accounts,** one in each realm.
- **Your API checks the same way.** An API that serves every business customer accepts each realm's issuer and keys,
  and keys its data by `iss`.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| `invalid_client` at one business customer only | The app isn't registered in that realm, or its secret there is different | Register it in that realm, and use that realm's secret |
| The ID token's issuer doesn't match | The app checked it against another realm's issuer | Find the provider from `state`, not from the token |
| A signature doesn't verify | The app used another realm's keys | Use the keys from the issuer's own `jwks_uri` |
| `redirect_uri` refused at one business customer | That realm's client lists other return addresses | List the same `redirectUris` in every realm |

## Undo it

To stop the app for one business customer, deactivate its client in their realm:

```sh
curl -X PATCH https://foobar-acme.cvyuh.com/api/v0/am/objects/v1/oauth2Client/example-app \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "status": "inactive" }'
```

Then remove that realm from the app's providers.

## Related

- [A realm per business customer](../../realms/business-customer-realms/): make and hand over each realm.
- [Discovery](../../../workforce-identity/applications/registration/discovery/): what each realm's discovery document
  holds.
- [Get started with Customer Identity](../../get-started/): one app and one realm, end to end.
