From 8bdebd543dd9026c7ab8b51cc6da346643864c4c Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Tue, 7 Jun 2016 21:32:27 +0300 Subject: [PATCH] Clear local storage on logout --- app/js/lib/config.js | 52 ++++++++++++++++++++++++++--------- app/js/lib/mtproto_wrapper.js | 33 ++++++++++++++++++++-- app/js/lib/ng_utils.js | 7 ++++- 3 files changed, 75 insertions(+), 17 deletions(-) diff --git a/app/js/lib/config.js b/app/js/lib/config.js index f5b26f3c..f510c9a7 100644 --- a/app/js/lib/config.js +++ b/app/js/lib/config.js @@ -140,11 +140,14 @@ Config.LangCountries = {"es": "ES", "ru": "RU", "en": "US", "de": "DE", "it": "I return keyPrefix; } - function storageGetValue() { - var keys = Array.prototype.slice.call(arguments), - callback = keys.pop(), - result = [], - single = keys.length == 1, + function storageGetValue(keys, callback) { + var single = false; + if (!Array.isArray(keys)) { + keys = Array.prototype.slice.call(arguments); + callback = keys.pop(); + single = keys.length == 1; + } + var result = [], value, allFound = true, prefix = storageGetPrefix(), @@ -227,14 +230,16 @@ Config.LangCountries = {"es": "ES", "ru": "RU", "en": "US", "de": "DE", "it": "I chrome.storage.local.set(keyValues, callback); }; - function storageRemoveValue () { - var keys = Array.prototype.slice.call(arguments), - prefix = storageGetPrefix(), - i, key, callback; - - if (typeof keys[keys.length - 1] === 'function') { - callback = keys.pop(); + function storageRemoveValue (keys, callback) { + if (!Array.isArray(keys)) { + keys = Array.prototype.slice.call(arguments); + if (typeof keys[keys.length - 1] === 'function') { + callback = keys.pop(); + } } + var prefix = storageGetPrefix(), + i, key; + for (i = 0; i < keys.length; i++) { key = keys[i] = prefix + keys[i]; @@ -255,12 +260,33 @@ Config.LangCountries = {"es": "ES", "ru": "RU", "en": "US", "de": "DE", "it": "I } }; + function storageClear(callback) { + if (useLs) { + try { + localStorage.clear(); + } catch (e) { + useLs = false; + } + } + + if (useCs) { + chrome.storage.local.clear(function () { + cache = {}; + callback(); + }); + } else { + cache = {}; + callback(); + } + }; + window.ConfigStorage = { prefix: storageSetPrefix, noPrefix: storageSetNoPrefix, get: storageGetValue, set: storageSetValue, - remove: storageRemoveValue + remove: storageRemoveValue, + clear: storageClear }; })(this); diff --git a/app/js/lib/mtproto_wrapper.js b/app/js/lib/mtproto_wrapper.js index 4bc57429..444152d8 100644 --- a/app/js/lib/mtproto_wrapper.js +++ b/app/js/lib/mtproto_wrapper.js @@ -47,7 +47,7 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto']) for (var dcID = 1; dcID <= 5; dcID++) { storageKeys.push('dc' + dcID + '_auth_key'); } - return Storage.get.apply(Storage, storageKeys).then(function (storageResult) { + return Storage.get(storageKeys).then(function (storageResult) { var logoutPromises = []; for (var i = 0; i < storageResult.length; i++) { if (storageResult[i]) { @@ -58,12 +58,36 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto']) Storage.remove('dc', 'user_auth'); baseDcID = false; telegramMeNotify(false); + return mtpClearStorage(); }, function (error) { - Storage.remove.apply(storageKeys); - Storage.remove('dc', 'user_auth'); + storageKeys.push('dc', 'user_auth'); + Storage.remove(storageKeys); baseDcID = false; error.handled = true; telegramMeNotify(false); + return mtpClearStorage(); + }); + }); + } + + function mtpClearStorage() { + var saveKeys = []; + for (var dcID = 1; dcID <= 5; dcID++) { + saveKeys.push('dc' + dcID + '_auth_key'); + saveKeys.push('t_dc' + dcID + '_auth_key'); + } + Storage.noPrefix(); + Storage.get(saveKeys).then(function (values) { + Storage.clear().then(function () { + var restoreObj = {}; + angular.forEach(saveKeys, function (key, i) { + var value = values[i]; + if (value !== false && value !== undefined) { + restoreObj[key] = value; + } + }); + Storage.noPrefix(); + return Storage.set(restoreObj); }); }); } @@ -95,6 +119,9 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto']) serverSaltHex = result[1]; // console.log('ass', dcID, authKeyHex, serverSaltHex); if (authKeyHex && authKeyHex.length == 512) { + if (!serverSaltHex || serverSaltHex.length != 16) { + serverSaltHex = 'AAAAAAAAAAAAAAAA'; + } var authKey = bytesFromHex(authKeyHex); var serverSalt = bytesFromHex(serverSaltHex); diff --git a/app/js/lib/ng_utils.js b/app/js/lib/ng_utils.js index 963dd2ed..94e97d14 100644 --- a/app/js/lib/ng_utils.js +++ b/app/js/lib/ng_utils.js @@ -15,7 +15,7 @@ angular.module('izhukov.utils', []) this.$get = ['$q', function ($q) { var methods = {}; - angular.forEach(['get', 'set', 'remove'], function (methodName) { + angular.forEach(['get', 'set', 'remove', 'clear'], function (methodName) { methods[methodName] = function () { var deferred = $q.defer(), args = Array.prototype.slice.call(arguments); @@ -28,6 +28,11 @@ angular.module('izhukov.utils', []) return deferred.promise; }; }); + + methods.noPrefix = function () { + ConfigStorage.noPrefix(); + }; + return methods; }];