UNPKG

s-bit-agent

Version:

s.BitAgent is a simple Bitwarden CLI wrapper which provides a SSH2 Key Agent solution for Bitwarden.

78 lines 3.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SshAgentcRequestIdentities = void 0; const handler_1 = require("../../types/handler"); const node_ipc_1 = require("node-ipc"); const SshPK = require("sshpk"); class SshAgentcRequestIdentities { constructor() { this.messageType = handler_1.IcpMessageType.SSH_AGENTC_REQUEST_IDENTITIES; } async handle(message, prefix, agentService, client) { agentService.logService.info(prefix + 'Received', 'SSH_AGENTC_REQUEST_IDENTITIES'); const response = agentService.keyCacheService.getCache()?.map((item) => { const pub = SshPK.parseKey(item.key, 'auto'); return { keyBlob: pub.toBuffer('rfc4253'), comment: item.comment, }; }) || (await agentService.cacheService.getCache(null)) .map((item) => { const raw = item.fields.find((field) => field.name === 'public-key'); if (!raw || !item.fields.find((field) => field.name === 'private-key')) { agentService.logService.error('Item', item.name, '@', item.id, 'has no public and/or private key'); return null; } const pub = SshPK.parseKey(raw.value, 'auto'); return { item, pub }; }) .filter((x) => x !== null) .reduce((acc, cur) => { acc.items.push({ key: cur.pub.toString('ssh'), comment: cur.item.name, }); return acc; }, new class ChainItem { constructor() { this.items = []; } save() { agentService.keyCacheService.setCache(this.items); return this.items; } }()) .save() .map((item) => { const pub = SshPK.parseKey(item.key, 'auto'); return { keyBlob: pub.toBuffer('rfc4253'), comment: item.comment, }; }); const parts = [ Buffer.from([handler_1.IcpMessageType.SSH_AGENT_IDENTITIES_ANSWER]), ]; const nkeysBuffer = Buffer.alloc(4); nkeysBuffer.writeUInt32BE(response.length, 0); parts.push(nkeysBuffer); response.forEach(({ keyBlob, comment }) => { const keyBlobLength = Buffer.alloc(4); keyBlobLength.writeUInt32BE(keyBlob.length, 0); parts.push(keyBlobLength); parts.push(keyBlob); const commentBuffer = Buffer.from(comment, 'utf-8'); const commentLength = Buffer.alloc(4); commentLength.writeUInt32BE(commentBuffer.length, 0); parts.push(commentLength); parts.push(commentBuffer); }); agentService.logService.info(prefix + 'Sending', 'SSH_AGENT_IDENTITIES_ANSWER'); const sizeBuffer = Buffer.alloc(4); sizeBuffer.writeUInt32BE(parts.reduce((acc, cur) => acc + cur.length, 0), 0); node_ipc_1.default.server.emit(client, Buffer.concat([sizeBuffer, ...parts])); } } exports.SshAgentcRequestIdentities = SshAgentcRequestIdentities; //# sourceMappingURL=SshAgentcRequestIdentities.js.map