Refactored to have the mobile and desktop secure storage contained with a more generic secure storage service.
This commit is contained in:
parent
fdc9a8c37b
commit
aaad6a1b4a
4 changed files with 118 additions and 84 deletions
|
|
@ -72,7 +72,8 @@
|
||||||
<plugin name="cordova-plugin-queries-schemes" spec="~0.1.5" />
|
<plugin name="cordova-plugin-queries-schemes" spec="~0.1.5" />
|
||||||
<plugin name="cordova-plugin-firebase" spec="https://github.com/arnesson/cordova-plugin-firebase.git" />
|
<plugin name="cordova-plugin-firebase" spec="https://github.com/arnesson/cordova-plugin-firebase.git" />
|
||||||
<plugin name="cordova-plugin-wkwebview-inputfocusfix" spec="https://github.com/onderceylan/cordova-plugin-wkwebview-inputfocusfix.git" />
|
<plugin name="cordova-plugin-wkwebview-inputfocusfix" spec="https://github.com/onderceylan/cordova-plugin-wkwebview-inputfocusfix.git" />
|
||||||
<plugin name="cordova-plugin-secure-storage" spec="~2.6.8" />
|
<!-- Changes in error descriptions may break the use of cordova-plugin-secure-storage -->
|
||||||
|
<plugin name="cordova-plugin-secure-storage" spec="2.6.8" />
|
||||||
<!-- Supported Platforms -->
|
<!-- Supported Platforms -->
|
||||||
<engine name="ios" spec="~4.5.3" />
|
<engine name="ios" spec="~4.5.3" />
|
||||||
<engine name="android" spec="~6.3.0" />
|
<engine name="android" spec="~6.3.0" />
|
||||||
|
|
|
||||||
93
src/js/services/mobileSecureStorageService.js
Normal file
93
src/js/services/mobileSecureStorageService.js
Normal file
|
|
@ -0,0 +1,93 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
angular.module('copayApp.services').factory('mobileSecureStorageService', function($log, appConfigService, platformInfo) {
|
||||||
|
var root = {};
|
||||||
|
|
||||||
|
var isReady = false;
|
||||||
|
var initialisationFailed = false;
|
||||||
|
var pending = [];
|
||||||
|
|
||||||
|
var storage = null;
|
||||||
|
|
||||||
|
this.get = function(key, cb) {
|
||||||
|
if (!isReady) {
|
||||||
|
if (initialisationFailed) {
|
||||||
|
cb(new Error("mobileSecureStorageService initialisation failed."));
|
||||||
|
} else {
|
||||||
|
pending.push(function(){ root.get(key, cb); });
|
||||||
|
}
|
||||||
|
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, null);
|
||||||
|
} else {
|
||||||
|
cb(new Error(error));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
key);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.set = function(key, value, cb) {
|
||||||
|
if (!isReady) {
|
||||||
|
if (initialisationFailed) {
|
||||||
|
cb(new Error("mobileSecureStorageService initialisation failed."));
|
||||||
|
} else {
|
||||||
|
pending.push(function(){ root.set(key, value, cb); });
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
storage.set(
|
||||||
|
function (value) {
|
||||||
|
cb();
|
||||||
|
},
|
||||||
|
function (error) {
|
||||||
|
cb(new Error(error));
|
||||||
|
},
|
||||||
|
key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (platformInfo.isCordova) {
|
||||||
|
storage = new cordova.plugins.SecureStorage(
|
||||||
|
function () {
|
||||||
|
$log.debug('mobileSecureStorageService initialised.');
|
||||||
|
isReady = true;
|
||||||
|
for (var i = 0; i < pending.length; i++) {
|
||||||
|
pending[i]();
|
||||||
|
}
|
||||||
|
pending = [];
|
||||||
|
},
|
||||||
|
function (error) {
|
||||||
|
c$log.debug('mobileSecureStorageService initialisation failed. ' + error);
|
||||||
|
initialisationFailed = true;
|
||||||
|
},
|
||||||
|
appConfigService.packageNameId);
|
||||||
|
}
|
||||||
|
|
||||||
|
root.get = function(key, cb) {
|
||||||
|
if (platformInfo.isMobile) {
|
||||||
|
storage.get(key, cb);
|
||||||
|
} else {
|
||||||
|
cb(new Error('mobileSecureStorageService is only available on mobile.'));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
root.set = function(key, value, cb) {
|
||||||
|
if (platformInfo.isMobile) {
|
||||||
|
storage.set(key, v, cb);
|
||||||
|
} else {
|
||||||
|
cb(new Error('mobileSecureStorageService is only available on mobile.'));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return root;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
@ -1,92 +1,32 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
angular.module('copayApp.services').factory('secureStorageService', function($log, appConfigService, platformInfo) {
|
angular.module('copayApp.services').factory('secureStorageService', function(desktopSecureStorageService, localStorageService, $log, mobileSecureStorageService, platformInfo) {
|
||||||
var root = {};
|
var root = {};
|
||||||
|
|
||||||
function CordovaSs() {
|
// To make wrong code look wrong
|
||||||
var isReady = false;
|
function alteredKeyIndicatingDesireForSecureStorage(key) {
|
||||||
var initialisationFailed = false;
|
return key + ":desiredSecure";
|
||||||
var pending = [];
|
|
||||||
|
|
||||||
var storage = null;
|
|
||||||
|
|
||||||
|
|
||||||
this.get = function(key, cb) {
|
|
||||||
if (!isReady) {
|
|
||||||
if (initialisationFailed) {
|
|
||||||
cb(new Error("Secure storage initialisation failed."));
|
|
||||||
} else {
|
|
||||||
pending.push(function(){ root.get(key, cb); });
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.set = function(key, value, cb) {
|
|
||||||
if (!isReady) {
|
|
||||||
if (initialisationFailed) {
|
|
||||||
cb(new Error("Secure storage initialisation failed."));
|
|
||||||
} else {
|
|
||||||
pending.push(function(){ root.set(key, value, cb); });
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
storage.set(
|
|
||||||
function (value) {
|
|
||||||
cb();
|
|
||||||
},
|
|
||||||
function (error) {
|
|
||||||
cb(new Error(error));
|
|
||||||
},
|
|
||||||
key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
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(k, cb) {
|
||||||
|
if (platformInfo.isMobile) {
|
||||||
root.get = function(key, cb) {
|
mobileSecureStorageService.get(k, cb);
|
||||||
cordovaSs.get(key, cb);
|
} else if (platformInfo.isNW) {
|
||||||
};
|
desktopSecureStorageService.get(k, cb);
|
||||||
|
} else { // Browser
|
||||||
root.set = function(key, value, cb) {
|
localStorageService.get(alteredKeyIndicatingDesireForSecureStorage(k), cb);
|
||||||
cordovaSs.set(key, value, cb);
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
|
root.set = function(k, v, cb) {
|
||||||
|
if (platformInfo.isMobile) {
|
||||||
|
mobileSecureStorageService.set(k, v, cb);
|
||||||
|
} else if (platformInfo.isNW) {
|
||||||
|
desktopSecureStorageService.set(k, v, cb);
|
||||||
|
} else { // Browser
|
||||||
|
localStorageService.set(alteredKeyIndicatingDesireForSecureStorage(k), v, cb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -126,7 +126,7 @@ angular.module('copayApp.services')
|
||||||
};
|
};
|
||||||
|
|
||||||
root.getProfile = function(cb) {
|
root.getProfile = function(cb) {
|
||||||
$log.debug("getProfile() 31 7");
|
$log.debug("getProfile() 1 8");
|
||||||
|
|
||||||
//storage.get('profile', function(err, str) {
|
//storage.get('profile', function(err, str) {
|
||||||
secureStorageService.get('profile', function(err, str) {
|
secureStorageService.get('profile', function(err, str) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue