keewebhttp: format handling

This commit is contained in:
antelle 2017-05-24 00:20:43 +02:00
parent 2ec338341e
commit 0f33a73441
2 changed files with 82 additions and 7 deletions

View File

@ -11,7 +11,7 @@
"licence": "MIT",
"url": "https://plugins.keeweb.info/plugins/keewebhttp",
"resources": {
"js": "LOKItHFTqpYOrqC5L7/P75w7r1sBMl1ZSGIdta3ifcIOQ7BJKAIH1cMNtjGuMIVZWLM7w3APjLwoeQ3pBzw91m09yGeBFY/aMQimVUJ9HV/NKls7YZN48sBtkkdR5ByIXSxniDbHsUIQJgOeTsNyDPy9jCN3tko/jO9tNG4cSgB5O77A0OYVZEbV8MtKwGgr6MNGG4mRdg+dN/23Xd+O8zgFrqCADXUjnMAQ+13y0upnIPbO6Ory1Ou7vtzssqSIpakkpxvnGqV/S25lxzLsdwtqQZRGJB4RJrY1SiB6FjZT0YuN1LqfugWEyIRbeyzdNMo7oCEiwfGR7vFnODq3kg=="
"js": "to0fWxarSAgke4VqZoV3P/dO92ea7RDlmCCyXJEbFOKER+JpJraPsPn0oeWoSzY396T7e2hRxM8HNo7rcnZtkojtCNALmOmz5aSg1cD8CmB3jYJJaG3r5t7WduyrOrha9+tU8XM5faAYeg6lrJb42D6e67/GHZG3GkPSpq3963Wjv36lb9/XQtUkgAOpV5dnuJhNdostFqngWnadvov1NnSNZtr6fbSoq+4worBPUnZTuDqzPTW0QIa2rfoA+Qsg4DLUBnuRLrgTA/8ObzI+qH5aKujIk4sBw6QZffGMpzESFlockS65cr5gmSu152A5oEgkqXT1sRKhMDC1qrFgnA=="
},
"publicKey": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0oZB2Kt7AzRFNqf8FuO3C3kepHPAIQYiDPYdQxHcsiaFCwyKVx6K1cE/3vBhb8/2rj+QIIWNfAAuu1Y+2VK90ZBeq6HciukWzQRO/HWhfdy0c7JwDAslmyGI5olj0ZQkNLhkde1MiMxjDPpRhZtdJaryVO5cFJaJESpv3dV6m0qXsaQCluWYOSNfSjP9C8o2zRVjSi3ZQZnZIV5pnk9K2MtlZIPXrN9iJiM5zZ9DTSnqApI6dC9mX4R3LvGN+GTovm9C8Crl+qb106nGRR3LcweicDnPyMtZLa/E0DBpWYxUVLDp6WeLhxoUBr+6+t3Xp9IDnPoANDQXJXD0f1vQxQIDAQAB",
"desktop": true,

View File

@ -10,7 +10,9 @@ function run() {
const path = nodeRequire('path');
const electron = nodeRequire('electron');
const kdbxweb = require('kdbxweb');
const AppModel = require('models/app-model');
const EntryModel = require('models/entry-model');
const AutoTypeFilter = require('auto-type/auto-type-filter');
const Logger = require('util/logger');
const Alerts = require('comp/alerts');
@ -19,8 +21,12 @@ function run() {
const Version = '1.8.4.2';
const DebugMode = true;
const FileReadTimeout = 500;
const EntryTitle = 'KeePassHttp Settings';
const EntryFieldPrefix = 'AES Key: ';
const keys = {};
const addedKeys = {};
const logger = new Logger('keewebhttp');
let uninstalled;
@ -34,6 +40,7 @@ function run() {
addEventListeners();
startServer();
readAllKeys();
function addEventListeners() {
AppModel.instance.files.on('add', fileOpened);
@ -43,10 +50,6 @@ function run() {
AppModel.instance.files.off('add', fileOpened);
}
function fileOpened(e) {
console.log('file opened', e);
}
function startServer() {
if (uninstalled) {
return;
@ -114,6 +117,78 @@ function run() {
}
}
function fileOpened(file) {
setTimeout(() => {
readKeys(file);
writeAddedKeys(file);
}, FileReadTimeout);
}
function readAllKeys() {
AppModel.instance.files.forEach(file => readKeys(file));
}
function readKeys(file) {
if (uninstalled) {
return;
}
const entry = getSettingsEntry(file);
if (!entry) {
return;
}
for (const field of Object.keys(entry.fields)) {
if (field.startsWith(EntryFieldPrefix)) {
const key = field.replace(EntryFieldPrefix, '');
let value = entry.fields[field];
if (value && value.isProtected) {
value = value.getText();
}
if (key && value && !keys[key]) {
keys[key] = value;
}
}
}
}
function writeAddedKeysToAllFiles() {
AppModel.instance.files.forEach(file => {
writeAddedKeys(file);
});
}
function writeAddedKeys(file) {
if (uninstalled || !Object.keys(addedKeys).length) {
return;
}
let settingsEntry = getSettingsEntry(file);
if (!settingsEntry) {
settingsEntry = EntryModel.newEntry(file.get('groups').first(), file);
settingsEntry.setField('Title', EntryTitle);
}
for (const key of Object.keys(addedKeys)) {
const keyFieldName = EntryFieldPrefix + key;
const value = addedKeys[key];
let oldValue = settingsEntry.fields[keyFieldName];
if (oldValue && oldValue.isProtected) {
oldValue = oldValue.getText();
}
if (oldValue !== value) {
settingsEntry.setField(keyFieldName, kdbxweb.ProtectedValue.fromString(value));
}
}
file.reload();
}
function getSettingsEntry(file) {
let entry = null;
file.get('groups').first().forEachOwnEntry({ textLower: EntryTitle.toLowerCase() }, e => {
if (e.title === EntryTitle) {
entry = e;
}
});
return entry;
}
class RequestContext {
constructor(postData) {
this.postData = postData;
@ -209,7 +284,6 @@ function run() {
getKeyById() {
let key = keys[this.req.Id];
if (!key) {
key = ''; // TODO: read key
keys[this.req.Id] = key;
}
return key;
@ -217,7 +291,8 @@ function run() {
saveKeyWithId() {
keys[this.req.Id] = this.req.Key;
// TODO: write key
addedKeys[this.req.Id] = this.req.Key;
writeAddedKeysToAllFiles();
}
verifyRequest() {