keepasshttp-client
Version:
Node.js module for interaction with KeePassHTTP
160 lines • 7.24 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var node_fetch_1 = tslib_1.__importDefault(require("node-fetch"));
var url_1 = tslib_1.__importDefault(require("url"));
var http_1 = require("http");
var https_1 = require("https");
var common_1 = require("./model/common");
var util_1 = require("./private/util");
var model_1 = require("./model");
var KeePassHttpClient = /** @class */ (function () {
function KeePassHttpClient(opts) {
this._url = "http://localhost:19455";
if (opts && opts.url) {
this._url = opts.url;
}
if (opts && opts.keyId) {
this._id = opts.keyId.id;
this._key = opts.keyId.key;
}
else {
this._key = util_1.generateRandomBase64(util_1.KEY_SIZE);
}
}
Object.defineProperty(KeePassHttpClient.prototype, "url", {
get: function () {
return this._url;
},
enumerable: true,
configurable: true
});
Object.defineProperty(KeePassHttpClient.prototype, "id", {
get: function () {
return this._id;
},
enumerable: true,
configurable: true
});
Object.defineProperty(KeePassHttpClient.prototype, "key", {
get: function () {
return this._key;
},
enumerable: true,
configurable: true
});
KeePassHttpClient.prototype.testAssociate = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
return [2 /*return*/, this.request(new model_1.Request.TestAssosiate(this.key, this.id))];
});
});
};
KeePassHttpClient.prototype.associate = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var response;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request(new model_1.Request.Associate(this.key))];
case 1:
response = _a.sent();
this._id = response.Id;
return [2 /*return*/, response];
}
});
});
};
KeePassHttpClient.prototype.getLogins = function (args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var response, decryptValue;
var _this = this;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request(new model_1.Request.GetLogins(this.key, this.id, args))];
case 1:
response = _a.sent();
decryptValue = (function (value) { return util_1.decrypt(_this.key, response.Nonce, value); });
if (response.Entries) {
response.Entries.forEach(function (entry) {
entry.Name = decryptValue(entry.Name);
entry.Login = decryptValue(entry.Login);
entry.Password = decryptValue(entry.Password);
entry.Uuid = decryptValue(entry.Uuid);
if (entry.StringFields) {
entry.StringFields.forEach(function (field) {
field.Key = decryptValue(field.Key);
field.Value = decryptValue(field.Value);
});
}
});
}
return [2 /*return*/, response];
}
});
});
};
KeePassHttpClient.prototype.getLoginsCount = function (args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
return [2 /*return*/, this.request(new model_1.Request.GetLoginsCount(this.key, this.id, args))];
});
});
};
KeePassHttpClient.prototype.createLogin = function (args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
return [2 /*return*/, this.request(new model_1.Request.CreateLogin(this.key, this.id, args))];
});
});
};
KeePassHttpClient.prototype.updateLogin = function (args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
return [2 /*return*/, this.request(new model_1.Request.UpdateLogin(this.key, this.id, args))];
});
});
};
KeePassHttpClient.prototype.request = function (request) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var body, _a, protocol, host, agent, fetchedResponse, err_1, response;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
body = JSON.stringify(request);
_a = url_1.default.parse(this.url), protocol = _a.protocol, host = _a.host;
agent = protocol === "http:" ? http_1.globalAgent : https_1.globalAgent;
_b.label = 1;
case 1:
_b.trys.push([1, 3, , 4]);
return [4 /*yield*/, node_fetch_1.default(this.url, {
agent: agent,
body: body,
compress: false,
method: "POST",
headers: tslib_1.__assign({ "accept": "application/json", "content-type": "application/json" }, (host ? { host: host } : {})),
})];
case 2:
fetchedResponse = _b.sent();
return [3 /*break*/, 4];
case 3:
err_1 = _b.sent();
throw new common_1.NetworkConnectionError(err_1.message);
case 4:
if (!fetchedResponse.ok) {
throw new common_1.NetworkResponseStatusCodeError(fetchedResponse.statusText, fetchedResponse.status);
}
return [4 /*yield*/, fetchedResponse.json()];
case 5:
response = _b.sent();
if (!response || !response.Success || response.Error) {
throw new common_1.NetworkResponseContentError("Remote service responded with an error response", request, response);
}
return [2 /*return*/, response];
}
});
});
};
return KeePassHttpClient;
}());
exports.KeePassHttpClient = KeePassHttpClient;
//# sourceMappingURL=client.js.map