generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
512 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"preset": "ts-jest", | ||
"testEnvironment": "node", | ||
"roots": ["./tests"], | ||
"coveragePathIgnorePatterns": ["node_modules", "mocks"], | ||
"collectCoverage": true, | ||
"coverageReporters": ["json", "lcov", "text", "clover", "json-summary"], | ||
"reporters": ["default", "jest-junit"], | ||
"coverageDirectory": "coverage" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const UBIQUIBOT_KERNEL_PUBLIC_KEY = ""; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { EmitterWebhookEvent as WebhookEvent, EmitterWebhookEventName as WebhookEventName } from "@octokit/webhooks"; | ||
import { Octokit } from "@octokit/rest"; | ||
|
||
export interface Context<TConfig = unknown, TEnv = unknown, TSupportedEvents extends WebhookEventName = WebhookEventName> { | ||
eventName: TSupportedEvents; | ||
payload: { | ||
[K in TSupportedEvents]: K extends WebhookEventName ? WebhookEvent<K> : never; | ||
}[TSupportedEvents]["payload"]; | ||
octokit: InstanceType<typeof Octokit>; | ||
config: TConfig; | ||
env: TEnv; | ||
logger: { | ||
fatal: (message: unknown, ...optionalParams: unknown[]) => void; | ||
error: (message: unknown, ...optionalParams: unknown[]) => void; | ||
warn: (message: unknown, ...optionalParams: unknown[]) => void; | ||
info: (message: unknown, ...optionalParams: unknown[]) => void; | ||
debug: (message: unknown, ...optionalParams: unknown[]) => void; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { createPlugin } from "./server"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Hono } from "hono"; | ||
import { HTTPException } from "hono/http-exception"; | ||
import { Context } from "./context"; | ||
import { customOctokit } from "../github/github-client"; | ||
import { EmitterWebhookEventName as WebhookEventName } from "@octokit/webhooks"; | ||
import { verifySignature } from "./signature"; | ||
import { UBIQUIBOT_KERNEL_PUBLIC_KEY } from "./constants"; | ||
|
||
interface Options { | ||
ubiquibotKernelPublicKey?: string; | ||
logger?: { | ||
fatal?: (message: unknown, ...optionalParams: unknown[]) => void; | ||
error?: (message: unknown, ...optionalParams: unknown[]) => void; | ||
warn?: (message: unknown, ...optionalParams: unknown[]) => void; | ||
info?: (message: unknown, ...optionalParams: unknown[]) => void; | ||
debug?: (message: unknown, ...optionalParams: unknown[]) => void; | ||
}; | ||
} | ||
|
||
export async function createPlugin<TConfig = unknown, TEnv = unknown, TSupportedEvents extends WebhookEventName = WebhookEventName>( | ||
handler: (context: Context<TConfig, TEnv, TSupportedEvents>) => Promise<Record<string, unknown> | undefined>, | ||
options?: Options | ||
) { | ||
const app = new Hono(); | ||
|
||
app.post("/", async (ctx) => { | ||
if (ctx.req.header("content-type") !== "application/json") { | ||
throw new HTTPException(400, { message: "Content-Type must be application/json" }); | ||
} | ||
|
||
const payload = await ctx.req.json(); | ||
const signature = payload.signature; | ||
delete payload.signature; | ||
if (!(await verifySignature(options?.ubiquibotKernelPublicKey || UBIQUIBOT_KERNEL_PUBLIC_KEY, payload, signature))) { | ||
console.error("Invalid signature"); | ||
throw new HTTPException(400, { message: "Invalid signature" }); | ||
} | ||
|
||
const context: Context<TConfig, TEnv, TSupportedEvents> = { | ||
eventName: payload.eventName, | ||
payload: payload.payload, | ||
octokit: new customOctokit({ auth: payload.authToken }), | ||
config: payload.settings as TConfig, | ||
env: ctx.env as TEnv, | ||
logger: { | ||
fatal: options?.logger?.fatal || console.error, | ||
error: options?.logger?.error || console.error, | ||
warn: options?.logger?.warn || console.warn, | ||
info: options?.logger?.info || console.info, | ||
debug: options?.logger?.debug || console.debug, | ||
}, | ||
}; | ||
|
||
try { | ||
console.log("CALLING HANDLER"); | ||
const result = await handler(context); | ||
return ctx.json({ stateId: payload.stateId, output: result }); | ||
} catch (error) { | ||
console.error(error); | ||
throw new HTTPException(500, { message: "Unexpected error" }); | ||
} | ||
}); | ||
|
||
return app; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export async function verifySignature(publicKeyPem: string, payload: unknown, signature: string) { | ||
const pemContents = publicKeyPem.replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", "").trim(); | ||
const binaryDer = Uint8Array.from(atob(pemContents), (c) => c.charCodeAt(0)); | ||
|
||
const publicKey = await crypto.subtle.importKey( | ||
"spki", | ||
binaryDer.buffer, | ||
{ | ||
name: "RSASSA-PKCS1-v1_5", | ||
hash: "SHA-256", | ||
}, | ||
true, | ||
["verify"] | ||
); | ||
|
||
const signatureArray = Uint8Array.from(atob(signature), (c) => c.charCodeAt(0)); | ||
const dataArray = new TextEncoder().encode(JSON.stringify(payload)); | ||
|
||
return await crypto.subtle.verify("RSASSA-PKCS1-v1_5", publicKey, signatureArray, dataArray); | ||
} |
Oops, something went wrong.