Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add skipKeyValidation option (to slshx handler) to skip key validation #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ export function createHandler<Env>(opts: Options<Env>) {
);
}
const hopts = opts as HandlerOptions<Env>;
const int = await validateInteraction(publicKeyData, request);
const int = await validateInteraction(
publicKeyData,
request,
opts.skipKeyValidation
);
if (!int) return new Response("Unauthorized", { status: 401 });

try {
Expand Down
11 changes: 7 additions & 4 deletions src/interactions/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ const ENCODER = /* @__PURE__ */ new TextEncoder();

export async function validateInteraction(
publicKeyData: Uint8Array,
request: Request
request: Request,
skipKeyValidation = false
): Promise<APIInteraction | APIModalSubmitInteraction | false> {
const requestBody = await request.text();
if (skipKeyValidation) return true && JSON.parse(requestBody);

const signature = hexDecode(
String(request.headers.get("X-Signature-Ed25519"))
);

const timestamp = String(request.headers.get("X-Signature-Timestamp"));
const body = await request.text();

const publicKey = await crypto.subtle.importKey(
"raw",
Expand All @@ -29,8 +32,8 @@ export async function validateInteraction(
"NODE-ED25519",
publicKey,
signature,
ENCODER.encode(timestamp + body)
ENCODER.encode(timestamp + requestBody)
);

return valid && JSON.parse(body);
return valid && JSON.parse(requestBody);
}
1 change: 1 addition & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface Options<Env = unknown> {
commands?: ChatInputCommands<Env>;
userCommands?: UserCommands<Env>;
messageCommands?: MessageCommands<Env>;
skipKeyValidation?: boolean;
}

export type HandlerOptions<Env> = Omit<Options<Env>, "applicationId"> &
Expand Down