Skip to content

Commit 0d94828

Browse files
committed
make functions sync
1 parent f73c4a5 commit 0d94828

File tree

6 files changed

+50
-63
lines changed

6 files changed

+50
-63
lines changed

src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ export { sign } from "./node/sign";
22
import { verify } from "./node/verify";
33
export { verify };
44

5-
export async function verifyWithFallback(
5+
export function verifyWithFallback(
66
secret: string,
77
payload: string,
88
signature: string,
99
additionalSecrets: undefined | string[],
10-
): Promise<any> {
11-
const firstPass = await verify(secret, payload, signature);
10+
): boolean {
11+
const firstPass = verify(secret, payload, signature);
1212

1313
if (firstPass) {
1414
return true;
1515
}
1616

1717
if (additionalSecrets !== undefined) {
1818
for (const s of additionalSecrets) {
19-
const v: boolean = await verify(s, payload, signature);
19+
const v: boolean = verify(s, payload, signature);
2020
if (v) {
2121
return v;
2222
}

src/node/sign.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { createHmac } from "crypto";
22
import { VERSION } from "../version";
33

4-
export async function sign(
5-
secret: string | Buffer,
6-
payload: string,
7-
): Promise<string> {
4+
export function sign(secret: string | Buffer, payload: string): string {
85
if (!secret || !payload) {
96
throw new TypeError(
107
"[@octokit/webhooks-methods] secret & payload required for sign()",

src/node/verify.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { Buffer } from "buffer";
44
import { VERSION } from "../version";
55
import { isValidSignaturePrefix } from "../utils";
66

7-
export async function verify(
7+
export function verify(
88
secret: string | Buffer,
99
eventPayload: string,
1010
signature: string,
11-
): Promise<boolean> {
11+
): boolean {
1212
if (!secret || !eventPayload || !signature) {
1313
throw new TypeError(
1414
"[@octokit/webhooks-methods] secret, eventPayload & signature required",
@@ -24,9 +24,9 @@ export async function verify(
2424
return false;
2525
}
2626

27-
const verificationBuffer = Buffer.from(
28-
createHmac("sha256", secret).update(eventPayload).digest(),
29-
);
27+
const verificationBuffer = createHmac("sha256", secret)
28+
.update(eventPayload)
29+
.digest().buffer as Buffer;
3030

3131
// constant time comparison to prevent timing attacks
3232
// https://stackoverflow.com/a/31096242/206879

src/web.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { type SignOptions } from "./types";
2-
31
const enc = new TextEncoder();
42

53
function hexToUInt8Array(string: string) {
@@ -35,15 +33,7 @@ async function importKey(secret: string) {
3533
);
3634
}
3735

38-
export async function sign(options: SignOptions | string, payload: string) {
39-
const { secret, algorithm } =
40-
typeof options === "object"
41-
? {
42-
secret: options.secret,
43-
algorithm: "sha256",
44-
}
45-
: { secret: options, algorithm: "sha256" };
46-
36+
export async function sign(secret: string, payload: string) {
4737
if (!secret || !payload) {
4838
throw new TypeError(
4939
"[@octokit/webhooks-methods] secret & payload required for sign()",
@@ -56,7 +46,7 @@ export async function sign(options: SignOptions | string, payload: string) {
5646
enc.encode(payload),
5747
);
5848

59-
return `${algorithm}=${UInt8ArrayToHex(signature)}`;
49+
return `sha256=${UInt8ArrayToHex(signature)}`;
6050
}
6151

6252
export async function verify(

test/sign.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,31 @@ describe("sign", () => {
1414
expect(sign.VERSION).toEqual("0.0.0-development");
1515
});
1616

17-
test("throws without options throws", async () => {
17+
test("throws without options throws", () => {
1818
// @ts-expect-error
19-
await expect(() => sign()).rejects.toThrow(
19+
expect(() => sign()).toThrow(
2020
"[@octokit/webhooks-methods] secret & payload required for sign()",
2121
);
2222
});
2323

24-
test("throws without secret", async () => {
24+
test("throws without secret", () => {
2525
// @ts-ignore
26-
await expect(() => sign(undefined, eventPayload)).rejects.toThrow(
26+
expect(() => sign(undefined, eventPayload)).toThrow(
2727
"[@octokit/webhooks-methods] secret & payload required for sign()",
2828
);
2929
});
3030

31-
test("throws without eventPayload", async () => {
31+
test("throws without eventPayload", () => {
3232
// @ts-expect-error
33-
await expect(() => sign(secret)).rejects.toThrow(
33+
expect(() => sign(secret)).toThrow(
3434
"[@octokit/webhooks-methods] secret & payload required for sign()",
3535
);
3636
});
3737

3838
describe("with secret as Buffer", () => {
3939
describe("returns expected sha256 signature", () => {
40-
test("sign(secret, eventPayload)", async () => {
41-
const signature = await sign(
40+
test("sign(secret, eventPayload)", () => {
41+
const signature = sign(
4242
Buffer.from(secret),
4343
JSON.stringify(eventPayload),
4444
);
@@ -51,8 +51,8 @@ describe("sign", () => {
5151

5252
describe("with eventPayload as string", () => {
5353
describe("returns expected sha256 signature", () => {
54-
test("sign(secret, eventPayload)", async () => {
55-
const signature = await sign(secret, JSON.stringify(eventPayload));
54+
test("sign(secret, eventPayload)", () => {
55+
const signature = sign(secret, JSON.stringify(eventPayload));
5656
expect(signature).toBe(
5757
"sha256=4864d2759938a15468b5df9ade20bf161da9b4f737ea61794142f3484236bda3",
5858
);

test/verify.test.ts

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,86 +22,86 @@ describe("verify", () => {
2222
expect(verify.VERSION).toEqual("0.0.0-development");
2323
});
2424

25-
test("verify() without options throws", async () => {
25+
test("verify() without options throws", () => {
2626
// @ts-expect-error
27-
await expect(() => verify()).rejects.toThrow(
27+
expect(() => verify()).toThrow(
2828
"[@octokit/webhooks-methods] secret, eventPayload & signature required",
2929
);
3030
});
3131

32-
test("verify(undefined, eventPayload) without secret throws", async () => {
32+
test("verify(undefined, eventPayload) without secret throws", () => {
3333
// @ts-expect-error
34-
await expect(() => verify(undefined, eventPayload)).rejects.toThrow(
34+
expect(() => verify(undefined, eventPayload)).toThrow(
3535
"[@octokit/webhooks-methods] secret, eventPayload & signature required",
3636
);
3737
});
3838

39-
test("verify(secret) without eventPayload throws", async () => {
39+
test("verify(secret) without eventPayload throws", () => {
4040
// @ts-expect-error
41-
await expect(() => verify(secret)).rejects.toThrow(
41+
expect(() => verify(secret)).toThrow(
4242
"[@octokit/webhooks-methods] secret, eventPayload & signature required",
4343
);
4444
});
4545

46-
test("verify(secret, eventPayload) without options.signature throws", async () => {
46+
test("verify(secret, eventPayload) without options.signature throws", () => {
4747
// @ts-expect-error
48-
await expect(() => verify(secret, eventPayload)).rejects.toThrow(
48+
expect(() => verify(secret, eventPayload)).toThrow(
4949
"[@octokit/webhooks-methods] secret, eventPayload & signature required",
5050
);
5151
});
5252

53-
test("verify(secret, eventPayload, signature) returns true for correct signature", async () => {
54-
const signatureMatches = await verify(secret, eventPayload, signature);
53+
test("verify(secret, eventPayload, signature) returns true for correct signature", () => {
54+
const signatureMatches = verify(secret, eventPayload, signature);
5555
expect(signatureMatches).toBe(true);
5656
});
5757

58-
test("verify(secret, eventPayload, signature) returns true for secret provided as Buffer", async () => {
59-
const signatureMatches = await verify(
58+
test("verify(secret, eventPayload, signature) returns true for secret provided as Buffer", () => {
59+
const signatureMatches = verify(
6060
Buffer.from(secret),
6161
eventPayload,
6262
signature,
6363
);
6464
expect(signatureMatches).toBe(true);
6565
});
6666

67-
test("verify(secret, eventPayload, signature) returns false for incorrect signature", async () => {
68-
const signatureMatches = await verify(
67+
test("verify(secret, eventPayload, signature) returns false for incorrect signature", () => {
68+
const signatureMatches = verify(
6969
secret,
7070
eventPayload,
7171
"sha256=xxxccac34c43c7dc1cbb905488b1b81347fcc700a7b025697a9d07862256023f",
7272
);
7373
expect(signatureMatches).toBe(false);
7474
});
7575

76-
test("verify(secret, eventPayload, signature) returns false for incorrect signature", async () => {
77-
const signatureMatches = await verify(secret, eventPayload, "foo");
76+
test("verify(secret, eventPayload, signature) returns false for incorrect signature", () => {
77+
const signatureMatches = verify(secret, eventPayload, "foo");
7878
expect(signatureMatches).toBe(false);
7979
});
8080

81-
test("verify(secret, eventPayload, signature) returns false for incorrect secret", async () => {
82-
const signatureMatches = await verify("foo", eventPayload, signature);
81+
test("verify(secret, eventPayload, signature) returns false for incorrect secret", () => {
82+
const signatureMatches = verify("foo", eventPayload, signature);
8383
expect(signatureMatches).toBe(false);
8484
});
8585

86-
test("verify(secret, eventPayload, signature) returns true if eventPayload contains special characters (#71)", async () => {
86+
test("verify(secret, eventPayload, signature) returns true if eventPayload contains special characters (#71)", () => {
8787
// https://github.com/octokit/webhooks.js/issues/71
88-
const signatureMatchesLowerCaseSequence = await verify(
88+
const signatureMatchesLowerCaseSequence = verify(
8989
"development",
9090
toNormalizedJsonString({
9191
foo: "Foo\n\u001b[34mbar: ♥♥♥♥♥♥♥♥\nthis-is-lost\u001b[0m\u001b[2K",
9292
}),
9393
"sha256=9dacf9003316b09be07df56d86a2d0d6872e42a1e6c72c3bad9ff915a7c5603e",
9494
);
9595
expect(signatureMatchesLowerCaseSequence).toBe(true);
96-
const signatureMatchesUpperCaseSequence = await verify(
96+
const signatureMatchesUpperCaseSequence = verify(
9797
"development",
9898
toNormalizedJsonString({
9999
foo: "Foo\n\u001B[34mbar: ♥♥♥♥♥♥♥♥\nthis-is-lost\u001B[0m\u001B[2K",
100100
}),
101101
"sha256=9dacf9003316b09be07df56d86a2d0d6872e42a1e6c72c3bad9ff915a7c5603e",
102102
);
103103
expect(signatureMatchesUpperCaseSequence).toBe(true);
104-
const signatureMatchesEscapedSequence = await verify(
104+
const signatureMatchesEscapedSequence = verify(
105105
"development",
106106
toNormalizedJsonString({
107107
foo: "\\u001b",
@@ -117,8 +117,8 @@ describe("verifyWithFallback", () => {
117117
expect(verifyWithFallback).toBeInstanceOf(Function);
118118
});
119119

120-
test("verifyWithFallback(secret, eventPayload, signature, [bogus]) returns true", async () => {
121-
const signatureMatches = await verifyWithFallback(
120+
test("verifyWithFallback(secret, eventPayload, signature, [bogus]) returns true", () => {
121+
const signatureMatches = verifyWithFallback(
122122
secret,
123123
eventPayload,
124124
signature,
@@ -127,8 +127,8 @@ describe("verifyWithFallback", () => {
127127
expect(signatureMatches).toBe(true);
128128
});
129129

130-
test("verifyWithFallback(bogus, eventPayload, signature, [secret]) returns true", async () => {
131-
const signatureMatches = await verifyWithFallback(
130+
test("verifyWithFallback(bogus, eventPayload, signature, [secret]) returns true", () => {
131+
const signatureMatches = verifyWithFallback(
132132
"foo",
133133
eventPayload,
134134
signature,
@@ -137,8 +137,8 @@ describe("verifyWithFallback", () => {
137137
expect(signatureMatches).toBe(true);
138138
});
139139

140-
test("verify(bogus, eventPayload, signature, [bogus]) returns false", async () => {
141-
const signatureMatches = await verifyWithFallback(
140+
test("verify(bogus, eventPayload, signature, [bogus]) returns false", () => {
141+
const signatureMatches = verifyWithFallback(
142142
"foo",
143143
eventPayload,
144144
signature,

0 commit comments

Comments
 (0)