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.

40 linesCodeBlameRaw
1import { ssoClient } from "@g1t/contracts";
2
3import { afterSso, bindingCookie, errorCookie, readBinding } from "../app/lib/sso";
4
5/**
6 * A workspace's identity provider posting its SAML response
7 * (`POST /auth/sso/<slug>/saml`). It comes from the provider's site, so it
8 * is answered here, before React Router, which refuses cross-site form
9 * posts; and it carries no session cookie (SameSite=Lax), so identity
10 * finds the session from the request g1t sent, which the response must
11 * answer. Either way the browser is sent on with a 303: on to where it was
12 * going, or back to the page that says what went wrong.
13 */
14export async function receiveSaml(env: Env, request: Request, slug: string): Promise<Response> {
15 // The binding cookie set when this browser set off: identity accepts the
16 // response only for the trip it binds. Spent either way.
17 const binding = readBinding(request.headers.get("cookie"));
18 const back = (message: string) => {
19 const headers = new Headers({ location: `/auth/sso/${slug}/saml`, "cache-control": "no-store" });
20 headers.append("set-cookie", errorCookie(message));
21 headers.append("set-cookie", bindingCookie(""));
22 return new Response(null, { status: 303, headers });
23 };
24 let samlResponse = "";
25 try {
26 const form = await request.formData();
27 samlResponse = String(form.get("SAMLResponse") ?? "");
28 } catch {
29 return back("The identity provider's response could not be read.");
30 }
31 if (!samlResponse) return back("The identity provider sent no SAML response.");
32 const finished = await ssoClient(env.IDENTITY)
33 .saml(slug, samlResponse, binding)
34 .catch(() => null);
35 if (!finished) return back("g1t could not check the identity provider's response just now. Try again in a minute.");
36 if (!finished.ok) return back(finished.error.message);
37 const headers = new Headers({ location: afterSso(finished.value), "cache-control": "no-store" });
38 headers.append("set-cookie", bindingCookie(""));
39 return new Response(null, { status: 303, headers });
40}