First-Party PKCE Flow | TTF Docs
Skip to main content
sso

First-Party PKCE Flow

Step-by-step guide to registering an internal TTF app and implementing OAuth2 Authorization Code + PKCE login and callback handling.

1. Register the App Client (Superadmin)

Register your app's exact redirectUris once via POST /api/sso/clients (no prefix matching is allowed):

bash
curl -X POST https://api.timetofuture.com/api/sso/clients \
  -H "Authorization: Bearer <superadmin JWT>" \
  -H "Content-Type: application/json" \
  -d '{
    "clientId": "mail",
    "name": "TTF Mail",
    "redirectUris": [
      "https://mail.timetofuture.com/sso/callback",
      "http://localhost:5173/sso/callback"
    ]
  }'

2. Redirect to /api/sso/authorize

Generate a S256 PKCE verifier/challenge pair and CSRF state, store them in sessionStorage, and redirect:

js
function base64url(bytes) {
  return btoa(String.fromCharCode(...new Uint8Array(bytes)))
    .replace(/\+/g, '-')
    .replace(/\//g, '_')
    .replace(/=+$/, '');
}

export async function signInWithTTF() {
  const verifier = base64url(crypto.getRandomValues(new Uint8Array(32)));
  const challenge = base64url(
    await crypto.subtle.digest('SHA-256', new TextEncoder().encode(verifier))
  );
  const state = base64url(crypto.getRandomValues(new Uint8Array(16)));

  sessionStorage.setItem('pkce_verifier', verifier);
  sessionStorage.setItem('pkce_state', state);

  const url = new URL('https://api.timetofuture.com/api/sso/authorize');
  url.searchParams.set('client_id', 'mail');
  url.searchParams.set('redirect_uri', 'https://mail.timetofuture.com/sso/callback');
  url.searchParams.set('response_type', 'code');
  url.searchParams.set('state', state);
  url.searchParams.set('code_challenge', challenge);
  url.searchParams.set('code_challenge_method', 'S256');

  window.location.href = url.toString();
}

3. Handle the Callback & Exchange Code

On /sso/callback, validate state and exchange code + code_verifier at POST /api/sso/token:

js
export async function handleSsoCallback() {
  const params = new URLSearchParams(window.location.search);
  const code = params.get('code');
  if (!code) return;

  if (params.get('state') !== sessionStorage.getItem('pkce_state')) {
    throw new Error('state mismatch — possible CSRF');
  }

  const res = await fetch('https://api.timetofuture.com/api/sso/token', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      grant_type: 'authorization_code',
      code,
      code_verifier: sessionStorage.getItem('pkce_verifier'),
      client_id: 'mail',
      redirect_uri: 'https://mail.timetofuture.com/sso/callback',
    }),
  });

  const { data } = await res.json();
  localStorage.setItem('token', data.token);
  localStorage.setItem('refreshToken', data.refreshToken);
  localStorage.setItem('user', JSON.stringify(data.user));
  window.location.replace('/');
}

4. Central Logout

Call POST /api/sso/logout in addition to clearing localStorage when you want "Sign out everywhere" across all TTF apps. Authorization codes are single-use and expire in SSO_AUTH_CODE_TTL_SECONDS (default 60s).