Add signed key rotation scheduler

Rotate signed prekey every 48hrs, waiting for online access if
necessary. After a rotation attempt is made, schedule the next run for
48hrs in the future.

We use a timeout to "wake up" and handle the rotation. This timeout gets
set on startup and whenever the next rotation time is changed. For
paranoia's sake, always clear the current timeout before setting the
next one.

Since new registrations necessarily upload new signed keys, we reset the
scheduled time to T+48hrs on `registration_done` events.

// FREEBIE
This commit is contained in:
lilia 2017-02-16 11:59:32 -08:00
parent b92dd45a22
commit 536dd7b951
3 changed files with 61 additions and 0 deletions

View File

@ -541,6 +541,7 @@
<script type='text/javascript' src='js/views/identicon_svg_view.js'></script>
<script type='text/javascript' src='js/views/settings_view.js'></script>
<script type='text/javascript' src='js/rotate_signed_prekey_listener.js'></script>
<script type='text/javascript' src='js/background.js'></script>
</head>
<body>

View File

@ -84,6 +84,8 @@
if (open) {
openInbox();
}
RotateSignedPreKeyListener.init();
});
window.getSyncRequest = function() {

View File

@ -0,0 +1,58 @@
/*
* vim: ts=4:sw=4:expandtab
*/
;(function () {
'use strict';
var ROTATION_INTERVAL = 48 * 60 * 60 * 1000;
var timeout;
function scheduleNextRotation() {
var now = Date.now();
var nextTime = now + ROTATION_INTERVAL;
storage.put('nextSignedKeyRotationTime', nextTime);
}
function run() {
console.log('Rotating signed prekey...');
getAccountManager().rotateSignedPreKey();
scheduleNextRotation();
setTimeoutForNextRun();
}
function runWhenOnline() {
if (navigator.onLine) {
run();
} else {
var listener = function() {
window.removeEventListener('online', listener);
run();
};
window.addEventListener('online', listener);
}
}
function setTimeoutForNextRun() {
var now = Date.now();
var scheduledTime = storage.get('nextSignedKeyRotationTime', now);
console.log('Next signed key rotation scheduled for', new Date(scheduledTime));
var waitTime = scheduledTime - now;
if (waitTime < 0) {
waitTime = 0;
}
clearTimeout(timeout);
timeout = setTimeout(runWhenOnline, waitTime);
}
window.RotateSignedPreKeyListener = {
init: function() {
if (Whisper.Registration.isDone()) {
setTimeoutForNextRun();
}
extension.on('registration_done', function() {
scheduleNextRotation();
setTimeoutForNextRun();
});
}
};
}());