Skip to content

Commit

Permalink
fix(typescript): mark verify parameters as required (#373) thanks @…
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored Dec 5, 2020
1 parent c352451 commit fc4ddd3
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { WebhookEvents } from "./generated/get-webhook-payload-type-from-event";
// U holds the return value of `transform` function in Options
class Webhooks<T extends WebhookEvent = WebhookEvent, U = {}> {
public sign: (payload: string | object) => string;
public verify: (eventPayload?: object, signature?: string) => boolean;
public verify: (eventPayload: object, signature: string) => boolean;
public on: <E extends WebhookEvents>(
event: E | E[],
callback: HandlerFunction<E, U>
Expand Down
7 changes: 6 additions & 1 deletion src/middleware/verify-and-receive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ export function verifyAndReceive(
state: State,
event: WebhookEvent & { signature: string }
): any {
const matchesSignature = verify(state.secret, event.payload, event.signature);
// verify will validate that the secret is not undefined
const matchesSignature = verify(
state.secret!,
event.payload,
event.signature
);

if (!matchesSignature) {
const error = new Error(
Expand Down
6 changes: 3 additions & 3 deletions src/verify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ The `verify` method can be used as a standalone method.

```js
const { verify } = require('@octokit/webhooks')
const matchesSignature = verify(secret, eventData, signature)
const matchesSignature = verify(secret, eventPayload, signature)
// true or false
```

<table width="100%">
<tr>
<td>
<code>
options.secret
secret
</code>
<em>(String)</em>
</td>
Expand Down Expand Up @@ -51,6 +51,6 @@ const matchesSignature = verify(secret, eventData, signature)
</tr>
</table>

Returns `true` or `false`. Throws error if `secret, ``eventPayload` or `signature` not passed.
Returns `true` or `false`. Throws error if `secret`, `eventPayload` or `signature` not passed.

Back to [@octokit/webhooks README](..).
6 changes: 3 additions & 3 deletions src/verify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const getAlgorithm = (signature: string) => {
};

export function verify(
secret?: string,
eventPayload?: object,
signature?: string
secret: string,
eventPayload: object,
signature: string
): boolean {
if (!secret || !eventPayload || !signature) {
throw new TypeError(
Expand Down
4 changes: 4 additions & 0 deletions test/integration/verify-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@ const signatureSHA256 =
"sha256=4864d2759938a15468b5df9ade20bf161da9b4f737ea61794142f3484236bda3";

test("verify() without options throws", () => {
// @ts-expect-error
expect(() => verify()).toThrow();
});

test("verify(undefined, eventPayload) without secret throws", () => {
// @ts-expect-error
expect(() => verify.bind(null, undefined, eventPayload)()).toThrow();
});

test("verify(secret) without eventPayload throws", () => {
// @ts-expect-error
expect(() => verify.bind(null, secret)()).toThrow();
});

test("verify(secret, eventPayload) without options.signature throws", () => {
// @ts-expect-error
expect(() => verify.bind(null, secret, eventPayload)()).toThrow();
});

Expand Down

0 comments on commit fc4ddd3

Please sign in to comment.