discord-verify
Advanced tools
Comparing version 0.0.2-beta.11 to 0.0.2-beta.12
@@ -1,2 +0,28 @@ | ||
export * from "./src/node"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
} | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "./src/node"], factory); | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./src/node"), exports); | ||
}); | ||
//# sourceMappingURL=node.js.map |
@@ -1,69 +0,85 @@ | ||
const encoder = new TextEncoder(); | ||
/** | ||
* Helper method that takes in a hex string and converts it to its binary representation. | ||
* @param hex Hex string to convert to binary | ||
* @returns The binary form of a hex string | ||
*/ | ||
export function hexToBinary(hex) { | ||
if (hex == null) { | ||
return new Uint8Array(0); | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
} | ||
const buffer = new Uint8Array(Math.ceil(hex.length / 2)); | ||
for (let i = 0; i < buffer.length; i++) { | ||
buffer[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16); | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports"], factory); | ||
} | ||
return buffer; | ||
} | ||
async function getCryptoKey(publicKey, subtleCrypto, algorithm) { | ||
const key = await subtleCrypto.importKey("raw", hexToBinary(publicKey), algorithm, true, ["verify"]); | ||
return key; | ||
} | ||
/** | ||
* Helper values for popular platforms | ||
*/ | ||
export const PlatformAlgorithm = { | ||
Cloudflare: { | ||
name: "NODE-ED25519", | ||
namedCurve: "NODE-ED25519", | ||
public: true, | ||
}, | ||
Vercel: { | ||
name: "eddsa", | ||
namedCurve: "ed25519", | ||
}, | ||
}; | ||
/** | ||
* Validates a request from Discord | ||
* @param request Request to verify | ||
* @param publicKey The application's public key | ||
* @param subtleCrypto The crypto engine to use | ||
* @param algorithm The name of the crypto algorithm to use | ||
* @returns Whether the request is valid or not | ||
*/ | ||
export async function isValidRequest(request, publicKey, subtleCrypto, algorithm = "Ed25519") { | ||
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 validate(body, signature, timestamp, publicKey, subtleCrypto, algorithm); | ||
} | ||
/** | ||
* Determines if a request is valid or not based on provided values | ||
* @param rawBody The raw body of the request | ||
* @param signature The signature header of the request | ||
* @param timestamp The timestamp header of the request | ||
* @param publicKey The application's public key | ||
* @param subtleCrypto The crypto engine to use | ||
* @param algorithm The name of the crypto algorithm to use | ||
* @returns Whether the request is valid or not | ||
*/ | ||
export async function validate(rawBody, signature, timestamp, publicKey, subtleCrypto, algorithm = "Ed25519") { | ||
if (timestamp == null || signature == null || rawBody == null) { | ||
return false; | ||
})(function (require, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validate = exports.isValidRequest = exports.PlatformAlgorithm = exports.hexToBinary = void 0; | ||
const encoder = new TextEncoder(); | ||
/** | ||
* Helper method that takes in a hex string and converts it to its binary representation. | ||
* @param hex Hex string to convert to binary | ||
* @returns The binary form of a hex string | ||
*/ | ||
function hexToBinary(hex) { | ||
if (hex == null) { | ||
return new Uint8Array(0); | ||
} | ||
const buffer = new Uint8Array(Math.ceil(hex.length / 2)); | ||
for (let i = 0; i < buffer.length; i++) { | ||
buffer[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16); | ||
} | ||
return buffer; | ||
} | ||
const key = await getCryptoKey(publicKey, subtleCrypto, algorithm); | ||
const name = typeof algorithm === "string" ? algorithm : algorithm.name; | ||
const isVerified = await subtleCrypto.verify(name, key, hexToBinary(signature), encoder.encode(`${timestamp ?? ""}${rawBody}`)); | ||
return isVerified; | ||
} | ||
exports.hexToBinary = hexToBinary; | ||
async function getCryptoKey(publicKey, subtleCrypto, algorithm) { | ||
const key = await subtleCrypto.importKey("raw", hexToBinary(publicKey), algorithm, true, ["verify"]); | ||
return key; | ||
} | ||
/** | ||
* Helper values for popular platforms | ||
*/ | ||
exports.PlatformAlgorithm = { | ||
Cloudflare: { | ||
name: "NODE-ED25519", | ||
namedCurve: "NODE-ED25519", | ||
public: true, | ||
}, | ||
Vercel: { | ||
name: "eddsa", | ||
namedCurve: "ed25519", | ||
}, | ||
}; | ||
/** | ||
* Validates a request from Discord | ||
* @param request Request to verify | ||
* @param publicKey The application's public key | ||
* @param subtleCrypto The crypto engine to use | ||
* @param algorithm The name of the crypto algorithm to use | ||
* @returns Whether the request is valid or not | ||
*/ | ||
async function isValidRequest(request, publicKey, subtleCrypto, algorithm = "Ed25519") { | ||
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 validate(body, signature, timestamp, publicKey, subtleCrypto, algorithm); | ||
} | ||
exports.isValidRequest = isValidRequest; | ||
/** | ||
* Determines if a request is valid or not based on provided values | ||
* @param rawBody The raw body of the request | ||
* @param signature The signature header of the request | ||
* @param timestamp The timestamp header of the request | ||
* @param publicKey The application's public key | ||
* @param subtleCrypto The crypto engine to use | ||
* @param algorithm The name of the crypto algorithm to use | ||
* @returns Whether the request is valid or not | ||
*/ | ||
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, hexToBinary(signature), encoder.encode(`${timestamp ?? ""}${rawBody}`)); | ||
return isVerified; | ||
} | ||
exports.validate = validate; | ||
}); | ||
//# sourceMappingURL=verify.js.map |
@@ -1,15 +0,34 @@ | ||
// @ts-expect-error Crypto types are not defined yet | ||
import crypto from "node:crypto"; | ||
import { isValidRequest as verifyRequest } from "./lib/verify"; | ||
export { hexToBinary, validate } from "./lib/verify"; | ||
/** | ||
* Validates a request from Discord | ||
* @param request Request to verify | ||
* @param publicKey The application's public key | ||
* @param algorithm The name of the crypto algorithm to use | ||
* @returns Whether the request is valid or not | ||
*/ | ||
export async function isValidRequest(request, publicKey, algorithm = "Ed25519") { | ||
return verifyRequest(request, publicKey, crypto.subtle, algorithm); | ||
} | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
} | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "node:crypto", "./lib/verify", "./lib/verify"], factory); | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isValidRequest = exports.validate = exports.hexToBinary = void 0; | ||
// @ts-expect-error Crypto types are not defined yet | ||
const node_crypto_1 = __importDefault(require("node:crypto")); | ||
const verify_1 = require("./lib/verify"); | ||
var verify_2 = require("./lib/verify"); | ||
Object.defineProperty(exports, "hexToBinary", { enumerable: true, get: function () { return verify_2.hexToBinary; } }); | ||
Object.defineProperty(exports, "validate", { enumerable: true, get: function () { return verify_2.validate; } }); | ||
/** | ||
* Validates a request from Discord | ||
* @param request Request to verify | ||
* @param publicKey The application's public key | ||
* @param algorithm The name of the crypto algorithm to use | ||
* @returns Whether the request is valid or not | ||
*/ | ||
async function isValidRequest(request, publicKey, algorithm = "Ed25519") { | ||
return (0, verify_1.isValidRequest)(request, publicKey, node_crypto_1.default.subtle, algorithm); | ||
} | ||
exports.isValidRequest = isValidRequest; | ||
}); | ||
//# sourceMappingURL=node.js.map |
@@ -1,2 +0,13 @@ | ||
export {}; | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
} | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports"], factory); | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
}); | ||
//# sourceMappingURL=crypto.js.map |
@@ -13,3 +13,14 @@ /** | ||
*/ | ||
export {}; | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
} | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports"], factory); | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
}); | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,13 @@ | ||
export {}; | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
} | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports"], factory); | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
}); | ||
//# sourceMappingURL=request.js.map |
@@ -1,13 +0,30 @@ | ||
import { isValidRequest as verifyRequest } from "./lib/verify"; | ||
export { hexToBinary, PlatformAlgorithm, validate } from "./lib/verify"; | ||
/** | ||
* Validates a request from Discord | ||
* @param request Request to verify | ||
* @param publicKey The application's public key | ||
* @param algorithm The name of the crypto algorithm to use | ||
* @returns Whether the request is valid or not | ||
*/ | ||
export async function isValidRequest(request, publicKey, algorithm = "Ed25519") { | ||
return verifyRequest(request, publicKey, crypto.subtle, algorithm); | ||
} | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
} | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "./lib/verify", "./lib/verify"], factory); | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isValidRequest = exports.validate = exports.PlatformAlgorithm = exports.hexToBinary = void 0; | ||
const verify_1 = require("./lib/verify"); | ||
var verify_2 = require("./lib/verify"); | ||
Object.defineProperty(exports, "hexToBinary", { enumerable: true, get: function () { return verify_2.hexToBinary; } }); | ||
Object.defineProperty(exports, "PlatformAlgorithm", { enumerable: true, get: function () { return verify_2.PlatformAlgorithm; } }); | ||
Object.defineProperty(exports, "validate", { enumerable: true, get: function () { return verify_2.validate; } }); | ||
/** | ||
* Validates a request from Discord | ||
* @param request Request to verify | ||
* @param publicKey The application's public key | ||
* @param algorithm The name of the crypto algorithm to use | ||
* @returns Whether the request is valid or not | ||
*/ | ||
async function isValidRequest(request, publicKey, algorithm = "Ed25519") { | ||
return (0, verify_1.isValidRequest)(request, publicKey, crypto.subtle, algorithm); | ||
} | ||
exports.isValidRequest = isValidRequest; | ||
}); | ||
//# sourceMappingURL=web.js.map |
@@ -1,2 +0,28 @@ | ||
export * from "./src/web"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
} | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "./src/web"], factory); | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./src/web"), exports); | ||
}); | ||
//# sourceMappingURL=web.js.map |
{ | ||
"name": "discord-verify", | ||
"version": "0.0.2-beta.11", | ||
"version": "0.0.2-beta.12", | ||
"author": "Ian Mitchell", | ||
@@ -5,0 +5,0 @@ "description": "A library for verifying the authenticity of requests coming from the Discord Interactions API", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31134
659