Skip to content

Commit e57bf23

Browse files
feat: add support for AWS lambda (#252)
1 parent 8f7d829 commit e57bf23

File tree

6 files changed

+73
-8
lines changed

6 files changed

+73
-8
lines changed

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/discord-verify/aws.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./src/aws.js";

packages/discord-verify/build.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineBuildConfig } from "unbuild";
22

33
export default defineBuildConfig({
4-
entries: ["./node", "./web"],
4+
entries: ["./node", "./web", "./aws"],
55
rollup: {
66
emitCJS: true,
77
},

packages/discord-verify/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
"import": "./dist/node.mjs",
2121
"require": "./dist/node.cjs",
2222
"types": "./dist/node.d.ts"
23+
},
24+
"./aws": {
25+
"import": "./dist/aws.mjs",
26+
"require": "./dist/aws.cjs",
27+
"types": "./dist/aws.d.ts"
2328
}
2429
},
2530
"main": "./dist/web.mjs",

packages/discord-verify/src/aws.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// AWS lambda requires an explicit crypto import.
2+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment, @typescript-eslint/prefer-ts-expect-error
3+
// @ts-ignore-error We can't use Node types since we aren't fully in a Node environment
4+
import crypto from "crypto";
5+
import { PlatformAlgorithm, verify } from "./lib/verify.js";
6+
7+
export interface AWSGatewayEvent {
8+
headers: Record<string, string>;
9+
body: string;
10+
}
11+
12+
/**
13+
* Validates a request from Discord received via AWS lambda.
14+
* @param event - The AWS Gateway event to verify
15+
* @param publicKey - The application's public key
16+
* @param algorithm - The name of the crypto algorithm to use
17+
* @returns Whether the request is valid or not
18+
*/
19+
export async function isValid(
20+
event: AWSGatewayEvent,
21+
publicKey: string,
22+
algorithm?: string
23+
) {
24+
const signature = event.headers["X-Signature-Ed25519"];
25+
const timestamp = event.headers["X-Signature-Timestamp"];
26+
const { body } = event;
27+
if (!signature || !timestamp || !body) return false;
28+
29+
try {
30+
await verify(
31+
event.body,
32+
signature,
33+
timestamp,
34+
publicKey,
35+
crypto.webcrypto.subtle,
36+
algorithm ?? PlatformAlgorithm.OldNode
37+
);
38+
} catch (error: unknown) {
39+
if (error instanceof Error && error.constructor.name === "DOMException") {
40+
return verify(
41+
event.body,
42+
signature,
43+
timestamp,
44+
publicKey,
45+
crypto.webcrypto.subtle,
46+
algorithm ?? PlatformAlgorithm.OldNode
47+
);
48+
}
49+
50+
throw error;
51+
}
52+
}

packages/discord-verify/tsconfig.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
"target": "ES2021",
1919
"moduleResolution": "NodeNext"
2020
},
21-
"include": ["src/**/*.ts", "node.ts", "node.cts", "web.ts"],
21+
"include": [
22+
"src/**/*.ts",
23+
"node.ts",
24+
"node.cts",
25+
"web.ts",
26+
"aws.ts",
27+
"aws.cts"
28+
],
2229
"exclude": ["node_modules", "dist", "*.js"]
2330
}

0 commit comments

Comments
 (0)