Skip to content

Commit

Permalink
style: use double instead of single quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
screendriver committed Oct 9, 2022
1 parent b2caf17 commit 55315d5
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 64 deletions.
9 changes: 4 additions & 5 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"singleQuote": true,
"trailingComma": "all",
"printWidth": 120,
"tabWidth": 4,
"useTabs": true
"trailingComma": "all",
"printWidth": 120,
"tabWidth": 4,
"useTabs": true
}
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ The exported function needs a [http.IncomingMessage](https://nodejs.org/api/http
You can use it for example with [micro](https://github.com/zeit/micro) as follows:

```ts
import micro from 'micro';
import { verifySecret } from 'verify-github-webhook-secret';
import micro from "micro";
import { verifySecret } from "verify-github-webhook-secret";

const server = micro(async (req) => {
const valid = await verifySecret(req, 'my-secret');
return valid ? 'Allowed' : 'Not allowed';
const valid = await verifySecret(req, "my-secret");
return valid ? "Allowed" : "Not allowed";
});
```

Another way to call the function is directly with the HTTP body and the `x-hub-signature` HTTP header. This is useful in an scenario where you don't have an `IncomingMessage` like in some [serverless](https://en.wikipedia.org/wiki/Serverless_computing) environments.

```ts
import { verifySecret } from 'verify-github-webhook-secret';
import { verifySecret } from "verify-github-webhook-secret";

async function myFunc() {
const valid = await verifySecret('{"foo":"bar"}', 'my-secret', 'sha1=30a233839fe2ddd9233c49fd593e8f1aec68f553');
return valid ? 'Allowed' : 'Not allowed';
const valid = await verifySecret('{"foo":"bar"}', "my-secret", "sha1=30a233839fe2ddd9233c49fd593e8f1aec68f553");
return valid ? "Allowed" : "Not allowed";
}
```
6 changes: 3 additions & 3 deletions ava.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
extensions: ['ts'],
require: ['ts-node/register/transpile-only'],
extensions: ["ts"],
require: ["ts-node/register/transpile-only"],
environmentVariables: {
TS_NODE_PROJECT: 'tsconfig.base.json',
TS_NODE_PROJECT: "tsconfig.base.json",
},
};
16 changes: 8 additions & 8 deletions release.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';
"use strict";

module.exports = {
branches: ['main'],
branches: ["main"],
plugins: [
'@semantic-release/commit-analyzer',
'@semantic-release/release-notes-generator',
'@semantic-release/changelog',
'@semantic-release/npm',
'@semantic-release/git',
'@semantic-release/github',
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/changelog",
"@semantic-release/npm",
"@semantic-release/git",
"@semantic-release/github",
],
};
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IncomingMessage } from 'http';
import { text } from 'micro';
import { verify } from './verify';
import { IncomingMessage } from "http";
import { text } from "micro";
import { verify } from "./verify";

export async function verifySecret(req: IncomingMessage, secret: string): Promise<boolean>;
export async function verifySecret(body: string, secret: string, xHubSignature?: string | string[]): Promise<boolean>;
Expand All @@ -11,12 +11,12 @@ export async function verifySecret(
) {
let body: string;
let signature: string | string[] | undefined;
if (typeof reqOrBody === 'string') {
if (typeof reqOrBody === "string") {
body = reqOrBody;
signature = xHubSignature;
} else {
body = await text(reqOrBody);
signature = reqOrBody.headers['x-hub-signature'];
signature = reqOrBody.headers["x-hub-signature"];
}
return verify(body, secret, signature);
}
6 changes: 3 additions & 3 deletions src/verify.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { createHmac } from 'crypto';
import { createHmac } from "crypto";

export function verify(body: string, secret: string, signature?: string | string[]): boolean {
if (!signature) {
return false;
}
const hmac = createHmac('sha1', secret);
const hmac = createHmac("sha1", secret);
hmac.update(body);
const calculated = `sha1=${hmac.digest('hex')}`;
const calculated = `sha1=${hmac.digest("hex")}`;
return signature === calculated;
}
50 changes: 25 additions & 25 deletions test/integration/verify.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import test from 'ava';
import micro from 'micro';
import listen from 'test-listen';
import got from 'got';
import { verifySecret } from '../../src/index';
import test from "ava";
import micro from "micro";
import listen from "test-listen";
import got from "got";
import { verifySecret } from "../../src/index";

test('return "false" when "x-hub-signature" header is missing', async (t) => {
t.plan(1);
const server = micro(async (req) => {
const valid = await verifySecret(req, 'my-secret');
const valid = await verifySecret(req, "my-secret");
t.false(valid);
return '';
return "";
});
const url = await listen(server);
await got(url, {
method: 'POST',
method: "POST",
json: true,
body: {},
});
Expand All @@ -23,19 +23,19 @@ test('return "false" when "x-hub-signature" header is missing', async (t) => {
test('return "false" when secret is wrong', async (t) => {
t.plan(1);
const server = micro(async (req) => {
const valid = await verifySecret(req, 'wrong-secret');
const valid = await verifySecret(req, "wrong-secret");
t.false(valid);
return '';
return "";
});
const url = await listen(server);
await got(url, {
method: 'POST',
method: "POST",
headers: {
'X-Hub-Signature': 'sha1=30a233839fe2ddd9233c49fd593e8f1aec68f553',
"X-Hub-Signature": "sha1=30a233839fe2ddd9233c49fd593e8f1aec68f553",
},
json: true,
body: {
foo: 'bar',
foo: "bar",
},
});
server.close();
Expand All @@ -44,41 +44,41 @@ test('return "false" when secret is wrong', async (t) => {
test('return "true" when secret is correct', async (t) => {
t.plan(1);
const server = micro(async (req) => {
const valid = await verifySecret(req, 'my-secret');
const valid = await verifySecret(req, "my-secret");
t.true(valid);
return '';
return "";
});
const url = await listen(server);
await got(url, {
method: 'POST',
method: "POST",
headers: {
'X-Hub-Signature': 'sha1=30a233839fe2ddd9233c49fd593e8f1aec68f553',
"X-Hub-Signature": "sha1=30a233839fe2ddd9233c49fd593e8f1aec68f553",
},
json: true,
body: {
foo: 'bar',
foo: "bar",
},
});
server.close();
});

test('should not hang when verify is called more than once', async (t) => {
test("should not hang when verify is called more than once", async (t) => {
t.plan(1);
const server = micro(async (req) => {
const valid = await verifySecret(req, 'my-secret');
await verifySecret(req, 'my-secret');
const valid = await verifySecret(req, "my-secret");
await verifySecret(req, "my-secret");
t.true(valid);
return '';
return "";
});
const url = await listen(server);
await got(url, {
method: 'POST',
method: "POST",
headers: {
'X-Hub-Signature': 'sha1=30a233839fe2ddd9233c49fd593e8f1aec68f553',
"X-Hub-Signature": "sha1=30a233839fe2ddd9233c49fd593e8f1aec68f553",
},
json: true,
body: {
foo: 'bar',
foo: "bar",
},
});
server.close();
Expand Down
16 changes: 8 additions & 8 deletions test/unit/verify.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import test from 'ava';
import { verify } from '../../src/verify';
import test from "ava";
import { verify } from "../../src/verify";

const body = JSON.stringify({
foo: 'bar',
foo: "bar",
});
const signature = 'sha1=30a233839fe2ddd9233c49fd593e8f1aec68f553';
const signature = "sha1=30a233839fe2ddd9233c49fd593e8f1aec68f553";

test('return "false" when signature is missing', (t) => {
const valid = verify(body, 'my-secret');
const valid = verify(body, "my-secret");
t.false(valid);
});

test('return "false" when signature is an Array', (t) => {
const valid = verify(body, 'wrong-secret', signature);
const valid = verify(body, "wrong-secret", signature);
t.false(valid);
});

test('return "false" when secret is wrong', (t) => {
const valid = verify(body, 'wrong-secret', signature);
const valid = verify(body, "wrong-secret", signature);
t.false(valid);
});

test('return "true" when secret is correct', (t) => {
const valid = verify(body, 'my-secret', signature);
const valid = verify(body, "my-secret", signature);
t.true(valid);
});

0 comments on commit 55315d5

Please sign in to comment.