-
Notifications
You must be signed in to change notification settings - Fork 81
feat(typescript): make verify a type-guard
#376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,18 @@ | ||
| import { timingSafeEqual } from "crypto"; | ||
| import { Buffer } from "buffer"; | ||
| import { EventTypesPayload } from "../generated/get-webhook-payload-type-from-event"; | ||
| import { sign } from "../sign/index"; | ||
|
|
||
| type WebhookEvents = Exclude< | ||
| keyof EventTypesPayload, | ||
| `${string}.${string}` | "errors" | "*" | ||
| >; | ||
|
|
||
| type GithubEvent<TName extends WebhookEvents = WebhookEvents> = Omit< | ||
| EventTypesPayload[TName], | ||
| "name" | ||
| > & { name: TName }; | ||
|
|
||
| const getAlgorithm = (signature: string) => { | ||
| return signature.startsWith("sha256=") ? "sha256" : "sha1"; | ||
| }; | ||
|
|
@@ -10,7 +21,7 @@ export function verify( | |
| secret: string, | ||
| eventPayload: object, | ||
| signature: string | ||
| ): boolean { | ||
| ): eventPayload is GithubEvent { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you explain how that helps exactly? I'm not familiar with that notion
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem - type guards allow you to define functions that narrow types based on if they return true or false. We actually now also have assertion guards which are the same except the function throws (they landed a few months ago, and very cool). So by making this a type-guard, you can do this: In the above, because
(it's actually a bit bigger, as it has a conditional check to say
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's the code in action, vs the current method. You'll see that the first use of
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perfect, I got it now, makes a lot of sense. Thank you for the thorough explanation! |
||
| if (!secret || !eventPayload || !signature) { | ||
| throw new TypeError( | ||
| "[@octokit/webhooks] secret, eventPayload & signature required" | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
woah I didn't know you can do the template literals with
stringtypes! Very coolThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, thats the nice hotness that landed with 4.1 - it only arrived about 2 weeks ago.
I'm actually very happy with this bit of code, because it's my first sensible reason to use it in production code, and it works like a treat 😍