From 80d32103d14472fd60da40f78a9378acaccfc7e6 Mon Sep 17 00:00:00 2001 From: lilia Date: Mon, 2 Nov 2015 13:27:36 -0800 Subject: [PATCH] Clear session store when re-registering When we re-register, our deviceId might change, which makes our sessions are no longer valid since the recipient will see us as a new device. Fixes #388 --- js/axolotl_store.js | 7 +++++++ js/libtextsecure.js | 29 ++++++++++++++++------------- libtextsecure/account_manager.js | 29 ++++++++++++++++------------- test/storage_test.js | 11 +++++++++++ 4 files changed, 50 insertions(+), 26 deletions(-) diff --git a/js/axolotl_store.js b/js/axolotl_store.js index b270bf7a..bc2d692c 100644 --- a/js/axolotl_store.js +++ b/js/axolotl_store.js @@ -228,6 +228,13 @@ }); }); }, + clearSessionStore: function() { + return new Promise(function(resolve) { + var sessions = new SessionCollection(); + sessions.sync('delete', sessions, {}).always(resolve); + }); + + }, getIdentityKey: function(identifier) { if (identifier === null || identifier === undefined) throw new Error("Tried to get identity key for undefined/null key"); diff --git a/js/libtextsecure.js b/js/libtextsecure.js index 36a64012..81974a67 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -39204,21 +39204,24 @@ var TextSecureServer = (function() { return this.server.confirmCode( number, verificationCode, password, signalingKey, registrationId, deviceName ).then(function(response) { - textsecure.storage.remove('identityKey'); - textsecure.storage.remove('signaling_key'); - textsecure.storage.remove('password'); - textsecure.storage.remove('registrationId'); - textsecure.storage.remove('number_id'); - textsecure.storage.remove('regionCode'); + return textsecure.storage.axolotl.clearSessionStore().then(function() { + textsecure.storage.remove('identityKey'); + textsecure.storage.remove('signaling_key'); + textsecure.storage.remove('password'); + textsecure.storage.remove('registrationId'); + textsecure.storage.remove('number_id'); + textsecure.storage.remove('device_name'); + textsecure.storage.remove('regionCode'); - textsecure.storage.put('identityKey', identityKeyPair); - textsecure.storage.put('signaling_key', signalingKey); - textsecure.storage.put('password', password); - textsecure.storage.put('registrationId', registrationId); + textsecure.storage.put('identityKey', identityKeyPair); + textsecure.storage.put('signaling_key', signalingKey); + textsecure.storage.put('password', password); + textsecure.storage.put('registrationId', registrationId); - textsecure.storage.user.setNumberAndDeviceId(number, response.deviceId || 1, deviceName); - textsecure.storage.put('regionCode', libphonenumber.util.getRegionCodeForNumber(number)); - this.server.username = textsecure.storage.get('number_id'); + textsecure.storage.user.setNumberAndDeviceId(number, response.deviceId || 1, deviceName); + textsecure.storage.put('regionCode', libphonenumber.util.getRegionCodeForNumber(number)); + this.server.username = textsecure.storage.get('number_id'); + }.bind(this)); }.bind(this)); }, generateKeys: function (count, progressCallback) { diff --git a/libtextsecure/account_manager.js b/libtextsecure/account_manager.js index 3ba747b7..47197248 100644 --- a/libtextsecure/account_manager.js +++ b/libtextsecure/account_manager.js @@ -97,21 +97,24 @@ return this.server.confirmCode( number, verificationCode, password, signalingKey, registrationId, deviceName ).then(function(response) { - textsecure.storage.remove('identityKey'); - textsecure.storage.remove('signaling_key'); - textsecure.storage.remove('password'); - textsecure.storage.remove('registrationId'); - textsecure.storage.remove('number_id'); - textsecure.storage.remove('regionCode'); + return textsecure.storage.axolotl.clearSessionStore().then(function() { + textsecure.storage.remove('identityKey'); + textsecure.storage.remove('signaling_key'); + textsecure.storage.remove('password'); + textsecure.storage.remove('registrationId'); + textsecure.storage.remove('number_id'); + textsecure.storage.remove('device_name'); + textsecure.storage.remove('regionCode'); - textsecure.storage.put('identityKey', identityKeyPair); - textsecure.storage.put('signaling_key', signalingKey); - textsecure.storage.put('password', password); - textsecure.storage.put('registrationId', registrationId); + textsecure.storage.put('identityKey', identityKeyPair); + textsecure.storage.put('signaling_key', signalingKey); + textsecure.storage.put('password', password); + textsecure.storage.put('registrationId', registrationId); - textsecure.storage.user.setNumberAndDeviceId(number, response.deviceId || 1, deviceName); - textsecure.storage.put('regionCode', libphonenumber.util.getRegionCodeForNumber(number)); - this.server.username = textsecure.storage.get('number_id'); + textsecure.storage.user.setNumberAndDeviceId(number, response.deviceId || 1, deviceName); + textsecure.storage.put('regionCode', libphonenumber.util.getRegionCodeForNumber(number)); + this.server.username = textsecure.storage.get('number_id'); + }.bind(this)); }.bind(this)); }, generateKeys: function (count, progressCallback) { diff --git a/test/storage_test.js b/test/storage_test.js index 8dba41ec..d8226d72 100644 --- a/test/storage_test.js +++ b/test/storage_test.js @@ -114,6 +114,17 @@ describe("AxolotlStore", function() { }); }).then(done,done); }); + it ('clears the session store', function(done) { + var testRecord = "an opaque string"; + store.putSession(identifier + '.1', testRecord).then(function() { + return store.clearSessionStore().then(function() { + return store.getSession(identifier + '.1').then(function(record) { + assert.isUndefined(record); + }); + }); + }).then(done,done); + + }); it('returns deviceIds for a number', function(done) { var testRecord = "an opaque string"; var devices = [1, 2, 3].map(function(deviceId) {