Quickstart

Sign a user in to a server-side application with the authorization code flow and PKCE, verify the ID token, and start your own session.

Sign a user in from a server-side application in five steps: send them to onefold, receive them back, exchange the code, verify the ID token, and start your own session.

Before you start, set up an application with:

  • the redirect URI http://localhost:3000/callback
  • the Identity scope enabled, which it is by default
  • its client ID and client secret at hand

Create a PKCE pair and a state value

Each sign-in needs a fresh code verifier, its SHA-256 code challenge, and a random state. Keep the verifier and the state in the user's session on your server. You need both when the user returns.

verifier=$(openssl rand -base64 32 | tr '+/' '-_' | tr -d '=')
challenge=$(printf '%s' "$verifier" | openssl dgst -sha256 -binary | openssl base64 | tr '+/' '-_' | tr -d '=')
state=$(openssl rand -hex 16)

Send the user to onefold

Redirect the browser to the authorization endpoint. URL-encode each value.

GET https://onefold.eitozx.com/o/authorize/
  ?response_type=code
  &client_id=YOUR_CLIENT_ID
  &redirect_uri=http://localhost:3000/callback
  &scope=openid identity
  &state=STATE
  &code_challenge=CHALLENGE
  &code_challenge_method=S256

The user signs in on your application's hosted sign-in page, then approves what your application asks for.

Handle the callback

onefold sends the browser back to your redirect URI:

http://localhost:3000/callback?code=AUTHORIZATION_CODE&state=STATE&iss=https%3A%2F%2Fonefold.eitozx.com
  1. Compare state with the value you stored. Stop if they differ.
  2. If the callback carries error instead of code, the sign-in did not complete. error=access_denied means the user declined.
  3. Accept the iss parameter. Every callback carries it. Authorization request and callback says when to check its value.

Exchange the code

From your server, post the code and the verifier to the token endpoint. The body is form-encoded, not JSON. A code works once and expires after 60 seconds.

curl -X POST https://onefold.eitozx.com/o/token/ \
  -d grant_type=authorization_code \
  -d code=AUTHORIZATION_CODE \
  -d redirect_uri=http://localhost:3000/callback \
  -d client_id=YOUR_CLIENT_ID \
  -d client_secret=YOUR_CLIENT_SECRET \
  -d code_verifier="$verifier"

The response carries an access_token, a refresh_token and, because you requested openid, an id_token.

Verify the ID token and start your session

The ID token is a JWT signed with RS256. Verify it before you trust it.

import { createRemoteJWKSet, jwtVerify } from "jose";

const jwks = createRemoteJWKSet(
  new URL("https://onefold.eitozx.com/.well-known/jwks.json"),
);
const { payload } = await jwtVerify(tokens.id_token, jwks, {
  issuer: "https://onefold.eitozx.com",
  audience: process.env.CLIENT_ID,
  algorithms: ["RS256"],
});
const userId = payload.sub; // a string

sub identifies the user. Store it as a string and key the user on it in your own database; an email address can change, sub does not. With the identity scope the ID token also carries email and email_verified.

Then set your own session, the way your framework does it. onefold does not manage your application's session.

Next

On this page