Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

45 linesCodeBlameRaw
1import { data, redirect } from "react-router";
2
3import type { Route } from "./+types/auth-sso-callback";
4import { cookie, readCookie, stateMatches } from "../lib/github";
5import { page } from "../lib/meta";
6import { SSO_STATE_COOKIE, afterSso } from "../lib/sso";
7import { sso } from "../lib/sso.server";
8import { sessionTokenOf } from "../lib/session.server";
9import { SsoProblem } from "./auth-sso";
10
11export function meta({ params, ...args }: Route.MetaArgs) {
12 return page(args, { title: `Single sign-on · ${params.owner} · g1t` });
13}
14
15/**
16 * Where a workspace's provider returns over OpenID Connect or OAuth 2.0.
17 * The state must match this browser's cookie, and the session must be the
18 * one that set off; identity then spends the state once, exchanges the
19 * code, and links, admits and marks the session.
20 */
21export async function loader({ request, params }: Route.LoaderArgs) {
22 const slug = params.owner.toLowerCase();
23 const url = new URL(request.url);
24 const clear = cookie(SSO_STATE_COOKIE, "", 0);
25 const failed = (error: string) => data({ slug, error }, { headers: { "set-cookie": clear } });
26 const refused = url.searchParams.get("error");
27 if (refused) {
28 const said = url.searchParams.get("error_description")?.slice(0, 300);
29 return failed(refused === "access_denied" ? "The sign-in was cancelled at the identity provider." : `The identity provider refused the sign-in${said ? `: ${said}` : ` (${refused.slice(0, 60)})`}.`);
30 }
31 const code = url.searchParams.get("code");
32 const state = url.searchParams.get("state");
33 if (!code || !stateMatches(readCookie(request.headers.get("cookie"), SSO_STATE_COOKIE), state)) {
34 return failed("This sign-in did not start in this browser, or took too long. Start again.");
35 }
36 const token = sessionTokenOf(request);
37 if (!token) return failed("Sign in to g1t again, then start single sign-on again.");
38 const finished = await sso.finish({ slug, session_token: token, state: state!, code });
39 if (!finished.ok) return failed(finished.error.message);
40 throw redirect(afterSso(finished.value), { headers: { "set-cookie": clear } });
41}
42
43export default function SsoCallback({ loaderData }: Route.ComponentProps) {
44 return <SsoProblem slug={loaderData.slug} error={loaderData.error} />;
45}