Skip to content

Commit

Permalink
refactor: rename isValidGithubWebhook to isValidGitHubWebhook (#5)
Browse files Browse the repository at this point in the history
* refactor: rename `isValidGithubWebhook` to `isValidGitHubWebhook` with backwards compatibility

* chore: use renamed composable in playground

* refactor: rename GitHub test methods
  • Loading branch information
Yizack authored Dec 10, 2024
1 parent c14fa0b commit da5a829
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Validate a GitHub webhook in a server API route.

```js
export default defineEventHandler(async (event) => {
const isValidWebhook = await isValidGithubWebhook(event)
const isValidWebhook = await isValidGitHubWebhook(event)

if (!isValidWebhook) {
throw createError({ statusCode: 401, message: 'Unauthorized: webhook is not valid' })
Expand Down
2 changes: 1 addition & 1 deletion playground/server/api/webhooks/github.post.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default defineEventHandler(async (event) => {
const isValidWebhook = await isValidGithubWebhook(event)
const isValidWebhook = await isValidGitHubWebhook(event)

if (!isValidWebhook) throw createError({ statusCode: 401, message: 'Unauthorized: webhook is not valid' })

Expand Down
8 changes: 7 additions & 1 deletion src/runtime/server/lib/validators/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const GITHUB_SIGNATURE = 'X-Hub-Signature-256'.toLowerCase()
* @param event H3Event
* @returns {boolean} `true` if the webhook is valid, `false` otherwise
*/
export const isValidGithubWebhook = async (event: H3Event): Promise<boolean> => {
export const isValidGitHubWebhook = async (event: H3Event): Promise<boolean> => {
const config = ensureConfiguration('github', event)

const headers = getRequestHeaders(event)
Expand All @@ -25,3 +25,9 @@ export const isValidGithubWebhook = async (event: H3Event): Promise<boolean> =>
const computedHash = await computeSignature(config.secretKey, HMAC_SHA256, body)
return computedHash === webhookSignature
}

/**
* Alias for backwards compatibility
* @deprecated Use `isValidGitHubWebhook` instead
*/
export const isValidGithubWebhook = (event: H3Event) => isValidGitHubWebhook(event)
2 changes: 1 addition & 1 deletion test/events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { simulateDiscordEvent } from './simulations/discord'
export { simulateGithubEvent } from './simulations/github'
export { simulateGitHubEvent } from './simulations/github'
export { simulateHerokuEvent } from './simulations/heroku'
export { simulatePaddleEvent } from './simulations/paddle'
export { simulateTwitchEvent } from './simulations/twitch'
Expand Down
2 changes: 1 addition & 1 deletion test/module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('webhooks', () => {
})

it('valid GitHub webhook', async () => {
const response = await events.simulateGithubEvent()
const response = await events.simulateGitHubEvent()
expect(response).toStrictEqual(validWebhook)
})

Expand Down
2 changes: 1 addition & 1 deletion test/simulations/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import nuxtConfig from '../fixtures/basic/nuxt.config'
const body = 'testBody'
const secretKey = nuxtConfig.runtimeConfig?.webhook?.github?.secretKey

export const simulateGithubEvent = async () => {
export const simulateGitHubEvent = async () => {
const signature = await subtle.importKey('raw', encoder.encode(secretKey), HMAC_SHA256, false, ['sign'])
const hmac = await subtle.sign(HMAC_SHA256.name, signature, encoder.encode(body))
const computedHash = Buffer.from(hmac).toString('hex')
Expand Down

0 comments on commit da5a829

Please sign in to comment.