Skip to content

Commit

Permalink
Expose array of secrets option at toplevel
Browse files Browse the repository at this point in the history
Add typechecking and integration tests

FIXES octokit#770
  • Loading branch information
nwf-msr committed Dec 5, 2022
1 parent a88e1a0 commit f5135c6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ class Webhooks<TTransformed = unknown> {
| EmitterWebhookEventWithSignature
) => Promise<void>;

constructor(options: Options<TTransformed> & { secret: string }) {
constructor(options: Options<TTransformed> & { secret: string | string[] }) {
if (!options || !options.secret) {
throw new Error("[@octokit/webhooks] options.secret required");
}

const state: State & { secret: string } = {
const state: State & { secret: string | string[] } = {
eventHandler: createEventHandler(options),
secret: options.secret,
hooks: {},
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface BaseWebhookEvent<TName extends WebhookEventName> {
}

export interface Options<TTransformed = unknown> {
secret?: string;
secret?: string | string[];
transform?: TransformMethod<TTransformed>;
log?: Partial<Logger>;
}
Expand Down
16 changes: 16 additions & 0 deletions test/integration/webhooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ describe("Webhooks", () => {
});
});

test("webhooks.verifyAndReceive({ ...event, signature }) with one of several secrets", async () => {
const secret1 = "mysecret";
const secret2 = "mysecret2";
const webhooks = new Webhooks({ secret: [secret1, secret2] });

await webhooks.verifyAndReceive({
id: "1",
name: "push",
payload: pushEventPayloadString,
signature: await sign(
{ secret: secret2, algorithm: "sha256" },
pushEventPayloadString
),
});
});

test("webhooks.verifyAndReceive(event) with incorrect signature", async () => {
const webhooks = new Webhooks({ secret: "mysecret" });

Expand Down
5 changes: 5 additions & 0 deletions test/typescript-validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export default async function () {
secret: "blah",
});

// Check that secret can be an array, too
new Webhooks({
secret: ["blah", "bleh"],
});

// Check all supported options
const webhooks = new Webhooks({
secret: "blah",
Expand Down

0 comments on commit f5135c6

Please sign in to comment.