diff --git a/package.json b/package.json index d6534bb..84fa214 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "discord-bot-cdk-construct", - "version": "0.1.7", + "version": "0.1.8", "description": "A quick CDK Construct for creating a serverless Discord bot in AWS!", "author": { "name": "Gerald McAlister", diff --git a/src/functions/DiscordBotFunction.ts b/src/functions/DiscordBotFunction.ts index 8d80854..193d4de 100644 --- a/src/functions/DiscordBotFunction.ts +++ b/src/functions/DiscordBotFunction.ts @@ -2,8 +2,8 @@ import {Context, Callback} from 'aws-lambda'; import {DiscordEventRequest, DiscordEventResponse} from '../types'; import {getDiscordSecrets} from './utils/DiscordSecrets'; import {Lambda} from 'aws-sdk'; -import * as nacl from 'tweetnacl'; import {commandLambdaARN} from './constants/EnvironmentProps'; +import {sign} from 'tweetnacl'; const lambda = new Lambda(); @@ -18,25 +18,32 @@ export async function handler(event: DiscordEventRequest, context: Context, callback: Callback): Promise { console.log(`Received event: ${JSON.stringify(event)}`); - if (event && await verifyEvent(event)) { + const verifyPromise = verifyEvent(event); + + if (event) { switch (event.jsonBody.type) { case 1: // Return pongs for pings - return { - type: 1, - }; + if (await verifyPromise) { + return { + type: 1, + }; + } + break; case 2: // Actual input request - console.log('Invoking command lambda...'); - await lambda.invoke({ + const lambdaPromise = lambda.invoke({ FunctionName: commandLambdaARN, Payload: JSON.stringify(event), InvocationType: 'Event', }).promise(); - console.log('Returning temporary response...'); - return { - type: 5, - }; + if (await Promise.all([verifyPromise, lambdaPromise])) { + console.log('Returning temporary response...'); + return { + type: 5, + }; + } + break; } } @@ -53,7 +60,7 @@ export async function verifyEvent(event: DiscordEventRequest): Promise console.log('Getting Discord secrets...'); const discordSecrets = await getDiscordSecrets(); console.log('Verifying incoming event...'); - const isVerified = nacl.sign.detached.verify( + const isVerified = sign.detached.verify( Buffer.from(event.timestamp + JSON.stringify(event.jsonBody)), Buffer.from(event.signature, 'hex'), Buffer.from(discordSecrets?.publicKey ?? '', 'hex'), diff --git a/src/index.ts b/src/index.ts index 8c999bb..eaca502 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ export * from './types'; export * from './functions/utils/DiscordSecrets'; export * from './constructs/DiscordBotConstruct'; +export { verifyEvent } from './functions/DiscordBotFunction';