-
Notifications
You must be signed in to change notification settings - Fork 9
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
1 parent
7c31138
commit 32275ed
Showing
12 changed files
with
206 additions
and
8 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
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,5 @@ | ||
--- | ||
"basehub": patch | ||
--- | ||
|
||
auth webhooks |
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 |
---|---|---|
@@ -1,5 +1,11 @@ | ||
# basehub | ||
|
||
## 8.0.0-canary.41 | ||
|
||
### Patch Changes | ||
|
||
- auth webhooks | ||
|
||
## 8.0.0-canary.40 | ||
|
||
### Patch Changes | ||
|
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
"name": "basehub", | ||
"description": "A very fast Headless CMS.", | ||
"author": "JB <[email protected]>", | ||
"version": "8.0.0-canary.40", | ||
"version": "8.0.0-canary.41", | ||
"license": "MIT", | ||
"repository": "basehub-ai/basehub", | ||
"bugs": "https://github.com/basehub-ai/basehub/issues", | ||
|
@@ -26,6 +26,7 @@ | |
"src/next/toolbar", | ||
"src/react/pump", | ||
"src/events", | ||
"src/workflows", | ||
"react-svg.js", | ||
"react-svg.d.ts", | ||
"react-rich-text.js", | ||
|
@@ -38,6 +39,8 @@ | |
"api-transaction.d.ts", | ||
"events.js", | ||
"events.d.ts", | ||
"workflows.js", | ||
"workflows.d.ts", | ||
"search.js", | ||
"search.d.ts", | ||
"next-image.js", | ||
|
@@ -52,7 +55,7 @@ | |
"build:client": "tsup --config tsup-client.config.ts" | ||
}, | ||
"dependencies": { | ||
"@basehub/genql": "9.0.0-canary.9", | ||
"@basehub/genql": "9.0.0-canary.10", | ||
"@basehub/mutation-api-helpers": "2.0.7", | ||
"@radix-ui/react-slot": "^1.1.0", | ||
"@shikijs/transformers": "1.17.7", | ||
|
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 * from "./primitive"; |
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,135 @@ | ||
/* eslint-disable turbo/no-undeclared-env-vars */ | ||
import { | ||
// @ts-ignore | ||
Scalars, | ||
// @ts-ignore | ||
// eslint-disable-next-line import/no-unresolved | ||
} from "../schema"; | ||
import crypto from "crypto"; | ||
|
||
/* ------------------------------------------------------------------------------------------------- | ||
* Client | ||
* -----------------------------------------------------------------------------------------------*/ | ||
|
||
type KeysStartingWith<Obj, Prefix extends string> = { | ||
[K in keyof Obj]: K extends `${Prefix}${string}` ? K : never; | ||
}[keyof Obj]; | ||
|
||
type ExtractWorkflowKey<T extends string> = T extends `${infer Base}:${string}` | ||
? Base | ||
: T; | ||
|
||
// Get all event key types (bshb_event_*) | ||
export type WorkflowKeys = KeysStartingWith<Scalars, "bshb_workflow">; | ||
|
||
// Map from event key to its schema type | ||
type WorkflowSchemaMap = { | ||
// @ts-ignore | ||
[K in WorkflowKeys]: Scalars[`schema_${K}`]; | ||
}; | ||
|
||
export const authenticateWebhook = async < | ||
Key extends `${WorkflowKeys}:${string}`, | ||
>({ | ||
secret, | ||
body, | ||
signature, | ||
}: { | ||
/** | ||
* The body of the incoming webhook request | ||
* Can be: | ||
* - Parsed JSON from request.json() | ||
* - Raw string from request.text() | ||
* - ReadableStream from request.body | ||
*/ | ||
body: unknown; | ||
/** | ||
* The signature of the incoming webhook request—you get this via request.headers["x-basehub-webhook-signature"] | ||
* This should be a hex-encoded HMAC SHA-256 hash of the request body | ||
*/ | ||
signature: string; | ||
/** | ||
* The secret used for verifying the incoming webhook request—you get this via the BaseHub API | ||
* This secret should never be exposed in requests or responses | ||
*/ | ||
secret: Key; | ||
}): Promise< | ||
| { success: true; payload: WorkflowSchemaMap[ExtractWorkflowKey<Key>] } | ||
| { success: false; error: string } | ||
> => { | ||
try { | ||
// Handle different body types | ||
let rawBody: string; | ||
let parsedBody: unknown; | ||
|
||
if (body instanceof ReadableStream) { | ||
// Convert stream to text | ||
const reader = body.getReader(); | ||
const chunks = []; | ||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) break; | ||
chunks.push(value); | ||
} | ||
const bodyText = new TextDecoder().decode( | ||
new Uint8Array(chunks.flatMap((chunk) => Array.from(chunk))) | ||
); | ||
rawBody = bodyText; | ||
parsedBody = JSON.parse(bodyText); | ||
} else if (typeof body === "string") { | ||
rawBody = body; | ||
parsedBody = JSON.parse(body); | ||
} else { | ||
// Already parsed JSON | ||
rawBody = JSON.stringify(body); | ||
parsedBody = body; | ||
} | ||
|
||
if (typeof parsedBody !== "object" || parsedBody === null) { | ||
return { success: false, error: "Invalid body" }; | ||
} | ||
|
||
const encoder = new TextEncoder(); | ||
const bodyData = encoder.encode(rawBody); | ||
const secretData = encoder.encode(secret); | ||
|
||
const key = await crypto.subtle.importKey( | ||
"raw", | ||
secretData, | ||
{ name: "HMAC", hash: "SHA-256" }, | ||
false, | ||
["sign"] | ||
); | ||
|
||
const signed = await crypto.subtle.sign("HMAC", key, bodyData); | ||
const calculatedSignature = Array.from(new Uint8Array(signed)) | ||
.map((b) => b.toString(16).padStart(2, "0")) | ||
.join(""); | ||
|
||
if (signature.length !== calculatedSignature.length) { | ||
return { success: false, error: "Invalid signature" }; | ||
} | ||
|
||
let mismatch = 0; | ||
for (let i = 0; i < signature.length; i++) { | ||
mismatch |= signature.charCodeAt(i) ^ calculatedSignature.charCodeAt(i); | ||
} | ||
|
||
if (mismatch !== 0) { | ||
return { success: false, error: "Invalid signature" }; | ||
} | ||
|
||
return { | ||
success: true, | ||
payload: parsedBody as WorkflowSchemaMap[ExtractWorkflowKey<Key>], | ||
}; | ||
} catch (error) { | ||
return { | ||
success: false, | ||
error: | ||
error instanceof Error | ||
? error.message | ||
: "Signature verification failed", | ||
}; | ||
} | ||
}; |
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,2 @@ | ||
/* eslint-disable import/no-unresolved */ | ||
export * from "./dist/generated-client/workflows"; |
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,6 @@ | ||
throw new Error( | ||
`\`workflows\` not found. Make sure to run \`npx basehub\` in order to generate it. | ||
If the error persist, please raise an issue at https://github.com/basehub-ai/basehub | ||
` | ||
); |
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 |
---|---|---|
@@ -1,5 +1,12 @@ | ||
# playground | ||
|
||
## 0.0.164-canary.41 | ||
|
||
### Patch Changes | ||
|
||
- Updated dependencies | ||
- [email protected] | ||
|
||
## 0.0.164-canary.40 | ||
|
||
### Patch Changes | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.