GeezerVison is showing github.com — Api: add endpoint for getting github app token · Verdagon/opencode@1c4fd7f
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: bun.lock +37-1Lines changed: 37 additions & 1 deletion Collapse file Copy file name to clipboardExpand all lines: infra/app.ts +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,6 +4,8 @@ export const domain = (() => { | 4 | 4 | return `${$app.stage}.dev.opencode.ai` | 5 | 5 | })() | 6 | 6 |
| | 7 | +const GITHUB_APP_ID = new sst.Secret("GITHUB_APP_ID") | | 8 | +const GITHUB_APP_PRIVATE_KEY = new sst.Secret("GITHUB_APP_PRIVATE_KEY") | 7 | 9 | const bucket = new sst.cloudflare.Bucket("Bucket") | 8 | 10 |
| 9 | 11 | export const api = new sst.cloudflare.Worker("Api", { | @@ -13,7 +15,7 @@ export const api = new sst.cloudflare.Worker("Api", { | 13 | 15 | WEB_DOMAIN: domain, | 14 | 16 | }, | 15 | 17 | url: true, | 16 | | - link: [bucket], | | 18 | + link: [bucket, GITHUB_APP_ID, GITHUB_APP_PRIVATE_KEY], | 17 | 19 | transform: { | 18 | 20 | worker: (args) => { | 19 | 21 | args.logpush = true | |
Collapse file Copy file name to clipboardExpand all lines: packages/function/package.json +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | "@cloudflare/workers-types": "4.20250522.0", | 9 | 9 | "typescript": "catalog:", | 10 | 10 | "@types/node": "catalog:" | | 11 | + }, | | 12 | + "dependencies": { | | 13 | + "@octokit/auth-app": "8.0.1", | | 14 | + "jose": "6.0.11" | 11 | 15 | } | 12 | 16 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/function/src/api.ts +40Lines changed: 40 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { DurableObject } from "cloudflare:workers" | 2 | 2 | import { randomUUID } from "node:crypto" | | 3 | +import { jwtVerify, createRemoteJWKSet } from "jose" | | 4 | +import { createAppAuth } from "@octokit/auth-app" | | 5 | +import { Resource } from "sst" | 3 | 6 |
| 4 | 7 | type Env = { | 5 | 8 | SYNC_SERVER: DurableObjectNamespace<SyncServer> | @@ -218,5 +221,42 @@ export default { | 218 | 221 | }, | 219 | 222 | ) | 220 | 223 | } | | 224 | + | | 225 | + if (request.method === "POST" && method === "exchange_github_app_token") { | | 226 | + const EXPECTED_AUDIENCE = "opencode-github-action" | | 227 | + const GITHUB_ISSUER = "https://token.actions.githubusercontent.com" | | 228 | + const JWKS_URL = `${GITHUB_ISSUER}/.well-known/jwks` | | 229 | + | | 230 | + // get Authorization header | | 231 | + const authHeader = request.headers.get("Authorization") | | 232 | + const token = authHeader?.replace(/^Bearer /, "") | | 233 | + if (!token) return new Response("Error: authorization header is required", { status: 401 }) | | 234 | + | | 235 | + // verify token | | 236 | + const JWKS = createRemoteJWKSet(new URL(JWKS_URL)) | | 237 | + try { | | 238 | + await jwtVerify(token, JWKS, { | | 239 | + issuer: GITHUB_ISSUER, | | 240 | + audience: EXPECTED_AUDIENCE, | | 241 | + }) | | 242 | + } catch (err) { | | 243 | + console.error("Token verification failed:", err) | | 244 | + return new Response(JSON.stringify({ error: "Invalid or expired token" }), { | | 245 | + status: 403, | | 246 | + headers: { "Content-Type": "application/json" }, | | 247 | + }) | | 248 | + } | | 249 | + | | 250 | + // Create app token | | 251 | + const auth = createAppAuth({ | | 252 | + appId: Resource.GITHUB_APP_ID.value, | | 253 | + privateKey: Resource.GITHUB_APP_PRIVATE_KEY.value, | | 254 | + }) | | 255 | + const appAuthentication = await auth({ type: "app" }) | | 256 | + | | 257 | + return new Response(JSON.stringify({ token: appAuthentication.token }), { | | 258 | + headers: { "Content-Type": "application/json" }, | | 259 | + }) | | 260 | + } | 221 | 261 | }, | 222 | 262 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/function/sst-env.d.ts +16-8Lines changed: 16 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | import "sst" | 7 | 7 | declare module "sst" { | 8 | 8 | export interface Resource { | 9 | | - Web: { | 10 | | - type: "sst.cloudflare.Astro" | 11 | | - url: string | | 9 | + "GITHUB_APP_ID": { | | 10 | + "type": "sst.sst.Secret" | | 11 | + "value": string | | 12 | + } | | 13 | + "GITHUB_APP_PRIVATE_KEY": { | | 14 | + "type": "sst.sst.Secret" | | 15 | + "value": string | | 16 | + } | | 17 | + "Web": { | | 18 | + "type": "sst.cloudflare.Astro" | | 19 | + "url": string | 12 | 20 | } | 13 | 21 | } | 14 | 22 | } | 15 | | -// cloudflare | 16 | | -import * as cloudflare from "@cloudflare/workers-types" | | 23 | +// cloudflare | | 24 | +import * as cloudflare from "@cloudflare/workers-types"; | 17 | 25 | declare module "sst" { | 18 | 26 | export interface Resource { | 19 | | - Api: cloudflare.Service | 20 | | - Bucket: cloudflare.R2Bucket | | 27 | + "Api": cloudflare.Service | | 28 | + "Bucket": cloudflare.R2Bucket | 21 | 29 | } | 22 | 30 | } | 23 | 31 |
| 24 | 32 | import "sst" | 25 | | -export {} | | 33 | +export {} |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/sst-env.d.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | /// <reference path="../../sst-env.d.ts" /> | 7 | 7 |
| 8 | 8 | import "sst" | 9 | | -export {} | | 9 | +export {} |
Collapse file Copy file name to clipboardExpand all lines: packages/web/sst-env.d.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | /// <reference path="../../sst-env.d.ts" /> | 7 | 7 |
| 8 | 8 | import "sst" | 9 | | -export {} | | 9 | +export {} |
Collapse file Copy file name to clipboardExpand all lines: sst-env.d.ts +17-9Lines changed: 17 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 |
| 6 | 6 | declare module "sst" { | 7 | 7 | export interface Resource { | 8 | | - Api: { | 9 | | - type: "sst.cloudflare.Worker" | 10 | | - url: string | | 8 | + "Api": { | | 9 | + "type": "sst.cloudflare.Worker" | | 10 | + "url": string | 11 | 11 | } | 12 | | - Bucket: { | 13 | | - type: "sst.cloudflare.Bucket" | | 12 | + "Bucket": { | | 13 | + "type": "sst.cloudflare.Bucket" | 14 | 14 | } | 15 | | - Web: { | 16 | | - type: "sst.cloudflare.Astro" | 17 | | - url: string | | 15 | + "GITHUB_APP_ID": { | | 16 | + "type": "sst.sst.Secret" | | 17 | + "value": string | | 18 | + } | | 19 | + "GITHUB_APP_PRIVATE_KEY": { | | 20 | + "type": "sst.sst.Secret" | | 21 | + "value": string | | 22 | + } | | 23 | + "Web": { | | 24 | + "type": "sst.cloudflare.Astro" | | 25 | + "url": string | 18 | 26 | } | 19 | 27 | } | 20 | 28 | } | 21 | 29 | /// <reference path="sst-env.d.ts" /> | 22 | 30 |
| 23 | 31 | import "sst" | 24 | | -export {} | | 32 | +export {} |
|