From 454b4726bd235f3d6b3b2df6dde3e9404fcdcf6e Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Tue, 24 Mar 2015 15:48:59 -0700 Subject: [PATCH] Replace load/decode/index around our own number with helpers --- Gruntfile.js | 1 + js/index.js | 2 +- js/libtextsecure.js | 70 +++++++++++++++++++++++++++------ js/options.js | 6 +-- js/register.js | 6 +-- js/views/conversation_view.js | 4 +- js/views/message_detail_view.js | 4 +- libtextsecure/api.js | 4 +- libtextsecure/helpers.js | 8 ++-- libtextsecure/sendmessage.js | 7 ++-- libtextsecure/storage/groups.js | 4 +- libtextsecure/storage/user.js | 45 +++++++++++++++++++++ 12 files changed, 120 insertions(+), 41 deletions(-) create mode 100644 libtextsecure/storage/user.js diff --git a/Gruntfile.js b/Gruntfile.js index 7ca42b19e06b..2ab99ca49fd8 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -43,6 +43,7 @@ module.exports = function(grunt) { 'libtextsecure/crypto.js', 'libtextsecure/storage.js', + 'libtextsecure/storage/user.js', 'libtextsecure/storage/devices.js', 'libtextsecure/storage/groups.js', 'libtextsecure/protobufs.js', diff --git a/js/index.js b/js/index.js index a5a5950bdf93..812d2a355298 100644 --- a/js/index.js +++ b/js/index.js @@ -19,7 +19,7 @@ var bg = extension.windows.getBackground(); window.Whisper = window.Whisper || {}; - if (bg.textsecure.storage.getUnencrypted("number_id") === undefined) { + if (bg.textsecure.storage.user.getNumber() === undefined) { window.location = '/options.html'; } else { new bg.Whisper.InboxView().$el.prependTo(bg.$('body',document)); diff --git a/js/libtextsecure.js b/js/libtextsecure.js index af198dd377b1..5e733f43d013 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -38004,6 +38004,53 @@ window.axolotl.sessions = { })(); +/* vim: ts=4:sw=4 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +'use strict'; + +;(function() { + /********************************************* + *** Utilities to store data about the user *** + **********************************************/ + window.textsecure = window.textsecure || {}; + window.textsecure.storage = window.textsecure.storage || {}; + + window.textsecure.storage.user = { + setNumberAndDeviceId: function(number, deviceId) { + textsecure.storage.putUnencrypted("number_id", number + "." + deviceId); + }, + + getNumber: function(key, defaultValue) { + var number_id = textsecure.storage.getUnencrypted("number_id"); + if (number_id === undefined) + return undefined; + return textsecure.utils.unencodeNumber(number_id)[0]; + }, + + getDeviceId: function(key) { + var number_id = textsecure.storage.getUnencrypted("number_id"); + if (number_id === undefined) + return undefined; + return textsecure.utils.unencodeNumber(number_id)[1]; + } + }; +})(); + + /* vim: ts=4:sw=4 * * This program is free software: you can redistribute it and/or modify @@ -38213,7 +38260,7 @@ window.axolotl.sessions = { while (groupId === undefined || textsecure.storage.getEncrypted("group" + groupId) !== undefined) groupId = getString(textsecure.crypto.getRandomBytes(16)); - var me = textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0]; + var me = textsecure.storage.user.getNumber(); var haveMe = false; var finalNumbers = []; for (var i in numbers) { @@ -38251,7 +38298,7 @@ window.axolotl.sessions = { if (group === undefined) return undefined; - var me = textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0]; + var me = textsecure.storage.user.getNumber(); if (number == me) throw new Error("Cannot remove ourselves from a group, leave the group instead"); @@ -38738,7 +38785,7 @@ textsecure.processDecrypted = function(decrypted, source) { if (decrypted.flags == null) decrypted.flags = 0; - if (decrypted.sync !== null && textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0] != source) + if (decrypted.sync !== null && textsecure.storage.user.getNumber() != source) throw new Error("Got sync context on a message not from a peer device"); if ((decrypted.flags & textsecure.protobuf.PushMessageContent.Flags.END_SESSION) @@ -38843,8 +38890,7 @@ window.textsecure.registerSingleDevice = function(number, verificationCode, step textsecure.storage.putUnencrypted("registrationId", registrationId); return textsecure.api.confirmCode(number, verificationCode, password, signalingKey, registrationId, true).then(function() { - var numberId = number + ".1"; - textsecure.storage.putUnencrypted("number_id", numberId); + textsecure.storage.user.setNumberAndDeviceId(number, 1); textsecure.storage.putUnencrypted("regionCode", libphonenumber.util.getRegionCodeForNumber(number)); stepDone(1); @@ -38874,8 +38920,7 @@ window.textsecure.registerSecondDevice = function(encodedProvisionEnvelope, cryp textsecure.storage.putUnencrypted("registrationId", registrationId); return textsecure.api.confirmCode(identityKey.number, identityKey.provisioningCode, password, signalingKey, registrationId, false).then(function(result) { - var numberId = identityKey.number + "." + result.deviceId; - textsecure.storage.putUnencrypted("number_id", numberId); + textsecure.storage.user.setNumberAndDeviceId(identityKey.number, result.deviceId); textsecure.storage.putUnencrypted("regionCode", libphonenumber.util.getRegionCodeForNumber(identityKey.number)); stepDone(2); @@ -39084,7 +39129,7 @@ window.textsecure.api = function () { } if (param.do_auth) { - param.user = textsecure.storage.getUnencrypted("number_id"); + param.user = textsecure.storage.user.getNumber() + "." + textsecure.storage.user.getDeviceId(); param.password = textsecure.storage.getEncrypted("password"); } @@ -39310,7 +39355,7 @@ window.textsecure.api = function () { var URL = URL_BASE.replace(/^http/g, 'ws') + url + '/?'; var params = ''; if (auth) { - var user = textsecure.storage.getUnencrypted("number_id"); + var user = textsecure.storage.user.getNumber() + "." + textsecure.storage.user.getDeviceId(); var password = textsecure.storage.getEncrypted("password"); var params = 'login=%2B' + encodeURIComponent(user.substring(1)) + '&password=' + encodeURIComponent(password); } @@ -39586,9 +39631,8 @@ window.textsecure.messaging = function() { } var sendSyncMessage = function(message, timestamp, destination) { - var numberDevice = textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id")); - var myNumber = numberDevice[0]; - var myDevice = numberDevice[1]; + var myNumber = textsecure.storage.user.getNumber(); + var myDevice = textsecure.storage.user.getDeviceId(); if (myDevice != 1) { var sync_message = textsecure.protobuf.PushMessageContent.decode(message.encode()); sync_message.sync = new textsecure.protobuf.PushMessageContent.SyncMessageContext(); @@ -39600,7 +39644,7 @@ window.textsecure.messaging = function() { var sendGroupProto = function(numbers, proto, timestamp) { timestamp = timestamp || Date.now(); - var me = textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0]; + var me = textsecure.storage.user.getNumber(); numbers = numbers.filter(function(number) { return number != me; }); return new Promise(function(resolve, reject) { diff --git a/js/options.js b/js/options.js index 56d63e360ca2..6474a32dc07d 100644 --- a/js/options.js +++ b/js/options.js @@ -37,11 +37,7 @@ $(function() { if (textsecure.registration.isDone()) { - $('#complete-number').text( - textsecure.utils.unencodeNumber( - textsecure.storage.getUnencrypted("number_id") - )[0] - );//TODO: no + $('#complete-number').text(textsecure.storage.user.getNumber()); $('#setup-complete').show().addClass('in'); initOptions(); } else { diff --git a/js/register.js b/js/register.js index ea9830f666f6..3775c0ff8a09 100644 --- a/js/register.js +++ b/js/register.js @@ -36,9 +36,9 @@ var phoneView = new Whisper.PhoneInputView({el: $('#phone-number-input')}); phoneView.$el.find('input.number').intlTelInput(); - var number = textsecure.storage.getUnencrypted('number_id'); + var number = textsecure.storage.user.getNumber(); if (number) { - $('input.number').val(number.split('.')[0]); + $('input.number').val(number); } $('input.number').on('validation', function() { @@ -98,7 +98,7 @@ textsecure.storage.putUnencrypted('registrationId', registrationId); textsecure.storage.putEncrypted('signaling_key', signalingKey); textsecure.storage.putEncrypted('password', password); - textsecure.storage.putUnencrypted('number_id', number + '.1'); + textsecure.storage.user.setNumberAndDeviceId(number, 1); textsecure.storage.putUnencrypted('regionCode', libphonenumber.util.getRegionCodeForNumber(number)); log('verifying code'); diff --git a/js/views/conversation_view.js b/js/views/conversation_view.js index 907f136708fe..380d808e8298 100644 --- a/js/views/conversation_view.js +++ b/js/views/conversation_view.js @@ -74,9 +74,7 @@ var view = new Whisper.KeyVerificationView({ model: { their_key: textsecure.storage.devices.getIdentityKeyForNumber(number), - your_key: textsecure.storage.devices.getIdentityKeyForNumber( - textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0] - ) + your_key: textsecure.storage.devices.getIdentityKeyForNumber(textsecure.storage.user.getNumber()) } }); this.$el.hide(); diff --git a/js/views/message_detail_view.js b/js/views/message_detail_view.js index 5152a2345b92..9b54ef22daba 100644 --- a/js/views/message_detail_view.js +++ b/js/views/message_detail_view.js @@ -58,9 +58,7 @@ var view = new Whisper.KeyVerificationView({ model: { their_key: textsecure.storage.devices.getIdentityKeyForNumber(number), - your_key: textsecure.storage.devices.getIdentityKeyForNumber( - textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0] - ) + your_key: textsecure.storage.devices.getIdentityKeyForNumber(textsecure.storage.user.getNumber()) } }); this.$el.hide(); diff --git a/libtextsecure/api.js b/libtextsecure/api.js index 91dfcc0107ce..848731bad9cf 100644 --- a/libtextsecure/api.js +++ b/libtextsecure/api.js @@ -95,7 +95,7 @@ window.textsecure.api = function () { } if (param.do_auth) { - param.user = textsecure.storage.getUnencrypted("number_id"); + param.user = textsecure.storage.user.getNumber() + "." + textsecure.storage.user.getDeviceId(); param.password = textsecure.storage.getEncrypted("password"); } @@ -321,7 +321,7 @@ window.textsecure.api = function () { var URL = URL_BASE.replace(/^http/g, 'ws') + url + '/?'; var params = ''; if (auth) { - var user = textsecure.storage.getUnencrypted("number_id"); + var user = textsecure.storage.user.getNumber() + "." + textsecure.storage.user.getDeviceId(); var password = textsecure.storage.getEncrypted("password"); var params = 'login=%2B' + encodeURIComponent(user.substring(1)) + '&password=' + encodeURIComponent(password); } diff --git a/libtextsecure/helpers.js b/libtextsecure/helpers.js index 2f186b74af79..a074e1dd3b8d 100644 --- a/libtextsecure/helpers.js +++ b/libtextsecure/helpers.js @@ -161,7 +161,7 @@ textsecure.processDecrypted = function(decrypted, source) { if (decrypted.flags == null) decrypted.flags = 0; - if (decrypted.sync !== null && textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0] != source) + if (decrypted.sync !== null && textsecure.storage.user.getNumber() != source) throw new Error("Got sync context on a message not from a peer device"); if ((decrypted.flags & textsecure.protobuf.PushMessageContent.Flags.END_SESSION) @@ -266,8 +266,7 @@ window.textsecure.registerSingleDevice = function(number, verificationCode, step textsecure.storage.putUnencrypted("registrationId", registrationId); return textsecure.api.confirmCode(number, verificationCode, password, signalingKey, registrationId, true).then(function() { - var numberId = number + ".1"; - textsecure.storage.putUnencrypted("number_id", numberId); + textsecure.storage.user.setNumberAndDeviceId(number, 1); textsecure.storage.putUnencrypted("regionCode", libphonenumber.util.getRegionCodeForNumber(number)); stepDone(1); @@ -297,8 +296,7 @@ window.textsecure.registerSecondDevice = function(encodedProvisionEnvelope, cryp textsecure.storage.putUnencrypted("registrationId", registrationId); return textsecure.api.confirmCode(identityKey.number, identityKey.provisioningCode, password, signalingKey, registrationId, false).then(function(result) { - var numberId = identityKey.number + "." + result.deviceId; - textsecure.storage.putUnencrypted("number_id", numberId); + textsecure.storage.user.setNumberAndDeviceId(identityKey.number, result.deviceId); textsecure.storage.putUnencrypted("regionCode", libphonenumber.util.getRegionCodeForNumber(identityKey.number)); stepDone(2); diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index 98dd92567be7..bd231836b88b 100644 --- a/libtextsecure/sendmessage.js +++ b/libtextsecure/sendmessage.js @@ -256,9 +256,8 @@ window.textsecure.messaging = function() { } var sendSyncMessage = function(message, timestamp, destination) { - var numberDevice = textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id")); - var myNumber = numberDevice[0]; - var myDevice = numberDevice[1]; + var myNumber = textsecure.storage.user.getNumber(); + var myDevice = textsecure.storage.user.getDeviceId(); if (myDevice != 1) { var sync_message = textsecure.protobuf.PushMessageContent.decode(message.encode()); sync_message.sync = new textsecure.protobuf.PushMessageContent.SyncMessageContext(); @@ -270,7 +269,7 @@ window.textsecure.messaging = function() { var sendGroupProto = function(numbers, proto, timestamp) { timestamp = timestamp || Date.now(); - var me = textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0]; + var me = textsecure.storage.user.getNumber(); numbers = numbers.filter(function(number) { return number != me; }); return new Promise(function(resolve, reject) { diff --git a/libtextsecure/storage/groups.js b/libtextsecure/storage/groups.js index 0ef55a11e2f0..e8039460d9e4 100644 --- a/libtextsecure/storage/groups.js +++ b/libtextsecure/storage/groups.js @@ -31,7 +31,7 @@ while (groupId === undefined || textsecure.storage.getEncrypted("group" + groupId) !== undefined) groupId = getString(textsecure.crypto.getRandomBytes(16)); - var me = textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0]; + var me = textsecure.storage.user.getNumber(); var haveMe = false; var finalNumbers = []; for (var i in numbers) { @@ -69,7 +69,7 @@ if (group === undefined) return undefined; - var me = textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0]; + var me = textsecure.storage.user.getNumber(); if (number == me) throw new Error("Cannot remove ourselves from a group, leave the group instead"); diff --git a/libtextsecure/storage/user.js b/libtextsecure/storage/user.js new file mode 100644 index 000000000000..c437201eb48d --- /dev/null +++ b/libtextsecure/storage/user.js @@ -0,0 +1,45 @@ +/* vim: ts=4:sw=4 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +'use strict'; + +;(function() { + /********************************************* + *** Utilities to store data about the user *** + **********************************************/ + window.textsecure = window.textsecure || {}; + window.textsecure.storage = window.textsecure.storage || {}; + + window.textsecure.storage.user = { + setNumberAndDeviceId: function(number, deviceId) { + textsecure.storage.putUnencrypted("number_id", number + "." + deviceId); + }, + + getNumber: function(key, defaultValue) { + var number_id = textsecure.storage.getUnencrypted("number_id"); + if (number_id === undefined) + return undefined; + return textsecure.utils.unencodeNumber(number_id)[0]; + }, + + getDeviceId: function(key) { + var number_id = textsecure.storage.getUnencrypted("number_id"); + if (number_id === undefined) + return undefined; + return textsecure.utils.unencodeNumber(number_id)[1]; + } + }; +})();