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).RealmViewercan 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.
Register the app
Section titled “Register the app”-
Make sure the realm offers every scope the app needs. It already offers
openid,profile,emailandoffline_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"] }' -
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.redirectUrismust be the exact addresses the app returns to, and their query may not carrycode,state,iss,error,error_descriptionorerror_uri. Every token names the API it is for:defaultAudienceis used when the app names none, and must be one ofallowedResources. -
Give the app its settings: the client ID
team-wiki, the secret, and the issuerhttps://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.
What users see
Section titled “What users see”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.
What the app sees
Section titled “What the app sees”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_accessand the user allowed it.
Check it worked
Section titled “Check it worked”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.
Troubleshooting
Section titled “Troubleshooting”| Symptom | Cause | Fix |
|---|---|---|
| The user sees an error page and never returns to the app | The return address isn’t registered exactly, or the client is inactive | Register the address the app sends, character for character |
The app is told invalid_request, “code_challenge is required” | No PKCE, or plain | Send an S256 challenge |
The app is told invalid_scope | It asked for a scope the client or the realm doesn’t offer | Add it to both lists (step 1 and 2) |
invalid_grant at the token endpoint | The code was used before, is over a minute old, or the PKCE verifier doesn’t match | Redeem each code once, at once, with its own verifier |
invalid_client at the token endpoint | Wrong secret, or sent by another method than tokenEndpointAuthMethod | Check the secret and the method |
| No refresh token | The app didn’t ask for offline_access, or the user didn’t allow it | See Choose online or offline access |
Undo it
Section titled “Undo it”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.
Related
Section titled “Related”- Authorization code with PKCE: the flow the app uses.
- OpenID Connect sign-in: ID tokens and userinfo.
- Consent: the page users see the first time.
- Refresh tokens: how the app stays signed in.
- Sign users in to a single-page app: when the app can’t keep a secret.
- Choose which details an app receives: what the ID token and userinfo carry.