This repository has been archived by the owner on Jul 31, 2019. It is now read-only.
/
securestorage.js
133 lines (115 loc) · 3.73 KB
/
securestorage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var SecureStorage;
var SUPPORTED_PLATFORMS = ['android', 'ios', 'windows'];
var _checkCallbacks = function (success, error) {
if (typeof success != 'function') {
throw new Error('SecureStorage failure: success callback parameter must be a function');
}
if (typeof error != 'function') {
throw new Error('SecureStorage failure: error callback parameter must be a function');
}
};
//Taken from undescore.js
var _isString = function isString(x) {
return Object.prototype.toString.call(x) === '[object String]';
};
/**
* Helper method to execute Cordova native method
*
* @param {String} nativeMethodName Method to execute.
* @param {Array} args Execution arguments.
* @param {Function} success Called when returning successful result from an action.
* @param {Function} error Called when returning error result from an action.
*
*/
var _executeNativeMethod = function (success, error, nativeMethodName, args) {
var fail;
// args checking
_checkCallbacks(success, error);
// By convention a failure callback should always receive an instance
// of a JavaScript Error object.
fail = function(err) {
// provide default message if no details passed to callback
if (typeof err === 'undefined') {
error(new Error('Error occured while executing native method.'));
} else {
// wrap string to Error instance if necessary
error(_isString(err) ? new Error(err) : err);
}
};
cordova.exec(success, fail, 'SecureStorage', nativeMethodName, args);
};
SecureStorage = function (success, error, service, options) {
var platformId = cordova.platformId;
var opts = options && options[platformId] ? options[platformId] : {};
this.service = service;
try {
_executeNativeMethod(success, error, 'init', [this.service, opts]);
} catch (e) {
error(e);
}
return this;
};
SecureStorage.prototype = {
get: function (success, error, key) {
try {
if (!_isString(key)) {
throw new Error('Key must be a string');
}
_executeNativeMethod(success, error, 'get', [this.service, key]);
} catch (e) {
error(e);
}
},
set: function (success, error, key, value) {
try {
if (!_isString(value)) {
throw new Error('Value must be a string');
}
_executeNativeMethod(success, error, 'set', [this.service, key, value]);
} catch (e) {
error(e);
}
},
remove: function (success, error, key) {
try {
if (!_isString(key)) {
throw new Error('Key must be a string');
}
_executeNativeMethod(success, error, 'remove', [this.service, key]);
} catch (e) {
error(e);
}
},
keys: function (success, error) {
try {
_executeNativeMethod(success, error, 'keys', [this.service]);
} catch (e) {
error(e);
}
},
clear: function (success, error) {
try {
_executeNativeMethod(success, error, 'clear', [this.service]);
} catch (e) {
error(e);
}
}
};
if (cordova.platformId === 'android') {
SecureStorage.prototype.secureDevice = function (success, error) {
try {
_executeNativeMethod(success, error, 'secureDevice', []);
} catch (e) {
error(e);
}
}
}
if (!cordova.plugins) {
cordova.plugins = {};
}
if (!cordova.plugins.SecureStorage) {
cordova.plugins.SecureStorage = SecureStorage;
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = SecureStorage;
}