import { createHash } from "node:crypto"; import { type AppSlug, type Provider } from "./contracts.js"; import { getAppProfile } from "./app-profiles.js"; import { providerCatalog } from "./providers.js"; import { type StoredConsentReceipt } from "./store/index.js"; /** * What Heimdall tells a person before it holds a provider credential for an * app, and the page that lists what they agreed to. The wording is built from * the facts the code holds (the app, the provider, the scopes sent), never * from a promise the code does not keep. Any change to a line changes the * digest every later receipt carries, so a reader can tell which wording a * person saw. */ export interface ConsentAsk { appSlug: AppSlug; provider: Provider; /** The scopes Heimdall will send the provider. */ scopes: readonly string[]; /** The origin the person returns to afterwards. */ returnOrigin: string; } export const GRANTS_PATH = "/v1/me/grants"; function appName(slug: AppSlug): string { return getAppProfile(slug)?.displayName ?? slug; } /** The exact lines shown on the ask, apart from the two buttons. The digest is over these. */ export function consentLines(ask: ConsentAsk): string[] { const app = appName(ask.appSlug); const provider = providerCatalog[ask.provider].displayName; return [ `${app} asks Heimdall to hold a connection to your ${provider} account.`, `Heimdall will ask ${provider} for these permissions: ${ask.scopes.join(", ")}.`, `Heimdall keeps an encrypted refresh token and access token for this connection until you take it back or ${provider} ends it.`, `${app}'s server can ask Heimdall for a current access token and use it for those permissions. It never receives the refresh token.`, `If you say no, nothing is kept.`, `You can take this back at any time. Heimdall then deletes its copy of the tokens and stops handing out access tokens. The permission stays with ${provider} until you remove it in your ${provider} settings.`, `Afterwards you are returned to ${ask.returnOrigin}.`, ]; } export function consentDigest(ask: ConsentAsk): string { return createHash("sha256").update(consentLines(ask).join("\n"), "utf8").digest("hex"); } export function escapeHtml(value: string): string { return value.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """).replace(/'/g, "'"); } const pageStyle = ` :root { color-scheme: light dark; font-family: ui-sans-serif, system-ui, sans-serif; } body { margin: 0; min-height: 100vh; display: grid; place-items: center; background: #11161d; color: #f4f7fb; } main { width: min(38rem, calc(100vw - 2rem)); border-radius: 1rem; padding: 1.5rem; background: rgba(20, 27, 36, 0.96); box-shadow: 0 24px 60px rgba(0, 0, 0, 0.3); } h1 { margin: 0 0 0.75rem; font-size: 1.35rem; } p, li { line-height: 1.5; color: #d5dce6; } ul { padding-left: 1.2rem; } code { color: #d8eefe; } button { font: inherit; font-weight: 600; border: 0; border-radius: 999px; padding: 0.75rem 1rem; margin: 0.25rem 0.5rem 0.25rem 0; cursor: pointer; } button.go { color: #11161d; background: #d8eefe; } button.no { color: #f4f7fb; background: #2c3745; } section { border-top: 1px solid #2c3745; padding: 0.75rem 0; } small { color: #9aacbe; }`; function page(title: string, body: string): string { return `
${escapeHtml(grant.id)}; wording ${escapeHtml(grant.consentPageDigest.slice(0, 12))}`;
if (grant.status === "revoked") {
return ` ${escapeHtml(grant.revocationReceiptId ?? "")}You have not connected anything through Heimdall.
`; return page( "Your connections", `${escapeHtml(notice)}
\n` : ""}${cards}Taking a connection back deletes Heimdall's copy of the tokens. The app's next request for a token is refused. The permission stays with the provider until you remove it in the provider's settings.
` ); }