Skip to content

Commit 0fea229

Browse files
authored
fix: add verifyWithFallback() function in the web entrypoint (#312)
1 parent b97234c commit 0fea229

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/web.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,26 @@ export async function verify(
7979
enc.encode(eventPayload),
8080
);
8181
}
82+
export async function verifyWithFallback(
83+
secret: string,
84+
payload: string,
85+
signature: string,
86+
additionalSecrets: undefined | string[],
87+
): Promise<any> {
88+
const firstPass = await verify(secret, payload, signature);
89+
90+
if (firstPass) {
91+
return true;
92+
}
93+
94+
if (additionalSecrets !== undefined) {
95+
for (const s of additionalSecrets) {
96+
const v: boolean = await verify(s, payload, signature);
97+
if (v) {
98+
return v;
99+
}
100+
}
101+
}
102+
103+
return false;
104+
}

test/deno/web_test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { sign, verify } from "../../pkg/dist-web/index.js";
1+
import { sign, verify, verifyWithFallback } from "../../pkg/dist-web/index.js";
22

33
import { assertEquals } from "@std/assert";
44

@@ -16,3 +16,11 @@ Deno.test("verify", async () => {
1616
const expected = true;
1717
assertEquals(actual, expected);
1818
});
19+
20+
Deno.test("verify with fallback", async () => {
21+
const signature =
22+
"sha256=1b2c16b75bd2a870c114153ccda5bcfca63314bc722fa160d690de133ccbb9db";
23+
const actual = await verifyWithFallback("foo", "data", signature, ["secret"]);
24+
const expected = true;
25+
assertEquals(actual, expected);
26+
});

0 commit comments

Comments
 (0)