~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
125 lines · 3.9 KB Raw History
// What the server puts in each page (spec/OAUTH.md, "The authorization
// page", and spec/PASSKEYS.md, "The page's options" and "The passkey
// page"): `__authorizeData`, `__sessions` and `__passkeyOptions`,
// `__passkeyData`, or `__errorData`, read from `window`; and
// what the views make of them.

import { words } from './words';

export type Account = {
	did: string;
	handle: string;
	email?: string;
	name?: string;
	picture?: string;
};

export type Session = { account: Account; loginRequired: boolean };

// An account the page knows: one of `__sessions`, or one signed in on this
// page since it loaded, with the ephemeral token its sign-in gave if any.
export type Known = Session & { token?: string };

export type ClientMetadata = {
	client_name?: unknown;
	logo_uri?: unknown;
	tos_uri?: unknown;
	policy_uri?: unknown;
};

export type AuthorizeData = {
	requestUri: string;
	clientId: string;
	clientMetadata?: ClientMetadata;
	scope?: string;
	loginHint?: string;
	selectedDid?: string;
	// Each set the scope includes, by its NSID: its document's `defs.main`
	// as published, unchecked (spec/OAUTH-PERMISSION-SETS.md).
	permissionSets?: Record<string, unknown>;
	// With a `space` permission (spec/OAUTH-SCOPES.md, "On the authorization
	// page"): each space type's declaration, by its NSID, its `defs.main`
	// as published; and the handle of each owner a `space:` token names
	// whose handle verifies, by its DID. Unchecked.
	spaces?: Record<string, unknown>;
	spaceHandles?: Record<string, unknown>;
};

export type ErrorData = { error: string; error_description: string };

// The passkey page's: the account the link adds a passkey to, the link's
// name for it, when the link ends, and the registration's options.
export type PasskeyData = {
	account: Account;
	name: string;
	expiresAt: string;
	options: PublicKeyCredentialCreationOptionsJSON;
};

declare global {
	interface Window {
		__authorizeData?: AuthorizeData;
		__sessions?: Session[];
		__passkeyOptions?: PublicKeyCredentialRequestOptionsJSON;
		__passkeyData?: PasskeyData;
		__errorData?: ErrorData;
	}
}

// What the server says, if this is the authorization page.
export const authorizeData: AuthorizeData | undefined = window.__authorizeData;
export const initialSessions: Session[] = window.__sessions ?? [];
// The options of the challenge the page gave its device, for the autofill.
export const passkeyOptions: PublicKeyCredentialRequestOptionsJSON | undefined = window.__passkeyOptions;
// What the server says, if this is the passkey page.
export const passkeyData: PasskeyData | undefined = window.__passkeyData;
export const errorData: ErrorData | undefined = window.__errorData;

// Whether `account` is the one a `loginHint` names: by its handle, DID or
// email, handle and email without regard to case.
export function isHinted(account: Account, hint: string): boolean {
	const lower = hint.toLowerCase();
	return (
		account.did === hint ||
		account.handle.toLowerCase() === lower ||
		(!!account.email && account.email.toLowerCase() === lower)
	);
}

// Accounts in order of handle.
export const byHandle = (known: Known[]): Known[] =>
	[...known].sort((a, b) =>
		a.account.handle < b.account.handle ? -1 : a.account.handle > b.account.handle ? 1 : 0
	);

// The client as the consent view shows it.
export type Client = {
	loopback: boolean;
	name: string;
	host: string;
	logo?: string;
	terms?: string;
	privacy?: string;
};

const text = (v: unknown): string | undefined => (typeof v === 'string' && v ? v : undefined);

export function clientOf(data: AuthorizeData): Client {
	let url: URL | null = null;
	try {
		url = new URL(data.clientId);
	} catch {
		url = null;
	}
	const host = url?.host || data.clientId;
	const loopback = url?.protocol === 'http:' && url.hostname === 'localhost';
	const m = data.clientMetadata ?? {};
	return {
		loopback,
		name: loopback ? words.loopbackClient : (text(m.client_name) ?? host),
		host,
		logo: text(m.logo_uri),
		terms: text(m.tos_uri),
		privacy: text(m.policy_uri)
	};
}