OpenID Connect
Sign people in and out with any OpenID Connect library, the flow it follows, and the endpoints, scopes and claims.
Sign people in (OpenID Connect)
Any standard OpenID Connect library can do it from the discovery document.
Paste this into Claude Code, Cursor or another coding agent, in your web app's project. It tells you what to register on the console, then writes sign-in and sign-out with OpenID Connect, and, for an app with a backend, the sign-in check and the token check.
Show the full prompt
Connect this web app's sign-in to Otoko Identity (OpenID Connect)
You are connecting the sign-in of the web app you're working in to Otoko Identity (OI), a standard OpenID Connect provider. Work through the steps below in order, and stop where a step says to wait for the person.
Scope
- Sign-in only: sending people to OI to sign in, the callback, the session or tokens, signing out, the sign-in check OI calls on the backend, and checking OI's tokens in the backend's APIs.
- OI runs sign-up and forgot password on its own pages. Don't build them, and don't link to OI for them.
- Change nothing unrelated to signing in.
Rules
- Secrets live only in an env file that git ignores: the client secret, the API key and the webhook secret.
- Never open, print or paste the whole env file, and never ask for a secret in the chat. To check a setting is there, look for its name only (for example
grep -c '^IDENTITY_CLIENT_SECRET=' .env.local). - To use a value that isn't secret, read only its own line by name.
- Never put a secret in browser code, a log line or a commit.
- Never open, print or paste the whole env file, and never ask for a secret in the chat. To check a setting is there, look for its name only (for example
- Use a maintained OpenID Connect library: openid-client on a Node server, oidc-client-ts in the browser, or the established one for the project's stack. For the backend's token checks, use a JWT library such as jose. Never write the flow or the token checks by hand.
- Use https everywhere outside a local machine.
1. Look at the project and tell which kind it is
Find its stack, how it runs, its address and port, where its routes and server code live, and any sign-in it already has. Then decide, and confirm with the person:
- Server sign-in: the app runs server code when someone signs in and can keep a secret, as with Next.js route handlers or server actions, Express, Django, Rails or Laravel. Its server signs people in, as a backend-for-frontend, and the browser gets only a session cookie. This is the recommended kind whenever the app has server code.
- Browser sign-in: the app is only static files, such as a single-page app (React, Vue, Svelte) or a static export, perhaps calling a separate backend. The browser signs people in, with no secret, and the backend answers OI and checks tokens.
- A mobile app: stop, and send the person to the prompt on https://identity-docs.otoko.com/app-settings/mobile-apps.
Decide by what the project runs, not by its framework: a Next.js app built with output: 'export' is static files, and has no server code at sign-in.
2. Tell the person what to register, then wait
The app is registered on OI's console; you can't do this part.
- Name the env file the settings go in, the server's or backend's, such as
.env.local. Check that git ignores it before anything is pasted, and add it to.gitignoreif not. - Work out the values from the project, then tell the person to open https://identity.otoko.com/admin/apps/new and enter:
- App name: the product's name. App ID: a slug of it in lowercase letters, digits and hyphens. It can't change later.
- Does your app have an API that Otoko Identity calls? Yes, unless the project has no server or backend at all. Answered No, the kit's Server section takes the Backend section's place, with no
IDENTITY_CALLBACK_TOKENorIDENTITY_ALLOWED_CLIENT_IDS, OI calls nothing, and steps 5 and 6 don't apply. - Kind of app: Web app. Then Does your app run server code when someone signs in? Yes for server sign-in, the default, or No for browser sign-in, as you found. Client ID: keep the one the wizard suggests.
- App address: where the app runs, such as
http://localhost:3000. - Sign-in return address: the callback route or page you'll add, such as
http://localhost:3000/auth/callback. It must match the code exactly. For browser sign-in, OI lets this address's origin call its sign-in endpoints. - Sign-out return address: the page people land on after signing out, such as
http://localhost:3000/signed-out. Leave the password reset return address as it is. - Backend address, with a backend: where OI reaches this project's server or backend. With OI running in Docker on the same machine, that's
http://host.docker.internal:<port>, notlocalhost. - Send this app's emails through Otoko Identity's mail server: leave it ticked, with an optional Reply-to address. Only for an app with its own mail server, untick it, enter the From address OI sends this app's emails from, and fill in the Mail server section that appears: host, port, encryption, username and password. The person types the password there themselves; never ask for it in the chat.
- Tell them to choose Create, then on the last step each section's own Copy all, and paste them into the env file you named. The secrets are shown only once. Ask them to tell you when it's done, and wait.
- Check the paste by names only.
The kit's sections give:
- Frontend, for the app:
IDENTITY_DISCOVERY_URL,IDENTITY_ISSUER,IDENTITY_CLIENT_ID,IDENTITY_REDIRECT_URIandIDENTITY_SCOPES, plusIDENTITY_CLIENT_SECRETfor server sign-in only. - Backend:
IDENTITY_URL,IDENTITY_APP_ID,IDENTITY_API_KEY,IDENTITY_CALLBACK_TOKEN(the webhook secret) andIDENTITY_ALLOWED_CLIENT_IDS, or with no API that OI calls, Server: onlyIDENTITY_URL,IDENTITY_APP_IDandIDENTITY_API_KEY. Sign-in doesn't use the API key; leave it there for later. - For browser sign-in: its five app settings aren't secret, and the browser build needs them. Add each to the front end's env with the stack's public prefix, such as
VITE_IDENTITY_CLIENT_IDorNEXT_PUBLIC_IDENTITY_CLIENT_ID, reading each value only by its own line. Never expose the backend's settings to the browser, and never makeIDENTITY_itself a public prefix.
3. Sign in
Server sign-in:
- Read OI's discovery document at
IDENTITY_DISCOVERY_URLthrough the library. Its issuer isIDENTITY_ISSUER. For a plainhttp://issuer on a local machine, let the library allow it for that issuer only. - A sign-in route sends the browser to the authorization endpoint with
response_type=code,IDENTITY_CLIENT_ID,IDENTITY_REDIRECT_URI,IDENTITY_SCOPES, astate, anonce, and PKCE withcode_challenge_method=S256. Keep the PKCE verifier, the state and the nonce in a short-lived, httpOnly cookie, sealed or signed. - The callback route at
IDENTITY_REDIRECT_URIswaps the code with the PKCE verifier andIDENTITY_CLIENT_SECRET, sent as HTTP Basic, the console's default. The library checks the state, the nonce and the ID token. - Keep the session, the ID token included, in an httpOnly, SameSite=Lax cookie, Secure outside localhost and encrypted, or in a server-side store.
Browser sign-in:
- Configure the library with
IDENTITY_ISSUERas the authority (it reads the discovery document),IDENTITY_CLIENT_ID,IDENTITY_REDIRECT_URIandIDENTITY_SCOPES, using the code flow with PKCE (S256), astateand anonce. It keeps them between the redirect and the callback. - Sign-in sends the browser to OI. The callback page lets the library swap the code, with no secret, and check the ID token.
- Keep the tokens where the library keeps them, in memory or session storage, never in local storage.
- The browser sends the access token to the backend as
Authorization: Bearer <access token>.
Either way:
- OI's ID token says only who signed in and how:
sub,amr,auth_timeandnonce. Get the name and email from the userinfo endpoint with the access token. - Pages that need a signed-in person send everyone else to sign in.
4. Sign out
- Server sign-in: a sign-out route, a POST so a link elsewhere can't sign people out, reads the ID token from the session and clears the session. It then sends the browser to OI's end-session endpoint with that ID token as
id_token_hintand the sign-out return address aspost_logout_redirect_uri. - Browser sign-in: use the library's sign-out redirect, which sends the stored ID token as
id_token_hintand the sign-out return address aspost_logout_redirect_uri, and clears the tokens. - Without the ID token, OI asks the person to confirm and won't return them to the app, so keep it until sign-out.
5. The sign-in check route (required)
OI asks the backend on every sign-in whether this person may use the app, and anything other than {"allowed": true} keeps them out. Until this route works, nobody can sign in. Add it to the server or backend exactly so:
POST /internal/v1/sign-in-check
Authorization: Bearer <IDENTITY_CALLBACK_TOKEN>
Content-Type: application/json
{
"sub": "5b1c6f0e-0000-4000-8000-000000000000",
"client_id": "your-client-id",
"pending_actions": []
}
HTTP/1.1 200 OK
Content-Type: application/json
{ "allowed": true }- Compare the bearer token with
IDENTITY_CALLBACK_TOKENin constant time, and answer 401 to anything else. - Refuse (
{"allowed": false}) aclient_idthat isn't inIDENTITY_ALLOWED_CLIENT_IDS, a comma-separated list. - Answer
{"allowed": true}for everyone else for now. Leave a comment that a real app checks its own records here, such as a blocked account or a missing invitation. - Keep it read-only and fast: OI waits 3 seconds for it. Make no user here. With no answer, the app's If the backend can't be reached setting decides, and it refuses by default.
- OI calls the
/internal/v1/routes server to server, with that bearer token and no cookies. Keep them out of the project's sign-in middleware and CSRF protection, or every sign-in fails.
6. Check OI's tokens in the backend's APIs
With browser sign-in, the backend needs this on every route that needs a signed-in person. With server sign-in, it's needed only for APIs that take OI's access tokens. Answer 401 to a missing or failing token:
- ES256 only, with keys from
IDENTITY_URLfollowed by/.well-known/jwks.json, andissequal toIDENTITY_URL. audandazpboth amongIDENTITY_ALLOWED_CLIENT_IDS.- The
typclaim in the token's payload isBearer. It's a claim, not the JWT header'styp: with jose, check it among the claims, not with thetypoption. iss,sub,exp,iat,jtiandsidall present. Refuse a token with"service": true.
To refuse a signed-out session's tokens at once, not when they expire, add the ended sessions route as the prompt on https://identity-docs.otoko.com/api-reference/calls-to-your-backend describes.
To give the backend its own user for each person, make it on the person's first request, from IDENTITY_URL followed by /oauth/userinfo with their access token, keyed by sub, never by email. To make it earlier, add the signed account created route as the prompt on https://identity-docs.otoko.com/api-reference/calls-to-your-backend describes.
7. Check it works
Run the project. Sign in with a person of this app: one already in it, or one the person adds on the app's Users tab in the console. Call one protected route, then sign out. If OI shows an error, read https://identity-docs.otoko.com/troubleshooting.
Finish with a short report:
- the kind you set up;
- the files you changed, and the routes and pages you added;
- the settings the code reads, by name;
- anything the person still has to do.
More detail
- https://identity-docs.otoko.com/api-reference/openid-connect: the flow, endpoints and scopes.
- https://identity-docs.otoko.com/api-reference/calls-to-your-backend: the routes OI calls.
- https://identity-docs.otoko.com/api-reference/tokens: checking tokens and lifetimes.
- https://identity-docs.otoko.com/api-reference/errors: every error code.
| Value | This deployment |
|---|---|
| Issuer | The address of your Otoko Identity service. The setting IDENTITY_URL holds it, and the console shows it as Issuer under Connection details, on the app's Overview tab. |
| Discovery URL | The Issuer followed by /.well-known/ |
The examples below write the Issuer as https://identity.example.com. Put your own in its place.
The flow
- Send the browser to
/oauth/authorizewithresponse_type=code, your client ID, an exact redirect address, ascopestarting withopenid, astate, anonce, and a PKCEcode_challengewithcode_challenge_method=S256. PKCE stays required for confidential clients. - Send
ui_localesto pick the page language. One the app doesn't offer comes in the app's default.promptmay benone,loginorconsent. - Back at your redirect address, swap the code at
/oauth/tokenwith the PKCE verifier. A public client sends no secret. A confidential client sends its secret the way its client authenticator says, never both: Basic is anAuthorizationheader ofclient_idand the secret, each URL-encoded and joined by a colon, then base64, as RFC 6749 section 2.3.1 says, and post isclient_idandclient_secretin the form. - Refresh the same way, revoke at
/oauth/revoke, and introspect at/oauth/introspect. - Sign out at the end session endpoint, with the ID token as
id_token_hint. Where it may go back to is the client's post-logout redirect list. - For a service with no person signed in, use
grant_type=client_credentials, once the confidential client's Client credentials scopes list names what it may ask for. The token has the client ID assuband"service": true, and no refresh token, so ask again when it expires.
Building the sign-in address in Next.js:
import { createHash, randomBytes } from "node:crypto";
// In a Next.js route handler. Keep codeVerifier, state and nonce (in a cookie, for example)
// for the callback, where the code is swapped at https://identity.example.com/oauth/token.
export function buildSignInUrl(redirectUri: string) {
const codeVerifier = randomBytes(32).toString("base64url");
const codeChallenge = createHash("sha256").update(codeVerifier).digest("base64url");
const state = randomBytes(16).toString("base64url");
const nonce = randomBytes(16).toString("base64url");
const url = new URL("https://identity.example.com/oauth/authorize");
url.search = new URLSearchParams({
response_type: "code",
client_id: "your-client-id",
redirect_uri: redirectUri,
scope: "openid profile email",
state,
nonce,
code_challenge: codeChallenge,
code_challenge_method: "S256",
}).toString();
return { url: url.toString(), codeVerifier, state, nonce };
}The endpoints, scopes, claims and discovery settings follow.
Endpoints
| Endpoint | Path under the Issuer | What it does |
|---|---|---|
| Discovery document | /.well-known/ | Every address and setting below, for an OpenID Connect library to read. |
| Authorization | /oauth/ | Where the browser goes to sign in. Response type code, with PKCE S256. |
| Token endpoint | /oauth/ | Swaps a code for tokens, refreshes them, and gives a service its client credentials token. |
| Revocation | /oauth/ | Ends a session from a refresh token or an access token this client was given. The answer is 200 for any token, but 400 unsupported_token_type for the client's own client credentials token, which expires on its own. |
| Introspection | /oauth/ | Says whether a token is still good, for a confidential client. A token of any client of the same app answers active true while good, and anything else active false. |
| User info | /oauth/ | The claims the granted scopes allow, for an access token. |
| End session | /oauth/ | Signs a person out. Send the ID token as id_token_hint. |
| Keys (JWKS) | /.well-known/ | The public keys that verify tokens. Tokens are signed with ES256. |
Scopes
| Scope | What it lets your app read |
|---|---|
openid | Makes the request an OpenID Connect sign-in, with an ID token. |
profile | name, given_name, family_name, locale, and birthdate when the app's sign-up asks for one. |
email | email and email_verified. |
phone | phone_number and phone_number_verified, when the app's sign-up asks for a phone number. Only for people: a client credentials token can't have it. |
roles | roles. |
offline_access | Accepted and granted, and changes nothing, as every sign-in gets a refresh token anyway. |
What the discovery document says
| Setting | Value |
|---|---|
| Response types | code |
| Response modes | query |
| Grant types | authorization_code, refresh_token, client_credentials |
| Code challenge methods | S256 |
| Token endpoint authentication | none, client_secret_basic, client_secret_post |
| ID token signing | ES256 |
| Prompt values | none, login, consent |
| Languages (ui_locales) | de, en |
| Subject type | public |
Claims: iss, sub, aud, exp, iat, auth_time, nonce, amr, azp, name, given_name, family_name, locale, email, email_verified, phone_number, phone_number_verified, birthdate, roles