UNPKG

@ubiquity-os/plugin-sdk

Version:

SDK for plugin support.

121 lines (119 loc) 3.9 kB
"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); // src/signature.ts var signature_exports = {}; __export(signature_exports, { PluginInput: () => PluginInput, signPayload: () => signPayload, verifySignature: () => verifySignature }); module.exports = __toCommonJS(signature_exports); var PluginInput = class { _privateKey; stateId; eventName; eventPayload; settings; authToken; ref; command; constructor(privateKey, stateId, eventName, eventPayload, settings, authToken, ref, command) { this._privateKey = privateKey; this.stateId = stateId; this.eventName = eventName; this.eventPayload = eventPayload; this.settings = settings; this.authToken = authToken; this.ref = ref; this.command = command; } async getInputs() { const inputs = { stateId: this.stateId, eventName: this.eventName, eventPayload: JSON.stringify(this.eventPayload), settings: JSON.stringify(this.settings), authToken: this.authToken, ref: this.ref, command: JSON.stringify(this.command) }; const signature = await signPayload(JSON.stringify(inputs), this._privateKey); return { ...inputs, signature }; } }; async function verifySignature(publicKeyPem, inputs, signature) { try { const inputsOrdered = { stateId: inputs.stateId, eventName: inputs.eventName, eventPayload: inputs.eventPayload, settings: inputs.settings, authToken: inputs.authToken, ref: inputs.ref, command: inputs.command }; const pemContents = publicKeyPem.replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", "").trim(); const binaryDer = Uint8Array.from(atob(pemContents), (c) => c.charCodeAt(0)); const publicKey = await crypto.subtle.importKey( "spki", binaryDer, { name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" }, true, ["verify"] ); const signatureArray = Uint8Array.from(atob(signature), (c) => c.charCodeAt(0)); const dataArray = new TextEncoder().encode(JSON.stringify(inputsOrdered)); return await crypto.subtle.verify("RSASSA-PKCS1-v1_5", publicKey, signatureArray, dataArray); } catch (error) { console.error(error); return false; } } async function importRsaPrivateKey(pem) { const pemContents = pem.replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "").trim(); const binaryDer = Uint8Array.from(atob(pemContents), (c) => c.charCodeAt(0)); return await crypto.subtle.importKey( "pkcs8", binaryDer.buffer, { name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" }, true, ["sign"] ); } async function signPayload(payload, privateKey) { const data = new TextEncoder().encode(payload); const _privateKey = await importRsaPrivateKey(privateKey); const signature = await crypto.subtle.sign("RSASSA-PKCS1-v1_5", _privateKey, data); return btoa(String.fromCharCode(...new Uint8Array(signature))); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { PluginInput, signPayload, verifySignature });