UNPKG

@trap_stevo/veriauth

Version:

The ultimate authentication handshake engine for pairing devices and sessions with absolute precision. Empowering secure, modular client pairing with encrypted session key registration, trustless validation, and zero bootstrap dependencies. Designed for r

55 lines (54 loc) 1.63 kB
"use strict"; const crypto = require("crypto"); class VeriAuth { constructor(options = {}) { this.onKey = options.onKey; if (typeof this.onKey !== "function") { throw new Error("VeriAuth requires a `onKey(sessionID, keyBuffer, req)` function."); } this.acceptAlreadyPaired = options.acceptAlreadyPaired ?? false; this.allowAuthOverwrite = options.allowAuthOverwrite ?? false; this.containsOnKey = options.containsKey || null; } middleware() { return async (req, ress) => { const sessionKeyB64 = req.body?.sessionKey; const sessionID = req.headers["x-vlink-session-id"]; if (!sessionKeyB64 || !sessionID) { return ress.status(400).json({ error: "Missing session key or session ID." }); } try { if (this.containsOnKey) { const exists = await this.containsOnKey(sessionID); if (exists && !this.allowAuthOverwrite) { if (this.acceptAlreadyPaired) { return ress.json({ success: true, alreadyPaired: true }); } return ress.status(409).json({ error: "Session already paired." }); } } const keyBuffer = Buffer.from(sessionKeyB64, "base64"); await this.onKey(sessionID, keyBuffer, req); ress.json({ success: true }); } catch (error) { console.log("🔄 VeriAuth pairing error ~", error); ress.status(500).json({ error: "Pairing error." }); } }; } } ; module.exports = { VeriAuth };