discord-verify
Advanced tools
Comparing version 0.0.2-beta.29 to 0.0.2-beta.30
132
dist/node.js
@@ -1,117 +0,10 @@ | ||
"use strict"; | ||
var __create = Object.create; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
}; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
mod | ||
)); | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
import { | ||
PlatformAlgorithm, | ||
hexStringToBinary, | ||
isValidRequest, | ||
verify | ||
} from "./chunk-YPKOLIOM.js"; | ||
// node.ts | ||
var node_exports = {}; | ||
__export(node_exports, { | ||
PlatformAlgorithm: () => PlatformAlgorithm, | ||
hexStringToBinary: () => hexStringToBinary, | ||
isValidRequest: () => isValidRequest2, | ||
verify: () => verify | ||
}); | ||
module.exports = __toCommonJS(node_exports); | ||
// src/node.ts | ||
var import_node_crypto = __toESM(require("crypto")); | ||
// src/lib/verify.ts | ||
var encoder = new TextEncoder(); | ||
var KEYS = { | ||
ZERO: 48, | ||
A: 65, | ||
a: 97 | ||
}; | ||
function hexCharToBinary(char) { | ||
const code = char.charCodeAt(0); | ||
if (code >= KEYS.a) { | ||
return code - KEYS.a + 10; | ||
} | ||
if (code >= KEYS.A) { | ||
return code - KEYS.A + 10; | ||
} | ||
return code - KEYS.ZERO; | ||
} | ||
function hexStringToBinary(key) { | ||
if (key == null) { | ||
return new Uint8Array(0).buffer; | ||
} | ||
const view = new Uint8Array(key.length / 2); | ||
for (let i = 0, o = 0; i < key.length; i += 2, ++o) { | ||
view[o] = hexCharToBinary(key[i]) << 4 | hexCharToBinary(key[i + 1]); | ||
} | ||
return view.buffer; | ||
} | ||
async function getCryptoKey(publicKey, subtleCrypto, algorithm) { | ||
const key = await subtleCrypto.importKey( | ||
"raw", | ||
hexStringToBinary(publicKey), | ||
algorithm, | ||
true, | ||
["verify"] | ||
); | ||
return key; | ||
} | ||
var PlatformAlgorithm = { | ||
Web: "Ed25519", | ||
NewNode: "Ed25519", | ||
OldNode: { | ||
name: "NODE-ED25519", | ||
namedCurve: "NODE-ED25519", | ||
public: true | ||
}, | ||
Cloudflare: { | ||
name: "NODE-ED25519", | ||
namedCurve: "NODE-ED25519", | ||
public: true | ||
}, | ||
Vercel: { | ||
name: "eddsa", | ||
namedCurve: "ed25519" | ||
} | ||
}; | ||
async function isValidRequest(request, publicKey, subtleCrypto, algorithm = PlatformAlgorithm.NewNode) { | ||
const clone = request.clone(); | ||
const timestamp = clone.headers.get("X-Signature-Timestamp"); | ||
const signature = clone.headers.get("X-Signature-Ed25519"); | ||
const body = await clone.text(); | ||
return verify(body, signature, timestamp, publicKey, subtleCrypto, algorithm); | ||
} | ||
async function verify(rawBody, signature, timestamp, publicKey, subtleCrypto, algorithm = PlatformAlgorithm.NewNode) { | ||
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, | ||
hexStringToBinary(signature), | ||
encoder.encode(`${timestamp ?? ""}${rawBody}`) | ||
); | ||
return isVerified; | ||
} | ||
// src/node.ts | ||
import crypto from "node:crypto"; | ||
async function isValidRequest2(request, publicKey, algorithm) { | ||
@@ -122,3 +15,3 @@ try { | ||
publicKey, | ||
import_node_crypto.default.webcrypto.subtle, | ||
crypto.webcrypto.subtle, | ||
algorithm ?? PlatformAlgorithm.NewNode | ||
@@ -131,3 +24,3 @@ ); | ||
publicKey, | ||
import_node_crypto.default.webcrypto.subtle, | ||
crypto.webcrypto.subtle, | ||
algorithm ?? PlatformAlgorithm.OldNode | ||
@@ -139,9 +32,8 @@ ); | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
export { | ||
PlatformAlgorithm, | ||
hexStringToBinary, | ||
isValidRequest, | ||
isValidRequest2 as isValidRequest, | ||
verify | ||
}); | ||
}; | ||
//# sourceMappingURL=node.js.map |
118
dist/web.js
@@ -1,107 +0,8 @@ | ||
"use strict"; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
}; | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
import { | ||
PlatformAlgorithm, | ||
hexStringToBinary, | ||
isValidRequest, | ||
verify | ||
} from "./chunk-YPKOLIOM.js"; | ||
// web.ts | ||
var web_exports = {}; | ||
__export(web_exports, { | ||
PlatformAlgorithm: () => PlatformAlgorithm, | ||
hexStringToBinary: () => hexStringToBinary, | ||
isValidRequest: () => isValidRequest2, | ||
verify: () => verify | ||
}); | ||
module.exports = __toCommonJS(web_exports); | ||
// src/lib/verify.ts | ||
var encoder = new TextEncoder(); | ||
var KEYS = { | ||
ZERO: 48, | ||
A: 65, | ||
a: 97 | ||
}; | ||
function hexCharToBinary(char) { | ||
const code = char.charCodeAt(0); | ||
if (code >= KEYS.a) { | ||
return code - KEYS.a + 10; | ||
} | ||
if (code >= KEYS.A) { | ||
return code - KEYS.A + 10; | ||
} | ||
return code - KEYS.ZERO; | ||
} | ||
function hexStringToBinary(key) { | ||
if (key == null) { | ||
return new Uint8Array(0).buffer; | ||
} | ||
const view = new Uint8Array(key.length / 2); | ||
for (let i = 0, o = 0; i < key.length; i += 2, ++o) { | ||
view[o] = hexCharToBinary(key[i]) << 4 | hexCharToBinary(key[i + 1]); | ||
} | ||
return view.buffer; | ||
} | ||
async function getCryptoKey(publicKey, subtleCrypto, algorithm) { | ||
const key = await subtleCrypto.importKey( | ||
"raw", | ||
hexStringToBinary(publicKey), | ||
algorithm, | ||
true, | ||
["verify"] | ||
); | ||
return key; | ||
} | ||
var PlatformAlgorithm = { | ||
Web: "Ed25519", | ||
NewNode: "Ed25519", | ||
OldNode: { | ||
name: "NODE-ED25519", | ||
namedCurve: "NODE-ED25519", | ||
public: true | ||
}, | ||
Cloudflare: { | ||
name: "NODE-ED25519", | ||
namedCurve: "NODE-ED25519", | ||
public: true | ||
}, | ||
Vercel: { | ||
name: "eddsa", | ||
namedCurve: "ed25519" | ||
} | ||
}; | ||
async function isValidRequest(request, publicKey, subtleCrypto, algorithm = PlatformAlgorithm.NewNode) { | ||
const clone = request.clone(); | ||
const timestamp = clone.headers.get("X-Signature-Timestamp"); | ||
const signature = clone.headers.get("X-Signature-Ed25519"); | ||
const body = await clone.text(); | ||
return verify(body, signature, timestamp, publicKey, subtleCrypto, algorithm); | ||
} | ||
async function verify(rawBody, signature, timestamp, publicKey, subtleCrypto, algorithm = PlatformAlgorithm.NewNode) { | ||
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, | ||
hexStringToBinary(signature), | ||
encoder.encode(`${timestamp ?? ""}${rawBody}`) | ||
); | ||
return isVerified; | ||
} | ||
// src/web.ts | ||
@@ -111,9 +12,8 @@ async function isValidRequest2(request, publicKey, algorithm = PlatformAlgorithm.Web) { | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
export { | ||
PlatformAlgorithm, | ||
hexStringToBinary, | ||
isValidRequest, | ||
isValidRequest2 as isValidRequest, | ||
verify | ||
}); | ||
}; | ||
//# sourceMappingURL=web.js.map |
{ | ||
"name": "discord-verify", | ||
"version": "0.0.2-beta.29", | ||
"version": "0.0.2-beta.30", | ||
"author": "Ian Mitchell", | ||
"description": "A library for verifying the authenticity of requests coming from the Discord Interactions API", | ||
"license": "MIT", | ||
"type": "module", | ||
"homepage": "https://github.com/ianmitchell/interaction-kit/tree/main/packages/discord-verify", | ||
@@ -8,0 +9,0 @@ "exports": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
52968
Yes