s-bit-agent
Version:
s.BitAgent is a simple Bitwarden CLI wrapper which provides a SSH2 Key Agent solution for Bitwarden.
64 lines • 2.79 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BitwardenService = void 0;
const common_1 = require("@nestjs/common");
const child_process_1 = require("child_process");
const fs_1 = require("fs");
const path = require("path");
let BitwardenService = class BitwardenService {
constructor() {
this.logging = false;
}
enableLogging() {
this.logging = true;
}
find() {
const curDir = path.dirname(__filename);
const str = [
...Array(5)
.fill(0)
.map((_, i) => '../'.repeat(i))
.map((path) => `${path}node_modules/@bitwarden/cli/build/bw.js`),
'./node_modules/@bitwarden/cli/build/bw.js',
'./@bitwarden/cli/build/bw.js',
]
.map((p) => path.join(curDir, p))
.find((p) => (0, fs_1.existsSync)(p));
if (!str)
throw new Error('Bitwarden CLI not found');
return str;
}
run(session, ...args) {
const sess = session ? ['--session', session] : [];
const { status, stdout, stderr } = (0, child_process_1.spawnSync)('node', [this.find(), ...args, ...sess], { encoding: 'utf8', stdio: this.logging ? 'inherit' : 'pipe' });
if (status !== 0)
throw new Error(`Bitwarden CLI exited with status ${status}\nMessage:${stderr}`);
return stdout;
}
unlock(password) {
return this.run(null, 'unlock', password, '--raw').trim();
}
lock(session) {
this.run(session, 'lock');
}
create(object, session) {
const base64 = Buffer.from(JSON.stringify(object)).toString('base64');
return this.run(session, 'create', 'item', '--raw', base64);
}
getKeyItems(session) {
const items = JSON.parse(this.run(session, 'list', 'items'));
return items.filter((item) => !item.deletedDate &&
item.fields?.some((field) => field.name === 'custom-type' && field.value === 'ssh-key'));
}
};
exports.BitwardenService = BitwardenService;
exports.BitwardenService = BitwardenService = __decorate([
(0, common_1.Injectable)()
], BitwardenService);
//# sourceMappingURL=bitwarden.service.js.map