discord-verify
Advanced tools
Comparing version 0.0.2-beta.2 to 0.0.2-beta.3
@@ -14,2 +14,2 @@ /// <reference types="@cloudflare/workers-types" /> | ||
export declare function isValidRequest(request: Request, publicKey: string, subtleCrypto: SubtleCrypto, algorithm?: SubtleCryptoImportKeyAlgorithm | string): Promise<boolean>; | ||
export declare function validate(rawBody: string, signature: Uint8Array, timestamp: string, publicKey: string, subtleCrypto: SubtleCrypto, algorithm?: SubtleCryptoImportKeyAlgorithm | string): Promise<boolean>; | ||
export declare function validate(rawBody: string | null | undefined, signature: string | null | undefined, timestamp: string | null | undefined, publicKey: string, subtleCrypto: SubtleCrypto, algorithm?: SubtleCryptoImportKeyAlgorithm | string): Promise<boolean>; |
@@ -29,6 +29,3 @@ const encoder = new TextEncoder(); | ||
const timestamp = clone.headers.get("X-Signature-Timestamp"); | ||
if (timestamp == null) { | ||
return false; | ||
} | ||
const signature = hexToBinary(clone.headers.get("X-Signature-Ed25519")); | ||
const signature = clone.headers.get("X-Signature-Ed25519"); | ||
const body = await clone.text(); | ||
@@ -38,7 +35,10 @@ return validate(body, signature, timestamp, publicKey, subtleCrypto, algorithm); | ||
export async function validate(rawBody, signature, timestamp, publicKey, subtleCrypto, algorithm = "Ed25519") { | ||
if (timestamp == null || signature == null || rawBody == null) { | ||
return false; | ||
} | ||
const key = await getCryptoKey(publicKey, subtleCrypto, algorithm); | ||
const name = typeof algorithm === "string" ? algorithm : algorithm.name; | ||
const isVerified = await subtleCrypto.verify(name, key, signature, encoder.encode(`${timestamp ?? ""}${rawBody}`)); | ||
const isVerified = await subtleCrypto.verify(name, key, hexToBinary(signature), encoder.encode(`${timestamp ?? ""}${rawBody}`)); | ||
return isVerified; | ||
} | ||
//# sourceMappingURL=verify.js.map |
{ | ||
"name": "discord-verify", | ||
"version": "0.0.2-beta.2", | ||
"version": "0.0.2-beta.3", | ||
"author": "Ian Mitchell", | ||
@@ -5,0 +5,0 @@ "description": "A library for verifying the authenticity of requests coming from the Discord Interactions API", |
@@ -34,5 +34,5 @@ # discord-verify | ||
```ts | ||
import { validate, hexToBinary } from "discord-verify"; | ||
import { validate } from "discord-verify"; | ||
function handleRequest( | ||
async function handleRequest( | ||
req: FastifyRequest<{ | ||
@@ -53,3 +53,3 @@ Body: APIInteraction; | ||
rawBody, | ||
hexToBinary(signature), | ||
signature, | ||
timestmap, | ||
@@ -56,0 +56,0 @@ this.client.publicKey, |
@@ -51,8 +51,3 @@ const encoder = new TextEncoder(); | ||
const timestamp = clone.headers.get("X-Signature-Timestamp"); | ||
if (timestamp == null) { | ||
return false; | ||
} | ||
const signature = hexToBinary(clone.headers.get("X-Signature-Ed25519")); | ||
const signature = clone.headers.get("X-Signature-Ed25519"); | ||
const body = await clone.text(); | ||
@@ -71,5 +66,5 @@ | ||
export async function validate( | ||
rawBody: string, | ||
signature: Uint8Array, | ||
timestamp: string, | ||
rawBody: string | null | undefined, | ||
signature: string | null | undefined, | ||
timestamp: string | null | undefined, | ||
publicKey: string, | ||
@@ -79,2 +74,6 @@ subtleCrypto: SubtleCrypto, | ||
) { | ||
if (timestamp == null || signature == null || rawBody == null) { | ||
return false; | ||
} | ||
const key = await getCryptoKey(publicKey, subtleCrypto, algorithm); | ||
@@ -86,3 +85,3 @@ const name = typeof algorithm === "string" ? algorithm : algorithm.name; | ||
key, | ||
signature, | ||
hexToBinary(signature), | ||
encoder.encode(`${timestamp ?? ""}${rawBody}`) | ||
@@ -89,0 +88,0 @@ ); |
Sorry, the diff of this file is not supported yet
12794