-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathindex.ios.ts
225 lines (200 loc) · 6.02 KB
/
index.ios.ts
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { GetOptions, RemoveAllOptions, RemoveOptions, SecureStorageCommon, SetOptions } from './common';
declare const SAMKeychainQuery, SAMKeychain;
export class SecureStorage extends SecureStorageCommon {
private isSimulator: boolean;
private accessibilityType: string;
private static defaultService: string = 'my_app';
// This is a copy of 'kSSKeychainAccountKey_copy' which is not exposed from SSKeychain.h by {N}
private static kSSKeychainAccountKey_copy: string = 'acct';
constructor(accessibilityType: string = kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly, disableFallbackToUserDefaults = false) {
super();
if (disableFallbackToUserDefaults) {
this.isSimulator = false;
} else {
const isMinIOS9 = NSProcessInfo.processInfo.isOperatingSystemAtLeastVersion({
majorVersion: 9,
minorVersion: 0,
patchVersion: 0,
});
if (isMinIOS9) {
const simDeviceName = NSProcessInfo.processInfo.environment.objectForKey('SIMULATOR_DEVICE_NAME');
this.isSimulator = simDeviceName !== null;
} else {
this.isSimulator = UIDevice.currentDevice.name.toLowerCase().indexOf('simulator') > -1;
}
}
this.accessibilityType = accessibilityType;
}
get(arg: GetOptions): Promise<any> {
return new Promise((resolve, reject) => {
if (this.isSimulator) {
resolve(this.getUserDefaultsValue(arg));
return;
}
let query = SAMKeychainQuery.new();
query.service = arg.service || SecureStorage.defaultService;
query.account = arg.key;
if (arg.accessGroup) {
query.accessGroup = arg.accessGroup;
}
try {
query.fetch();
resolve(query.password);
} catch (e) {
resolve(null);
}
});
}
getSync(arg: GetOptions): any {
if (this.isSimulator) {
return this.getUserDefaultsValue(arg);
}
let query = SAMKeychainQuery.new();
query.service = arg.service || SecureStorage.defaultService;
query.account = arg.key;
if (arg.accessGroup) {
query.accessGroup = arg.accessGroup;
}
try {
query.fetch();
return query.password;
} catch (e) {
return null;
}
}
set(arg: SetOptions): Promise<boolean> {
return new Promise((resolve, reject) => {
if (this.isSimulator) {
this.setUserDefaultsValue(arg);
resolve(true);
return;
}
SAMKeychain.setAccessibilityType(this.accessibilityType);
let query = SAMKeychainQuery.new();
query.service = arg.service || SecureStorage.defaultService;
query.account = arg.key;
query.password = arg.value;
if (arg.accessGroup) {
query.accessGroup = arg.accessGroup;
}
resolve(query.save());
});
}
setSync(arg: SetOptions): boolean {
if (this.isSimulator) {
this.setUserDefaultsValue(arg);
return true;
}
SAMKeychain.setAccessibilityType(this.accessibilityType);
let query = SAMKeychainQuery.new();
query.service = arg.service || SecureStorage.defaultService;
query.account = arg.key;
query.password = arg.value;
if (arg.accessGroup) {
query.accessGroup = arg.accessGroup;
}
return query.save();
}
remove(arg: RemoveOptions): Promise<boolean> {
return new Promise((resolve, reject) => {
if (this.isSimulator) {
this.removeUserDefaultsValue(arg);
resolve(true);
return;
}
let query = SAMKeychainQuery.new();
query.service = arg.service || SecureStorage.defaultService;
query.account = arg.key;
if (arg.accessGroup) {
query.accessGroup = arg.accessGroup;
}
try {
resolve(query.deleteItem());
} catch (e) {
resolve(false);
}
});
}
removeSync(arg: RemoveOptions): boolean {
if (this.isSimulator) {
this.removeUserDefaultsValue(arg);
return true;
}
let query = SAMKeychainQuery.new();
query.service = arg.service || SecureStorage.defaultService;
query.account = arg.key;
if (arg.accessGroup) {
query.accessGroup = arg.accessGroup;
}
try {
return query.deleteItem();
} catch (e) {
return false;
}
}
removeAll(arg?: RemoveAllOptions): Promise<boolean> {
return new Promise((resolve, reject) => {
if (this.isSimulator) {
let defaults = NSUserDefaults.standardUserDefaults;
let bundleId = NSBundle.mainBundle.bundleIdentifier;
defaults.removePersistentDomainForName(bundleId);
resolve(true);
return;
}
const allAccounts = SAMKeychain.allAccounts();
if (allAccounts) {
for (let i = 0; i < allAccounts.count; i++) {
let key = allAccounts[i].objectForKey(SecureStorage.kSSKeychainAccountKey_copy);
try {
let query = SAMKeychainQuery.new();
query.service = arg && arg.service ? arg.service : SecureStorage.defaultService;
query.account = key;
query.deleteItem();
} catch (e) {
console.log('SecureStorage: Could not remove key -> ' + key);
}
}
}
resolve(true);
});
}
removeAllSync(arg?: RemoveAllOptions): boolean {
if (this.isSimulator) {
let defaults = NSUserDefaults.standardUserDefaults;
let bundleId = NSBundle.mainBundle.bundleIdentifier;
defaults.removePersistentDomainForName(bundleId);
return true;
}
const allAccounts = SAMKeychain.allAccounts();
if (allAccounts) {
for (let i = 0; i < allAccounts.count; i++) {
let key = allAccounts[i].objectForKey(SecureStorage.kSSKeychainAccountKey_copy);
try {
let query = SAMKeychainQuery.new();
query.service = arg && arg.service ? arg.service : SecureStorage.defaultService;
query.account = key;
query.deleteItem();
} catch (e) {
console.log('SecureStorage: Could not remove key -> ' + key);
}
}
}
return true;
}
private setUserDefaultsValue(arg: SetOptions) {
this.getUserDefaultToUse(arg.accessGroup).setValueForKey(arg.value, arg.key);
}
private getUserDefaultsValue(arg: GetOptions) {
return this.getUserDefaultToUse(arg.accessGroup).objectForKey(arg.key);
}
private removeUserDefaultsValue(arg: RemoveOptions) {
return this.getUserDefaultToUse(arg.accessGroup).removeObjectForKey(arg.key);
}
private getUserDefaultToUse(accessGroup?: string): NSUserDefaults {
if (accessGroup) {
return new NSUserDefaults({ suiteName: accessGroup });
} else {
return NSUserDefaults.standardUserDefaults;
}
}
}