Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions github/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ type Runtime = ReturnType<

const REQUESTED_SCOPES = "repo read:org read:user";

/**
* Key material that seals the OAuth `state` and the authorization `code`.
*
* Unset, the runtime falls back to plain base64url JSON and the code carries
* the GitHub access token in the clear, so this throws rather than silently
* downgrading. Set it with `bunx wrangler secret put OAUTH_STATE_SECRET`
* before deploying a build that reads it.
*
* Read per-call for the same reason as the credentials below.
*/
function getStateSecret(): string {
const stateSecret = process.env.OAUTH_STATE_SECRET || "";
if (!stateSecret) {
throw new Error(
"OAuth state secret not configured. " +
"Set the OAUTH_STATE_SECRET environment variable.",
);
}
return stateSecret;
}

/**
* Lazily read OAuth credentials — on Workers, process.env isn't populated
* at module-init time, so we must resolve per-call.
Expand Down Expand Up @@ -69,6 +90,7 @@ async function getRuntime(): Promise<Runtime> {
mode: "PKCE",
authorizationServer: "https://github.com",
allowedRedirectHosts: [...ALLOWED_REDIRECT_HOST_SUFFIXES],
stateSecret: getStateSecret(),

authorizationUrl: (callbackUrl) => {
const clientId = process.env.GITHUB_CLIENT_ID || "";
Expand Down
Loading