Sign users in to a single-page app
Register a browser app that holds no secret, so users sign in through the realm and the page calls the realm directly.
APIRFC 6749, RFC 7636
An app that runs entirely in the browser, such as a dashboard with no server of its own, can’t keep a secret: anything in the page can be read by whoever opens it. You register it as a public client. It gets the same sign-in as a web app, proves it started the sign-in with PKCE alone, and calls the realm’s token, revocation and userinfo endpoints straight from the page.
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: the realm offers the scopes the app needs (step 1 of Sign users in to a web app).
Register the app
Section titled “Register the app”-
Register the app as a public client:
Terminal window curl 'https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client?id=ops-dashboard' \-H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \-H "Content-Type: application/json" \-d '{"status": "active","clientName": "Ops Dashboard","tokenEndpointAuthMethod": "none","grantTypes": ["authorization_code", "refresh_token"],"redirectUris": ["https://ops.example.com/callback"],"postLogoutRedirectUris": ["https://ops.example.com/"],"scopes": ["openid", "profile", "offline_access", "ops:read"],"defaultScopes": ["openid", "ops:read"],"allowedResources": ["https://ops.example.com/api"],"defaultAudience": "https://ops.example.com/api"}'tokenEndpointAuthMethod: nonemakes it public; leave outclientSecret.refreshTokenRotationneeds no setting, since a public client always rotates. -
If the page runs on a different site from its return address, list that site in
webOrigins, keeping+for the return addresses’ own sites:Terminal window curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/ops-dashboard \-H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \-H "Content-Type: application/json" \-d '{ "webOrigins": ["+", "https://ops-app.example.com"] }'A site is written exactly as a browser sends it: scheme and host, a port only if it isn’t the default, no path. When the page runs on the same site as its return address, skip this step.
-
Give the app its settings: the client ID
ops-dashboardand the issuerhttps://foobar.cvyuh.com/oauth2. There’s no secret.
What the app sees
Section titled “What the app sees”- The page redeems the code at
https://foobar.cvyuh.com/oauth2/tokenwith its PKCE verifier and no secret. - The realm lets the page read the answer because the app’s site is one of its
webOrigins. By default that’s the site of each registered return address (webOrigins: ["+"]). A page on any other site can’t read the answer. See Browser apps. - Each refresh token works once. Every refresh returns a new one, and if an old one is ever used again, the realm ends the whole family. That is how a stolen refresh token is caught.
- A public app can never use client credentials.
Every sign-in must carry a PKCE challenge using the S256 method. A request without one, or with plain, is refused.
Users see the same as for a web app: the realm’s sign-in page, consent the first time, then back in the app.
Check it worked
Section titled “Check it worked”Sign in from the page as a test user, and watch the token call in the browser’s developer tools: it should succeed
with no CORS error. A CORS error means the page’s site isn’t in webOrigins.
Each sign-in is on the user’s trail as a LOGIN record and a TOKEN_ISSUED record for the client; a refresh token
used twice is a TOKEN_ISSUED record with result FAILED and the reason “the refresh token was already used”. Find
them in Audit → Authentication in the dashboard.
Troubleshooting
Section titled “Troubleshooting”| Symptom | Cause | Fix |
|---|---|---|
| A CORS error on the token call | The page’s site isn’t in webOrigins | Add it exactly as the browser sends it (step 2) |
invalid_client at the token endpoint | The page sent a secret, or the client isn’t registered with none | Send no secret; register tokenEndpointAuthMethod: none |
invalid_grant, “already used”, and the user is signed out | Two tabs refreshed with the same token, or a copy was used | Refresh from one place in the page; if neither, treat it as theft |
| The user is sent to sign in once a day | The refresh family’s lifetime ran out | Expected; see Keep apps signed in |
Undo it
Section titled “Undo it”PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/ops-dashboard with { "status": "inactive" } turns the app off.
Related
Section titled “Related”- Browser apps:
webOriginsand what the realm answers from the page. - Authorization code with PKCE: the flow the app uses.
- Refresh tokens: rotation and families.
- Sign users in to your own native app: your own app’s sign-in screens.