All files / app-leaderboard/src auth.ts

70.21% Statements 33/47
69.23% Branches 27/39
80% Functions 4/5
75% Lines 30/40

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                          2x 1x 1x                             14x 14x     14x   10x       320x 10x                               10x       14x 14x 14x 14x       14x 14x 2x     2x 1x 1x 1x   1x       1x   14x 13x       461x 461x 1x 460x           461x                          
import { handleAuth, readSession, verifyAccess, type Identity, type OIDCEnv } from '@be-music/service-auth-client';
import { and, eq } from 'drizzle-orm';
import type { Env } from './env.ts';
import { loadSession as loadLegacySession } from './legacy-auth.ts';
import { openDatabase } from './db/client.ts';
import { oidcIdentity, user } from './db/schema.ts';
import { syncAccountProfile } from './profile-sync.ts';
import { claimAccount } from './accounts.ts';
import { createProfile } from './profiles.ts';
import { reassignScores } from './scores/reassign.ts';
export { createAuth, authSettings, hasSessionCookie, passOnSetCookies } from './legacy-auth.ts';
 
export function oidcEnv(env: Env): OIDCEnv {
  if (!env.AUTH_ISSUER || !env.PUBLIC_ORIGIN || !env.OIDC_CLIENT_ID || !env.SESSION_SECRET || !env.API_AUDIENCE)
    throw new Error('Incomplete OIDC configuration');
  return {
    AUTH_ISSUER: env.AUTH_ISSUER,
    PUBLIC_ORIGIN: env.PUBLIC_ORIGIN,
    OIDC_CLIENT_ID: env.OIDC_CLIENT_ID,
    SESSION_SECRET: env.SESSION_SECRET,
    API_AUDIENCE: env.API_AUDIENCE,
    ACCOUNTS: env.ACCOUNTS,
  };
}
export async function oidcAuth(request: Request, env: Env): Promise<Response | null> {
  return handleAuth(request, oidcEnv(env));
}
 
/** The identity mapping, not email, connects an issuer/subject to a local player. */
export async function syncIdentity(env: Env, identity: Identity): Promise<string> {
  const db = openDatabase(env.DB);
  let mapped = await db.query.oidcIdentity.findFirst({
    where: and(eq(oidcIdentity.issuer, identity.issuer), eq(oidcIdentity.subject, identity.sub)),
  });
  if (!mapped) {
    // Use one deterministic namespaced ID so concurrent first requests cannot create duplicate mirror accounts.
    const digest = await crypto.subtle.digest(
      'SHA-256',
      new TextEncoder().encode(JSON.stringify([identity.issuer, identity.sub])),
    );
    const id = `oidc_${Array.from(new Uint8Array(digest), (n) => n.toString(16).padStart(2, '0')).join('')}`;
    await db.batch([
      db
        .insert(user)
        .values({
          id,
          name: 'Player',
          email: `${id}@identity.invalid`,
          emailVerified: false,
          isAnonymous: identity.anonymous,
        })
        .onConflictDoNothing(),
      db
        .insert(oidcIdentity)
        .values({ issuer: identity.issuer, subject: identity.sub, userId: id })
        .onConflictDoNothing(),
    ]);
    mapped = await db.query.oidcIdentity.findFirst({
      where: and(eq(oidcIdentity.issuer, identity.issuer), eq(oidcIdentity.subject, identity.sub)),
    });
  }
  Iif (!mapped) throw new Error('Identity synchronization failed');
  const previous = await db.query.user.findFirst({ where: eq(user.id, mapped.userId) });
  if (previous?.isAnonymous && !identity.anonymous) await claimAccount(db, mapped.userId);
  await db
    .update(user)
    .set({ isAnonymous: identity.anonymous, passkeyCount: identity.passkeys ?? 0 })
    .where(eq(user.id, mapped.userId));
  await createProfile(db, mapped.userId, identity.anonymous);
  for (const subject of identity.linkedGuests ?? []) {
    const guest = await db.query.oidcIdentity.findFirst({
      where: and(eq(oidcIdentity.issuer, identity.issuer), eq(oidcIdentity.subject, subject)),
    });
    if (!guest || guest.userId === mapped.userId) continue;
    const account = await db.query.user.findFirst({ where: eq(user.id, guest.userId) });
    Iif (account?.isAnonymous !== true) continue;
    await reassignScores(db, guest.userId, mapped.userId);
    // Keep the mapping as a tombstone: an old guest token must never recreate the same account.
    await db
      .update(oidcIdentity)
      .set({ userId: mapped.userId })
      .where(and(eq(oidcIdentity.issuer, identity.issuer), eq(oidcIdentity.subject, subject)));
    await db.delete(user).where(eq(user.id, guest.userId));
  }
  await syncAccountProfile(env, identity.sub, mapped.userId);
  return mapped.userId;
}
 
export async function loadSession(env: Env, request: Request) {
  Eif (!env.AUTH_ISSUER) {
    if (!['localhost', '127.0.0.1', '[::1]'].includes(new URL(request.url).hostname))
      return { session: null, setCookies: [] };
    return loadLegacySession(env, request);
  }
  const settings = oidcEnv(env);
  const identity = request.headers.has('authorization')
    ? await verifyAccess(request, settings)
    : (await readSession(request, settings))?.identity;
  Iif (!identity) return { session: null, setCookies: [] };
  // A guest token that has been linked is no longer authorized, even while its JWT is otherwise valid.
  const db = openDatabase(env.DB);
  const old = await db.query.oidcIdentity.findFirst({
    where: and(eq(oidcIdentity.issuer, identity.issuer), eq(oidcIdentity.subject, identity.sub)),
  });
  if (old && identity.anonymous) {
    const account = await db.query.user.findFirst({ where: eq(user.id, old.userId) });
    if (account?.isAnonymous === false) return { session: null, setCookies: [] };
  }
  const userId = await syncIdentity(env, identity);
  return { session: { user: { id: userId, name: identity.name, isAnonymous: identity.anonymous } }, setCookies: [] };
}