save privkey to sw to decrypt notifications

This commit is contained in:
Martti Malmi 2020-07-09 11:02:57 +03:00
parent 6f4bc1b342
commit a8ac5f1ebb
3 changed files with 19 additions and 6 deletions

View File

@ -217,9 +217,13 @@ function init() {
}
});
$('.logout-button').click(() => {
$('.logout-button').click(async () => {
// reg.active.postMessage({key: null}); TODO - also remove subscription from your chats
localStorage.removeItem('chatKeyPair');
const reg = await navigator.serviceWorker.getRegistration();
if (reg) {
reg.active.postMessage({key: null});
}
location.reload(); // ensure that everything is reset (especially on the gun side). TODO: without reload
});
$('#show-existing-account-login').click(showSwitchAccount);

7
src/js/lib/localforage.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,7 +1,11 @@
const window = self;
self.importScripts('js/lib/gun.js', 'js/lib/sea.js');
self.importScripts('js/lib/gun.js', 'js/lib/sea.js', 'js/lib/localforage.min.js');
var CACHE_NAME = 'iris-messenger-cache-v1';
localforage.getItem('swIrisKey', (err, val) => {
self.irisKey = (self.irisKey || JSON.parse(val));
});
// stale-while-revalidate
if (self.location.host.indexOf('localhost') !== 0) {
self.addEventListener('fetch', function(event) {
@ -21,20 +25,18 @@ if (self.location.host.indexOf('localhost') !== 0) {
}
self.onmessage = function(msg) {
if (msg.data.key) {
if (msg.data.hasOwnProperty('key')) {
self.irisKey = msg.data.key;
localforage.setItem('swIrisKey', JSON.stringify(self.irisKey));
}
}
self.addEventListener('push', async ev => {
const data = ev.data.json();
console.log('got push', data);
if (self.irisKey && data.from && data.from.epub) {
const secret = await Gun.SEA.secret(data.from.epub, self.irisKey);
data.title = await Gun.SEA.decrypt(data.title, secret);
data.body = await Gun.SEA.decrypt(data.body, secret);
} else {
console.log('self.irisKey', !!self.irisKey, 'data.from', !!data.from, 'data.from.epub', !!data.from.epub);
}
if (data.title.indexOf('SEA{') === 0) {
data.title = '';