Skip to content

Commit 83d67be

Browse files
committed
#18 | Popup Issues - association/connection status issues
1 parent be4d25a commit 83d67be

File tree

3 files changed

+99
-27
lines changed

3 files changed

+99
-27
lines changed

chromeipass/background/keepass.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,47 @@ keepass.isAssociated = function () {
385385
return !!(keepass.associated.value && keepass.associated.hash && keepass.associated.hash === keepass.databaseHash);
386386
};
387387

388+
keepass.isPreAssociated = function () {
389+
return (
390+
keepass.databaseHash !== "no-hash" &&
391+
(keepass.databaseHash in keepass.keyRing) &&
392+
keepass.getCryptoKey() &&
393+
!keepass.isEncryptionKeyUnrecognized &&
394+
!keepass.isDatabaseClosed &&
395+
keepass.isKeePassHttpAvailable
396+
);
397+
};
398+
399+
keepass.loadKeyRingAsync = function () {
400+
return new Promise(completePromise => {
401+
keepass._loadKeyRing(() => completePromise());
402+
});
403+
};
404+
405+
keepass.bootstrapAssociation = async function () {
406+
try {
407+
if (keepass.isAssociated()) {
408+
return;
409+
}
410+
await keepass.loadKeyRingAsync();
411+
await keepass._getDatabaseHashAsync(null, false);
412+
if (keepass.isPreAssociated() && !keepass.isAssociated()) {
413+
await keepass._testAssociationAsync(null, false);
414+
}
415+
} catch (e) {
416+
console.log("bootstrapAssociation error", e);
417+
}
418+
};
419+
420+
if (chrome && chrome.runtime) {
421+
try {
422+
chrome.runtime.onStartup.addListener(() => keepass.bootstrapAssociation());
423+
chrome.runtime.onInstalled.addListener(() => keepass.bootstrapAssociation());
424+
} catch (e) {
425+
console.log('bootstrapAssociation listener error', e);
426+
}
427+
}
428+
388429
keepass.send = function (request) {
389430
// ORIGINAL synchronous keepass.send replaced (do not use directly)
390431
throw new Error("keepass.send (synchronous) removed. Use keepass._sendAsync via higher level functions.");

chromeipass/background/service_worker.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,13 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
142142
return withTab(t => {
143143
if (typeof browserAction !== 'undefined') {
144144
if (!page.tabs[t.id]) page.tabs[t.id] = { stack: [], loginList: [], credentials: {}, errorMessage: null };
145-
page.tabs[t.id].loginList = (msg.args && msg.args[0]) ? (msg.args[0].map(u => ({ Login: u }))) : [];
145+
var stackData = {
146+
level: 1,
147+
iconType: "questionmark",
148+
popup: "popup_login.html"
149+
}
150+
browserAction.stackUnshift(stackData, t.id);
151+
page.tabs[t.id].loginList = (msg.args && msg.args[0]) ? (msg.args[0].map(u => ({ Login: u }))) : [];;
146152
browserAction.show(null, t);
147153
}
148154
sendResponse({ ok: true });
@@ -169,20 +175,20 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
169175
}, { ok: false });
170176
}
171177
case 'get_status': {
172-
return withTab(t => {
173-
const status = {
174-
configured: keepass.isConfigured(),
175-
associated: keepass.isAssociated(),
176-
identifier: (keepass.databaseHash in keepass.keyRing)
177-
? keepass.keyRing[keepass.databaseHash].id
178-
: null,
179-
keePassHttpAvailable: keepass.isKeePassHttpAvailable,
180-
databaseClosed: keepass.isDatabaseClosed,
181-
encryptionKeyUnrecognized: keepass.isEncryptionKeyUnrecognized,
182-
error: (t && page.tabs[t.id] && page.tabs[t.id].errorMessage) || undefined
183-
};
184-
sendResponse(status);
185-
}, { configured: false, associated: false });
178+
const tId = tab && tab.id;
179+
const error = (tId && page.tabs[tId] && page.tabs[tId].errorMessage) || undefined;
180+
sendResponse({
181+
configured: keepass.isConfigured(),
182+
associated: keepass.isAssociated(),
183+
identifier: (keepass.databaseHash in keepass.keyRing)
184+
? keepass.keyRing[keepass.databaseHash].id
185+
: null,
186+
keePassHttpAvailable: keepass.isKeePassHttpAvailable,
187+
databaseClosed: keepass.isDatabaseClosed,
188+
encryptionKeyUnrecognized: keepass.isEncryptionKeyUnrecognized,
189+
error
190+
});
191+
return;
186192
}
187193
case 'check_update_keepasshttp': {
188194
keepass.checkForNewKeePassHttpVersion()

chromeipass/popups/popup.js

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,50 @@ function status_response(r) {
2929
hide('configured-and-associated');
3030
hide('configured-not-associated');
3131

32+
// Plugin unreachable
33+
if (!r.keePassHttpAvailable) {
34+
setHTML('error-message', r.error || 'Cannot reach KeePassHttp. Is KeePass running?');
35+
show('error-encountered');
36+
return;
37+
}
3238

33-
if (!r.keePassHttpAvailable || r.databaseClosed) {
34-
setHTML('error-message', r.error);
39+
// Database closed
40+
if (r.databaseClosed) {
41+
setHTML('error-message', r.error || 'KeePass database is closed. Open it and reload status.');
3542
show('error-encountered');
36-
} else if (!r.configured) {
43+
return;
44+
}
45+
46+
// Not configured (no key stored for current hash)
47+
if (!r.configured) {
3748
show('not-configured');
38-
} else if (r.encryptionKeyUnrecognized) {
39-
show('need-reconfigure');
40-
setHTML('need-reconfigure-message', r.error);
41-
} else if (!r.associated) {
49+
return;
50+
}
51+
52+
// Key mismatch / encryption key lost
53+
if (r.encryptionKeyUnrecognized) {
4254
show('need-reconfigure');
43-
setHTML('need-reconfigure-message', r.error);
44-
} else if (typeof r.error !== 'undefined') {
55+
setHTML('need-reconfigure-message', r.error || 'Encryption key not recognized. Please re-associate.');
56+
return;
57+
}
58+
59+
// Configured but not yet associated this session
60+
if (!r.associated) {
61+
show('configured-not-associated');
62+
setHTML('unassociated-identifier', r.identifier);
63+
return;
64+
}
65+
66+
// Any explicit error after association
67+
if (typeof r.error !== 'undefined' && r.error) {
4568
show('error-encountered');
4669
setHTML('error-message', r.error);
47-
} else {
48-
show('configured-and-associated');
49-
setHTML('associated-identifier', r.identifier);
70+
return;
5071
}
72+
73+
// Success
74+
show('configured-and-associated');
75+
setHTML('associated-identifier', r.identifier || '(unknown)');
5176
}
5277

5378
document.addEventListener('DOMContentLoaded', () => {

0 commit comments

Comments
 (0)