Skip to content
Workforce IdentityMarkdown

Sign users in to a web app

Register a web app with a server behind it, so users sign in through the realm and the app learns who they are and calls its API.

APIRFC 6749, RFC 7636, OpenID Connect Core 1.0

Users sign in to your team wiki or customer portal with their realm account, and the app never sees their password. It learns who they are from an ID token, calls its API with an access token, and keeps them signed in with a refresh token. This guide is for a web app with a server behind it, which can keep a secret.

Before you start:

  • Who can do it: a realm administrator, in a group that holds RealmOwner (Delegated administration). RealmViewer can read the settings but not change them.
  • Where: today this is done through the API; Call the API shows how to get $CVYUH_SESSION.
  • First: know the app’s return address and the address of the API it calls.
  1. Make sure the realm offers every scope the app needs. It already offers openid, profile, email and offline_access. To add your API’s scopes, read the realm’s list first:

    Terminal window
    curl https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings \
    -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"

    Then send that list back with the new scopes added:

    Terminal window
    curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings \
    -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
    -H "Content-Type: application/json" \
    -d '{ "scopes": ["…existing scopes", "wiki:read", "wiki:write"] }'
  2. Register the app:

    Terminal window
    curl 'https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client?id=team-wiki' \
    -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
    -H "Content-Type: application/json" \
    -d '{
    "status": "active",
    "clientName": "Team Wiki",
    "tokenEndpointAuthMethod": "client_secret_basic",
    "clientSecret": "<a long random secret>",
    "grantTypes": ["authorization_code", "refresh_token"],
    "redirectUris": ["https://wiki.example.com/callback"],
    "postLogoutRedirectUris": ["https://wiki.example.com/"],
    "scopes": ["openid", "profile", "email", "offline_access",
    "wiki:read", "wiki:write"],
    "defaultScopes": ["openid", "wiki:read"],
    "allowedResources": ["https://wiki.example.com/api"],
    "defaultAudience": "https://wiki.example.com/api"
    }'

    The realm answers 201. redirectUris must be the exact addresses the app returns to, and their query may not carry code, state, iss, error, error_description or error_uri. Every token names the API it is for: defaultAudience is used when the app names none, and must be one of allowedResources.

  3. Give the app its settings: the client ID team-wiki, the secret, and the issuer https://foobar.cvyuh.com/oauth2. Most libraries read the rest from the discovery document. The secret is stored hashed and never shown again, so keep a copy for the app.

They click Sign in in the app and arrive at the realm’s sign-in page. They sign in through the realm’s usual steps. The first time, a consent page names the app and what it asks for, and they click Allow. Then they’re back in the app, signed in.

If they deny, the app is told access_denied. If the app’s return address isn’t one you registered, the user sees an error page and the app is told nothing.

The browser returns to the app’s registered address with a one-time code and the realm’s iss. The app’s server exchanges the code at https://foobar.cvyuh.com/oauth2/token, proving itself with its client secret and the PKCE verifier. It receives:

  • an access token for its API, valid five minutes by default;
  • an ID token naming the user, when it asked for openid;
  • a refresh token, when it asked for offline_access and the user allowed it.

Sign in to the app as a test user. You should pass the realm’s sign-in page and consent, and land back in the app. A code works once, within a minute: using it a second time is refused, and the tokens it bought are revoked.

Each sign-in is on the user’s trail: a LOGIN record for the sign-in, then a TOKEN_ISSUED record with grantType authorization_code, the client, and the scopes and API granted. Find them in Audit → Authentication in the dashboard.

SymptomCauseFix
The user sees an error page and never returns to the appThe return address isn’t registered exactly, or the client is inactiveRegister the address the app sends, character for character
The app is told invalid_request, “code_challenge is required”No PKCE, or plainSend an S256 challenge
The app is told invalid_scopeIt asked for a scope the client or the realm doesn’t offerAdd it to both lists (step 1 and 2)
invalid_grant at the token endpointThe code was used before, is over a minute old, or the PKCE verifier doesn’t matchRedeem each code once, at once, with its own verifier
invalid_client at the token endpointWrong secret, or sent by another method than tokenEndpointAuthMethodCheck the secret and the method
No refresh tokenThe app didn’t ask for offline_access, or the user didn’t allow itSee Choose online or offline access

To turn the app off, PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/team-wiki with { "status": "inactive" }. Its refresh tokens are refused from then on.