Skip to content

Commit

Permalink
Set up environments, fix auth
Browse files Browse the repository at this point in the history
  • Loading branch information
mandar1jn committed Apr 5, 2024
1 parent cf3505f commit a4e02c7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
22 changes: 18 additions & 4 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { verifyKey } from "discord-interactions";
import { Env } from "env";
import { Command } from "types";
import { DiscordResponse } from "./response";
import { APIBaseInteraction, InteractionResponseType, InteractionType } from "discord-api-types/v10";

export class DiscordClient {

Expand All @@ -18,22 +19,35 @@ export class DiscordClient {

async handle(request: Request): Promise<DiscordResponse>
{
if(!this.isRequestValid(request))
const {body, valid} = await this.isRequestValid(request);
if(!valid)
{
return new DiscordResponse("Invalid request signature", { status: 401 });
}


const interaction = JSON.parse(body) as APIBaseInteraction<InteractionType, any>

if (interaction.type == InteractionType.Ping) {
return new DiscordResponse({ type: InteractionResponseType.Pong });
}

return new DiscordResponse("Not Found", {status: 404});
}

async isRequestValid(request: Request): Promise<boolean>
async isRequestValid(request: Request): Promise<{body: string, valid: boolean}>
{
const signature = request.headers.get('x-signature-ed25519');
const timestamp = request.headers.get('x-signature-timestamp');
const body = await request.text();
return (signature && timestamp && verifyKey(body, signature, timestamp, this.env.DISCORD_PUBLIC_KEY)) as boolean;

const valid = signature && timestamp && verifyKey(body, signature, timestamp, this.env.DISCORD_PUBLIC_KEY)

if(valid)
{
return {body, valid: true};
}

return {body, valid: false};
}

}
4 changes: 3 additions & 1 deletion src/env.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Snowflake } from "discord-api-types/globals";

export interface Env {
DISCORD_PUBLIC_KEY: Snowflake
DISCORD_PUBLIC_KEY: string,
DISCORD_TOKEN: string,
DISCORD_CLIENT_ID: string
}
16 changes: 15 additions & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,18 @@ main = "./src/index.ts"
compatibility_date = "2024-04-04"
compatibility_flags = [ "nodejs_compat" ]

workers_dev = false
workers_dev = false

[env.staging]
routes = [{ pattern = "staging.ptal-bot.marijnkneppers.dev", custom_domain = true }]

[env.staging.vars]
DISCORD_PUBLIC_KEY = "0f04aa2be4c2825bcd520f73bd9523ba0e97b11acb58c91927855daf7831d22c"
DISCORD_APPLICATION_ID = "1225902905786110074"

[env.production]
routes = [{ pattern = "ptal-bot.marijnkneppers.dev", custom_domain = true }]

[env.production.vars]
DISCORD_PUBLIC_KEY = "5d53f1984903809b1b8dc5843cd4735904d5d011d66a076d41f21d0cedd356ee"
DISCORD_APPLICATION_ID = "1225902823842119820"

0 comments on commit a4e02c7

Please sign in to comment.