~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
36 lines · 1.8 KB Raw History
// Where a sign-in comes back to: a path of the app's own, as `back` names
// it, resolved against the app's origin the way a browser resolves a
// redirect's Location (so a tab or newline it strips, or a `//` or `/\`,
// cannot name another host), and kept only where it stays on that origin;
// else the home page. And where a sign-in goes on to, as HUD answered the
// app's server asking it to start one (oauth.ts's `begin`).

/** The path (with query and fragment) of `back` on `origin`, or `/` where it leaves it. */
export function backPath(back: string | null, origin: string): string {
	if (!back) return '/';
	let url: URL;
	try {
		url = new URL(back, origin);
	} catch {
		return '/';
	}
	return url.origin === new URL(origin).origin ? url.pathname + url.search + url.hash : '/';
}

/**
 * Where the browser goes for the sign-in at `url`, HUD's authorization
 * request, as HUD answered it (`answer`, its status and Location; null
 * where it was not reached): where it went straight on, there (the
 * account's server); where it sent it back to the app's `callback`
 * refused, why, for the app's screen to say; where it answered its own
 * page, to that page.
 */
export function wentOn(answer: { status: number; location: string | null } | null, url: string, callback: string): { to: string } | { error: string } {
	if (!answer) return { error: 'Signing in could not reach the server; try again.' };
	const { status, location } = answer;
	if (status === 200) return { to: url };
	if (!location || status < 300 || status > 399) return { error: `Signing in could not start (HTTP ${status}); try again.` };
	const to = new URL(location, url);
	if (`${to.origin}${to.pathname}` !== callback) return { to: to.href };
	return { error: to.searchParams.get('error_description') || to.searchParams.get('error') || 'Signing in was refused.' };
}