This commit is contained in:
Brendon Duncan 2018-06-01 09:31:14 +12:00
commit fdc9a8c37b

View file

@ -3,89 +3,87 @@
angular.module('copayApp.services').factory('secureStorageService', function($log, appConfigService, platformInfo) { angular.module('copayApp.services').factory('secureStorageService', function($log, appConfigService, platformInfo) {
var root = {}; var root = {};
var ssIsReady = false; function CordovaSs() {
var ssInitialisationFailed = false; var isReady = false;
var pending = []; var initialisationFailed = false;
var pending = [];
var ss = new cordova.plugins.SecureStorage( var storage = null;
function () {
console.log('ss Success');
ssIsReady = true;
for (var i = 0; i < pending.length; i++) {
pending[i]();
}
pending = [];
},
function (error) {
console.log('ss Error ' + error);
ssInitialisationFailed = true;
},
appConfigService.packageNameId);
this.get = function(key, cb) {
root.get = function(key, cb) { if (!isReady) {
$log.debug('secureStorageService.get()'); if (initialisationFailed) {
if (!ssIsReady) {
$log.debug("ss not ready.");
if (ssInitialisationFailed) {
$log.debug("returning error because initialisation failed.");
cb(new Error("Secure storage initialisation failed.")); cb(new Error("Secure storage initialisation failed."));
} else { } else {
$log.debug("adding get to pending.");
pending.push(function(){ root.get(key, cb); }); pending.push(function(){ root.get(key, cb); });
} }
return return
}
storage.get(
function (value) {
cb(null, value);
},
function (error) {
if (error.message === 'Failure in SecureStorage.get() - The specified item could not be found in the keychain' || // iOS
error.message === 'Key [_SS_profile] not found.') { // Android
// The callback expects no error, but also no value, if it cannot be found.
cb(null);
} else {
cb(new Error(error));
}
},
key);
} }
$log.debug("ss is ready.");
ss.get( this.set = function(key, value, cb) {
function (value) { if (!isReady) {
console.log('ss Success, got ' + value); if (initialisationFailed) {
cb(null, value); cb(new Error("Secure storage initialisation failed."));
}, } else {
function (error) { pending.push(function(){ root.set(key, value, cb); });
console.log('ss Error "' + error.message + '" ' + JSON.stringify(error)); }
return
}
if (error.message === 'Failure in SecureStorage.get() - The specified item could not be found in the keychain' || storage.set(
error.message === 'Key [_SS_profile] not found.') { function (value) {
$log.debug("Sending back null error."); cb();
// The callback expects no error, but also no value, if it cannot be found. },
cb(null); function (error) {
} else {
cb(new Error(error)); cb(new Error(error));
} },
}, key, value);
key); }
if (platformInfo.isCordova) {
storage = new cordova.plugins.SecureStorage(
function () {
console.log('ss Success');
isReady = true;
for (var i = 0; i < pending.length; i++) {
pending[i]();
}
spending = [];
},
function (error) {
console.log('ss Error ' + error);
initialisationFailed = true;
},
appConfigService.packageNameId);
}
}
var cordovaSs = new CordovaSs();
root.get = function(key, cb) {
cordovaSs.get(key, cb);
}; };
root.set = function(key, value, cb) { root.set = function(key, value, cb) {
$log.debug('secureStorageService.set()'); cordovaSs.set(key, value, cb);
if (!ssIsReady) {
$log.debug("ss not ready.");
if (ssInitialisationFailed) {
$log.debug("returning error because initialisation failed.");
cb(new Error("Secure storage initialisation failed."));
} else {
$log.debug("adding set to pending.");
pending.push(function(){ root.set(key, value, cb); });
}
return
}
$log.debug("ss is ready.");
ss.set(
function (value) {
console.log('ss Success, got ' + value);
cb();
},
function (error) {
console.log('ss Error ' + error);
cb(new Error(error));
},
key, value);
}; };