All files / service-auth-client/src index.ts

88.51% Statements 131/148
79.41% Branches 108/136
94.73% Functions 18/19
91.85% Lines 124/135

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376                                                                              17x 17x 17x 17x 17x 17x     95x 285x 285x       95x           21x 21x 21x 12x     30x 30x 30x 3x 3x       3x 3x     4x 4x 1x 1x   4x     34x 34x     38x     79x 62x       58x     15x                 19x                     27x 11x 16x 16x 16x       19x 17x 17x 9x 9x 6x   6x                         3x             12x 12x                               11x 10x 10x     8x 11x 8x   2x         48x 48x   17x                       14x 3x 3x 3x 3x 3x           31x 31x 30x 30x 10x 10x   30x                     30x 9x 6x 6x   6x   6x 4x 4x   2x   21x       21x 11x 11x 11x 11x 11x 11x 11x                     11x 7x   11x 11x                       11x 11x   10x 10x 10x 10x 9x           9x 9x           9x 9x   2x             1x 1x   1x   7x                 7x       4x 4x         4x 4x         10x 4x     4x 4x 4x                                     4x 4x 10x   5x          
import * as oauth from 'oauth4webapi';
import { EncryptJWT, jwtDecrypt, jwtVerify, createRemoteJWKSet, customFetch } from 'jose';
import { applySocialSignInMethod } from './social-sign-in.ts';
 
/** One relying party. Cookies never leave PUBLIC_ORIGIN; the issuer may be on an unrelated domain. */
export interface OIDCEnv {
  AUTH_ISSUER: string;
  PUBLIC_ORIGIN: string;
  OIDC_CLIENT_ID: string;
  /** A distinct random secret (at least 32 characters) for each service and environment. */
  SESSION_SECRET: string;
  /** The resource this service calls on behalf of its user. */
  API_AUDIENCE: string;
  /**
   * The Worker that runs the issuer, if this one has a Service Binding to it. Discovery, token and JWKS requests to
   * the issuer's origin then go through the binding instead of the public network, so they still work when the
   * issuer's host is behind Cloudflare Access (docs/services.md section 2). Without it they use `fetch`.
   */
  ACCOUNTS?: IssuerFetcher;
}
 
/** The part of a Service Binding (`Fetcher`) the relying party uses. */
export interface IssuerFetcher {
  fetch(request: Request): Promise<Response>;
}
 
export interface Identity {
  issuer: string;
  sub: string;
  name: string;
  anonymous: boolean;
  linkedGuests?: string[];
  passkeys?: number;
}
export interface LoginSession {
  identity: Identity;
  accessToken: string;
  expiresAt: number;
}
const SESSION_COOKIE = '__Host-be-music.session';
const TRANSACTION_COOKIE = '__Host-be-music.oidc';
const SSO_CHECK_COOKIE = '__Host-be-music.sso-check';
const CLAIM = 'https://be-music.app/anonymous';
const servers = new Map<string, oauth.AuthorizationServer>();
const keySets = new Map<string, ReturnType<typeof createRemoteJWKSet>>();
 
function validateEnv(env: OIDCEnv): void {
  for (const value of [env.AUTH_ISSUER, env.PUBLIC_ORIGIN, env.API_AUDIENCE]) {
    const url = new URL(value);
    Iif (url.protocol !== 'https:' || url.username || url.password || url.search || url.hash) {
      throw new Error('OIDC endpoints must be configured HTTPS URLs');
    }
  }
  Iif (new URL(env.PUBLIC_ORIGIN).origin !== env.PUBLIC_ORIGIN || env.SESSION_SECRET.length < 32) {
    throw new Error('Invalid relying-party configuration');
  }
}
/** Fetch requests to the issuer's origin through `ACCOUNTS` when it is bound; anything else uses `fetch`. */
function issuerFetch(env: Pick<OIDCEnv, 'AUTH_ISSUER' | 'ACCOUNTS'>) {
  const binding = env.ACCOUNTS;
  const origin = new URL(env.AUTH_ISSUER).origin;
  return (url: string, init: RequestInit): Promise<Response> =>
    binding && new URL(url).origin === origin ? binding.fetch(new Request(url, init)) : fetch(url, init);
}
async function server(env: OIDCEnv): Promise<oauth.AuthorizationServer> {
  validateEnv(env);
  const cached = servers.get(env.AUTH_ISSUER);
  if (cached) return cached;
  const issuer = new URL(env.AUTH_ISSUER);
  const metadata = await oauth.processDiscoveryResponse(
    issuer,
    await oauth.discoveryRequest(issuer, { [oauth.customFetch]: issuerFetch(env) }),
  );
  servers.set(env.AUTH_ISSUER, metadata);
  return metadata;
}
function keys(env: OIDCEnv, url: string) {
  let result = keySets.get(url);
  if (!result) {
    result = createRemoteJWKSet(new URL(url), { [customFetch]: issuerFetch(env) });
    keySets.set(url, result);
  }
  return result;
}
async function encryptionKey(env: OIDCEnv): Promise<Uint8Array> {
  validateEnv(env);
  return new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(env.SESSION_SECRET)));
}
function cookieValue(request: Request, name: string): string | undefined {
  return request.headers
    .get('cookie')
    ?.split(';')
    .map((s) => s.trim())
    .find((s) => s.startsWith(`${name}=`))
    ?.slice(name.length + 1);
}
function cookie(name: string, value: string, maxAge: number): string {
  return `${name}=${value}; Path=/; Secure; HttpOnly; SameSite=Lax; Max-Age=${maxAge}`;
}
async function seal(env: OIDCEnv, payload: Record<string, unknown>, seconds: number): Promise<string> {
  return new EncryptJWT(payload)
    .setProtectedHeader({ alg: 'dir', enc: 'A256GCM' })
    .setIssuer(env.AUTH_ISSUER)
    .setAudience(`${env.PUBLIC_ORIGIN}:${env.OIDC_CLIENT_ID}`)
    .setIssuedAt()
    .setExpirationTime(`${seconds}s`)
    .encrypt(await encryptionKey(env));
}
async function unseal(env: OIDCEnv, token: string) {
  return (
    await jwtDecrypt(token, await encryptionKey(env), {
      issuer: env.AUTH_ISSUER,
      audience: `${env.PUBLIC_ORIGIN}:${env.OIDC_CLIENT_ID}`,
      keyManagementAlgorithms: ['dir'],
      contentEncryptionAlgorithms: ['A256GCM'],
    })
  ).payload;
}
/** Only local paths can be used as the post-login destination. */
export function returnPath(value: string | null): string {
  if (!value || !value.startsWith('/') || value.startsWith('//') || value.includes('\\') || /\p{Cc}|\s/u.test(value))
    return '/';
  const target = new URL(value, 'https://return.invalid');
  Iif (target.origin !== 'https://return.invalid') return '/';
  return target.pathname + target.search + target.hash;
}
/** Read only the relying party's encrypted, host-only session. Invalid or expired cookies are signed out. */
export async function readSession(request: Request, env: OIDCEnv): Promise<LoginSession | null> {
  if (new URL(request.url).origin !== env.PUBLIC_ORIGIN) return null;
  const token = cookieValue(request, SESSION_COOKIE);
  if (!token) return null;
  try {
    const data = await unseal(env, token);
    Iif (typeof data.subject !== 'string' || typeof data.accessToken !== 'string' || typeof data.exp !== 'number')
      return null;
    return {
      identity: {
        issuer: env.AUTH_ISSUER,
        sub: data.subject,
        name: typeof data.name === 'string' ? data.name : 'Player',
        anonymous: data.anonymous === true,
        passkeys: typeof data.passkeys === 'number' ? data.passkeys : 0,
        linkedGuests: Array.isArray(data.linkedGuests) ? (data.linkedGuests as string[]) : [],
      },
      accessToken: data.accessToken,
      expiresAt: data.exp * 1000,
    };
  } catch {
    return null;
  }
}
function identityFromClaims(
  claims: { iss?: string; sub?: string; name?: unknown; [key: string]: unknown },
  issuer: string,
): Identity {
  Iif (claims.iss !== issuer || !claims.sub) throw new Error('Invalid identity');
  return {
    issuer,
    sub: claims.sub,
    name: typeof claims.name === 'string' ? claims.name : 'Player',
    anonymous: claims[CLAIM] === true,
    passkeys:
      typeof claims['https://be-music.app/passkeys'] === 'number'
        ? (claims['https://be-music.app/passkeys'] as number)
        : 0,
    linkedGuests: Array.isArray(claims['https://be-music.app/linked-guests'])
      ? (claims['https://be-music.app/linked-guests'] as unknown[]).filter((v): v is string => typeof v === 'string')
      : [],
  };
}
/** Verify a resource-bound access token, never an ID token, before accepting cross-service API requests. */
export async function verifyAccess(request: Request, env: OIDCEnv): Promise<Identity | null> {
  if (!/^Bearer /i.test(request.headers.get('authorization') ?? '')) return null;
  try {
    const claims = await oauth.validateJwtAccessToken(await server(env), request, env.API_AUDIENCE, {
      [oauth.customFetch]: issuerFetch(env),
    });
    const scopes = typeof claims.scope === 'string' ? claims.scope.split(' ') : [];
    Iif (!scopes.includes('be-music:api')) return null;
    return identityFromClaims(claims, env.AUTH_ISSUER);
  } catch {
    return null;
  }
}
/** Handle local sign-in/callback/session/sign-out routes. No token is returned to browser JavaScript. */
export async function handleAuth(request: Request, env: OIDCEnv): Promise<Response | null> {
  const url = new URL(request.url);
  if (!url.pathname.startsWith('/api/auth/')) {
    // Only top-level browser documents can start SSO; never redirect APIs, embeds, assets or prefetches.
    if (
      url.origin !== env.PUBLIC_ORIGIN ||
      request.method !== 'GET' ||
      url.pathname.startsWith('/api/') ||
      request.headers.get('sec-fetch-mode') !== 'navigate' ||
      request.headers.get('sec-fetch-dest') !== 'document' ||
      !request.headers.get('accept')?.includes('text/html') ||
      request.headers.has('purpose') ||
      request.headers.has('sec-purpose') ||
      cookieValue(request, SSO_CHECK_COOKIE) ||
      (await readSession(request, env))
    )
      return null;
    const start = new URL('/api/auth/sign-in', env.PUBLIC_ORIGIN);
    start.searchParams.set('automatic', '1');
    start.searchParams.set('returnTo', url.pathname + url.search);
    try {
      return await handleAuth(new Request(start), env);
    } catch {
      // An unavailable identity provider must not prevent browsing public pages.
      return null;
    }
  }
  validateEnv(env);
  if (url.origin !== env.PUBLIC_ORIGIN) return new Response('Unknown origin', { status: 400 });
  const headers = new Headers({ 'cache-control': 'private, no-store', 'referrer-policy': 'no-referrer' });
  const reply = (body: unknown, status = 200) => {
    headers.set('content-type', 'application/json');
    return new Response(JSON.stringify(body), { status, headers });
  };
  Iif (url.pathname === '/api/auth/get-session' && request.method === 'GET') {
    const session = await readSession(request, env);
    return reply(
      session
        ? {
            user: { id: session.identity.sub, name: session.identity.name, isAnonymous: session.identity.anonymous },
            session: { expiresAt: new Date(session.expiresAt).toISOString() },
          }
        : null,
    );
  }
  if (url.pathname === '/api/auth/sign-out' && request.method === 'POST') {
    if (request.headers.get('origin') !== env.PUBLIC_ORIGIN) return reply({ error: 'invalid-origin' }, 403);
    headers.append('set-cookie', cookie(SESSION_COOKIE, '', 0));
    headers.append('set-cookie', cookie(TRANSACTION_COOKIE, '', 0));
    // Respect an explicit app sign-out until the user starts sign-in again.
    headers.append('set-cookie', cookie(SSO_CHECK_COOKIE, 'signed-out', 86400));
    // Native forms can return to a local page without requiring browser JavaScript.
    if (url.searchParams.has('returnTo')) {
      headers.set('location', returnPath(url.searchParams.get('returnTo')));
      return new Response(null, { status: 303, headers });
    }
    return reply({ success: true });
  }
  Iif (url.pathname === '/api/auth/manage' && request.method === 'GET') {
    headers.set('location', new URL('/account', env.AUTH_ISSUER).href);
    return new Response(null, { status: 302, headers });
  }
  if (url.pathname === '/api/auth/sign-in' && request.method === 'GET') {
    const automatic = url.searchParams.get('automatic') === '1';
    const as = await server(env);
    const state = oauth.generateRandomState();
    const nonce = oauth.generateRandomNonce();
    const verifier = oauth.generateRandomCodeVerifier();
    const target = new URL(as.authorization_endpoint!);
    target.search = new URLSearchParams({
      client_id: env.OIDC_CLIENT_ID,
      redirect_uri: `${env.PUBLIC_ORIGIN}/api/auth/callback`,
      response_type: 'code',
      scope: 'openid profile be-music:api',
      resource: env.API_AUDIENCE,
      state,
      nonce,
      code_challenge: await oauth.calculatePKCECodeChallenge(verifier),
      code_challenge_method: 'S256',
    }).toString();
    if (automatic) target.searchParams.set('prompt', 'none');
    else applySocialSignInMethod(target, url.searchParams.get('method'));
    // Back off after a failed silent attempt; also prevent a navigation loop if the provider is unavailable.
    headers.append('set-cookie', cookie(SSO_CHECK_COOKIE, 'checked', 60));
    headers.append(
      'set-cookie',
      cookie(
        TRANSACTION_COOKIE,
        await seal(
          env,
          { state, nonce, verifier, automatic, returnTo: returnPath(url.searchParams.get('returnTo')) },
          600,
        ),
        600,
      ),
    );
    headers.set('location', target.href);
    return new Response(null, { status: 302, headers });
  }
  Eif (url.pathname === '/api/auth/callback' && request.method === 'GET') {
    headers.append('set-cookie', cookie(TRANSACTION_COOKIE, '', 0));
    try {
      const transaction = await unseal(env, cookieValue(request, TRANSACTION_COOKIE) ?? '');
      Iif (
        typeof transaction.state !== 'string' ||
        typeof transaction.nonce !== 'string' ||
        typeof transaction.verifier !== 'string'
      )
        throw new Error('Invalid transaction');
      const as = await server(env);
      const client = {
        client_id: env.OIDC_CLIENT_ID,
        token_endpoint_auth_method: 'none',
        id_token_signed_response_alg: 'RS256',
      };
      let params: URLSearchParams;
      try {
        params = oauth.validateAuthResponse(as, client, url, transaction.state);
      } catch (error) {
        if (
          transaction.automatic === true &&
          error instanceof oauth.AuthorizationResponseError &&
          ['login_required', 'interaction_required', 'consent_required', 'account_selection_required'].includes(
            error.error,
          )
        ) {
          headers.set('location', returnPath(typeof transaction.returnTo === 'string' ? transaction.returnTo : null));
          return new Response(null, { status: 302, headers });
        }
        throw error;
      }
      const tokenResponse = await oauth.authorizationCodeGrantRequest(
        as,
        client,
        oauth.None(),
        params,
        `${env.PUBLIC_ORIGIN}/api/auth/callback`,
        transaction.verifier,
        { additionalParameters: { resource: env.API_AUDIENCE }, [oauth.customFetch]: issuerFetch(env) },
      );
      const tokens = await oauth.processAuthorizationCodeResponse(as, client, tokenResponse, {
        expectedNonce: transaction.nonce,
        requireIdToken: true,
      });
      Iif (!tokens.id_token || !as.jwks_uri) throw new Error('Missing identity token');
      const verified = await jwtVerify(tokens.id_token, keys(env, as.jwks_uri), {
        issuer: env.AUTH_ISSUER,
        audience: env.OIDC_CLIENT_ID,
        algorithms: ['RS256'],
      });
      const identity = identityFromClaims(verified.payload, env.AUTH_ISSUER);
      const lifetime = Math.min(
        600,
        typeof tokens.expires_in === 'number' ? tokens.expires_in : 600,
        (verified.payload.exp ?? 0) - Math.floor(Date.now() / 1000),
      );
      Iif (lifetime <= 0) throw new Error('Expired identity');
      const accessRequest = new Request(`${env.API_AUDIENCE}/`, {
        headers: { authorization: `Bearer ${tokens.access_token}` },
      });
      const access = await verifyAccess(accessRequest, env);
      Iif (!access || access.sub !== identity.sub) throw new Error('Invalid resource token');
      headers.append(
        'set-cookie',
        cookie(
          SESSION_COOKIE,
          await seal(
            env,
            {
              subject: identity.sub,
              name: identity.name,
              anonymous: identity.anonymous,
              linkedGuests: identity.linkedGuests,
              passkeys: identity.passkeys,
              accessToken: tokens.access_token,
            },
            lifetime,
          ),
          lifetime,
        ),
      );
      headers.append('set-cookie', cookie(SSO_CHECK_COOKIE, '', 0));
      headers.set('location', returnPath(typeof transaction.returnTo === 'string' ? transaction.returnTo : null));
      return new Response(null, { status: 302, headers });
    } catch {
      return reply({ error: 'sign-in-failed', message: 'Sign-in could not be verified. Start again.' }, 400);
    }
  }
  return reply({ error: 'sign-in-required', signInURL: '/api/auth/sign-in' }, 401);
}