Functional Weave
Code in Python

auth.account-link@1.0.0

impl/typescript.ts

2,845 bytes · the TypeScript implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

import { normaliseEmail } from "./auth_normalise_email.ts";  ← from auth.normalise-email ^1.0.0 · built alongside by fune
import type { IdentityLink, LinkAccount, LinkAction, LinkDecision, LinkReason, OAuthIdentity } from "./auth_account_link_types.ts";

function decision(action: LinkAction, reason: LinkReason, accountId: string | null, email: string | null): LinkDecision {
  return { action, reason, accountId, email };
}

/**
 * What to do with an OAuth sign-in, given the accounts and links the app
 * already has. The rules run in a fixed order, so the same inputs always get
 * the same answer; see the README for the attack each rule stops.
 */
export function decideAccountLink(identity: OAuthIdentity, accounts: readonly LinkAccount[], links: readonly IdentityLink[]): LinkDecision {
  if (typeof identity !== "object" || identity === null) throw new TypeError("identity must be an OAuthIdentity");
  if (typeof identity.provider !== "string" || identity.provider === "") throw new Error("identity.provider must be a non-empty string");
  if (typeof identity.subject !== "string" || identity.subject === "") throw new Error("identity.subject must be a non-empty string");
  if (!Array.isArray(accounts)) throw new TypeError("accounts must be a list");
  if (!Array.isArray(links)) throw new TypeError("links must be a list");

  const email = identity.email === null || identity.email === undefined ? null : normaliseEmail(identity.email);

  // The provider's subject is the identity; once linked, the email no longer matters.
  const existing = links.find((l) => l.provider === identity.provider && l.subject === identity.subject);
  if (existing) {
    const known = accounts.some((a) => a.id === existing.accountId);
    return known
      ? decision("sign-in", "existing_link", existing.accountId, email)
      : decision("refuse", "linked_account_missing", existing.accountId, email);
  }

  if (email === null) return decision("refuse", "no_email", null, null);

  const matches = accounts.filter((a) => normaliseEmail(a.email) === email);
  if (matches.length === 0) {
    return identity.emailVerified === true
      ? decision("create", "new_account", null, email)
      : decision("require-verification", "unverified_new_email", null, email);
  }
  if (matches.length > 1) return decision("refuse", "ambiguous_email", null, email);

  const account = matches[0];
  if (identity.emailVerified !== true) return decision("refuse", "unverified_email", account.id, email);
  const clash = links.some((l) => l.accountId === account.id && l.provider === identity.provider && l.subject !== identity.subject);
  if (clash) return decision("refuse", "provider_already_linked", account.id, email);
  if (account.emailVerified !== true) return decision("require-verification", "account_email_unverified", account.id, email);
  return decision("link", "verified_email_match", account.id, email);
}