From fd5fa666f9cbd3d598e98f420d44792a1905ec1a Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Thu, 16 Nov 2017 16:19:24 -0800 Subject: [PATCH] Retry failed signed key rotation; start rotation when registered (#1772) --- js/background.js | 3 ++- js/libtextsecure.js | 31 ++++++++++++++++++----------- js/rotate_signed_prekey_listener.js | 13 +++++++++++- libtextsecure/account_manager.js | 31 ++++++++++++++++++----------- 4 files changed, 52 insertions(+), 26 deletions(-) diff --git a/js/background.js b/js/background.js index 55500bd21fe..e3cde97ea3e 100644 --- a/js/background.js +++ b/js/background.js @@ -99,19 +99,20 @@ console.log('listening for registration events'); Whisper.events.on('registration_done', function() { console.log('handling registration event'); + Whisper.RotateSignedPreKeyListener.init(Whisper.events); connect(true); }); var appView = window.owsDesktopApp.appView = new Whisper.AppView({el: $('body')}); Whisper.WallClockListener.init(Whisper.events); - Whisper.RotateSignedPreKeyListener.init(Whisper.events); Whisper.ExpiringMessagesListener.init(Whisper.events); if (Whisper.Import.isIncomplete()) { console.log('Import was interrupted, showing import error screen'); appView.openImporter(); } else if (Whisper.Registration.everDone()) { + Whisper.RotateSignedPreKeyListener.init(Whisper.events); connect(); appView.openInbox({ initialLoadComplete: initialLoadComplete diff --git a/js/libtextsecure.js b/js/libtextsecure.js index d2788c4587d..f61fb3cce4c 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -37995,19 +37995,26 @@ var TextSecureServer = (function() { keyId : res.keyId, publicKey : res.keyPair.pubKey, signature : res.signature - }).then(function() { - textsecure.storage.put('signedKeyId', signedKeyId + 1); - textsecure.storage.remove('signedKeyRotationRejected'); - return store.storeSignedPreKey(res.keyId, res.keyPair).then(function() { - return cleanSignedPreKeys(); - }); - }).catch(function(e) { - if (e instanceof Error && e.name == 'HTTPError' && e.code >= 400 && e.code <= 599) { - var rejections = 1 + textsecure.storage.get('signedKeyRotationRejected', 0); - textsecure.storage.put('signedKeyRotationRejected', rejections); - console.log('Signed key rotation rejected count:', rejections); - } }); + }).then(function() { + textsecure.storage.put('signedKeyId', signedKeyId + 1); + textsecure.storage.remove('signedKeyRotationRejected'); + return store.storeSignedPreKey(res.keyId, res.keyPair).then(function() { + return cleanSignedPreKeys(); + }); + }).catch(function(e) { + console.log( + 'rotateSignedPrekey error:', + e && e.stack ? e.stack : e + ); + + if (e instanceof Error && e.name == 'HTTPError' && e.code >= 400 && e.code <= 599) { + var rejections = 1 + textsecure.storage.get('signedKeyRotationRejected', 0); + textsecure.storage.put('signedKeyRotationRejected', rejections); + console.log('Signed key rotation rejected count:', rejections); + } + + throw e; }); }.bind(this)); }, diff --git a/js/rotate_signed_prekey_listener.js b/js/rotate_signed_prekey_listener.js index 43fe6ff8277..a52ece8e39f 100644 --- a/js/rotate_signed_prekey_listener.js +++ b/js/rotate_signed_prekey_listener.js @@ -17,7 +17,10 @@ function run() { console.log('Rotating signed prekey...'); - getAccountManager().rotateSignedPreKey(); + getAccountManager().rotateSignedPreKey().catch(function() { + console.log('rotateSignedPrekey() failed. Trying again in five seconds'); + setTimeout(runWhenOnline, 5000); + }); scheduleNextRotation(); setTimeoutForNextRun(); } @@ -26,6 +29,7 @@ if (navigator.onLine) { run(); } else { + console.log('We are offline; keys will be rotated when we are next online'); var listener = function() { window.removeEventListener('online', listener); run(); @@ -52,8 +56,15 @@ timeout = setTimeout(runWhenOnline, waitTime); } + var initComplete; Whisper.RotateSignedPreKeyListener = { init: function(events) { + if (initComplete) { + console.log('Rotate signed prekey listener: Already initialized'); + return; + } + initComplete = true; + if (Whisper.Registration.isDone()) { setTimeoutForNextRun(); } diff --git a/libtextsecure/account_manager.js b/libtextsecure/account_manager.js index 61c06a44c6f..f0bfb60191a 100644 --- a/libtextsecure/account_manager.js +++ b/libtextsecure/account_manager.js @@ -132,19 +132,26 @@ keyId : res.keyId, publicKey : res.keyPair.pubKey, signature : res.signature - }).then(function() { - textsecure.storage.put('signedKeyId', signedKeyId + 1); - textsecure.storage.remove('signedKeyRotationRejected'); - return store.storeSignedPreKey(res.keyId, res.keyPair).then(function() { - return cleanSignedPreKeys(); - }); - }).catch(function(e) { - if (e instanceof Error && e.name == 'HTTPError' && e.code >= 400 && e.code <= 599) { - var rejections = 1 + textsecure.storage.get('signedKeyRotationRejected', 0); - textsecure.storage.put('signedKeyRotationRejected', rejections); - console.log('Signed key rotation rejected count:', rejections); - } }); + }).then(function() { + textsecure.storage.put('signedKeyId', signedKeyId + 1); + textsecure.storage.remove('signedKeyRotationRejected'); + return store.storeSignedPreKey(res.keyId, res.keyPair).then(function() { + return cleanSignedPreKeys(); + }); + }).catch(function(e) { + console.log( + 'rotateSignedPrekey error:', + e && e.stack ? e.stack : e + ); + + if (e instanceof Error && e.name == 'HTTPError' && e.code >= 400 && e.code <= 599) { + var rejections = 1 + textsecure.storage.get('signedKeyRotationRejected', 0); + textsecure.storage.put('signedKeyRotationRejected', rejections); + console.log('Signed key rotation rejected count:', rejections); + } + + throw e; }); }.bind(this)); },