|
| 1 | +import { createCookieSessionStorage } from "@remix-run/node"; |
| 2 | +import { randomBytes } from "crypto"; |
| 3 | +import { env } from "../env.server"; |
| 4 | +import { logger } from "./logger.server"; |
| 5 | + |
| 6 | +const sessionStorage = createCookieSessionStorage({ |
| 7 | + cookie: { |
| 8 | + name: "__github_app_install", |
| 9 | + httpOnly: true, |
| 10 | + maxAge: 60 * 60, // 1 hour |
| 11 | + path: "/", |
| 12 | + sameSite: "lax", |
| 13 | + secrets: [env.SESSION_SECRET], |
| 14 | + secure: env.NODE_ENV === "production", |
| 15 | + }, |
| 16 | +}); |
| 17 | + |
| 18 | +/** |
| 19 | + * Creates a secure session for GitHub App installation with organization tracking |
| 20 | + */ |
| 21 | +export async function createGitHubAppInstallSession( |
| 22 | + organizationId: string, |
| 23 | + redirectTo: string |
| 24 | +): Promise<{ url: string; cookieHeader: string }> { |
| 25 | + const state = randomBytes(32).toString("hex"); |
| 26 | + |
| 27 | + const session = await sessionStorage.getSession(); |
| 28 | + session.set("organizationId", organizationId); |
| 29 | + session.set("redirectTo", redirectTo); |
| 30 | + session.set("state", state); |
| 31 | + session.set("createdAt", Date.now()); |
| 32 | + |
| 33 | + const githubAppSlug = env.GITHUB_APP_SLUG; |
| 34 | + |
| 35 | + // the state query param gets passed through to the installation callback |
| 36 | + const url = `https://github.com/apps/${githubAppSlug}/installations/new?state=${state}`; |
| 37 | + |
| 38 | + const cookieHeader = await sessionStorage.commitSession(session); |
| 39 | + |
| 40 | + return { url, cookieHeader }; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Validates and retrieves the GitHub App installation session |
| 45 | + */ |
| 46 | +export async function validateGitHubAppInstallSession( |
| 47 | + cookieHeader: string | null, |
| 48 | + state: string |
| 49 | +): Promise< |
| 50 | + { valid: true; organizationId: string; redirectTo: string } | { valid: false; error?: string } |
| 51 | +> { |
| 52 | + if (!cookieHeader) { |
| 53 | + return { |
| 54 | + valid: false, |
| 55 | + error: "No installation session cookie found", |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + const session = await sessionStorage.getSession(cookieHeader); |
| 60 | + |
| 61 | + const sessionState = session.get("state"); |
| 62 | + const organizationId = session.get("organizationId"); |
| 63 | + const redirectTo = session.get("redirectTo"); |
| 64 | + const createdAt = session.get("createdAt"); |
| 65 | + |
| 66 | + if (!sessionState || !organizationId || !createdAt || !redirectTo) { |
| 67 | + logger.warn("GitHub App installation session missing required fields", { |
| 68 | + hasState: !!sessionState, |
| 69 | + hasOrgId: !!organizationId, |
| 70 | + hasCreatedAt: !!createdAt, |
| 71 | + hasRedirectTo: !!redirectTo, |
| 72 | + }); |
| 73 | + |
| 74 | + return { |
| 75 | + valid: false, |
| 76 | + error: "invalid_session_data", |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + if (sessionState !== state) { |
| 81 | + logger.warn("GitHub App installation state mismatch", { |
| 82 | + expectedState: sessionState, |
| 83 | + receivedState: state, |
| 84 | + }); |
| 85 | + return { |
| 86 | + valid: false, |
| 87 | + error: "state_mismatch", |
| 88 | + }; |
| 89 | + } |
| 90 | + |
| 91 | + const expirationTime = createdAt + 60 * 60 * 1000; |
| 92 | + if (Date.now() > expirationTime) { |
| 93 | + logger.warn("GitHub App installation session expired", { |
| 94 | + createdAt: new Date(createdAt), |
| 95 | + now: new Date(), |
| 96 | + }); |
| 97 | + return { |
| 98 | + valid: false, |
| 99 | + error: "session_expired", |
| 100 | + }; |
| 101 | + } |
| 102 | + |
| 103 | + return { |
| 104 | + valid: true, |
| 105 | + organizationId, |
| 106 | + redirectTo, |
| 107 | + }; |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Destroys the GitHub App installation cookie session |
| 112 | + */ |
| 113 | +export async function destroyGitHubAppInstallSession(cookieHeader: string | null): Promise<string> { |
| 114 | + if (!cookieHeader) { |
| 115 | + return ""; |
| 116 | + } |
| 117 | + |
| 118 | + const session = await sessionStorage.getSession(cookieHeader); |
| 119 | + return await sessionStorage.destroySession(session); |
| 120 | +} |
0 commit comments