-
Notifications
You must be signed in to change notification settings - Fork 25
/
AccountManagerPlugin.java
437 lines (393 loc) · 11.1 KB
/
AccountManagerPlugin.java
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// Copyright (C) 2013 Polychrom Pty Ltd
//
// This program is licensed under the 3-clause "Modified" BSD license,
// see LICENSE file for full definition.
package com.polychrom.cordova;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map.Entry;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerFuture;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.os.Bundle;
/**! Android AccountManager plugin for Cordova
*
* @author Mitchell Wheeler
*
* Implements a basic Android AccountManager plugin for Cordova with support for common account management routines.
*
* Features not currently supported are:
* * Account features
* * Automatic Authentication via AccountManagers (only explicit accounts and auth tokens are supported currently)
*/
public class AccountManagerPlugin extends CordovaPlugin
{
AccountManager manager = null;
// Naive int to account mapping so our JS can simply reference native objects
Integer accumulator = 0;
HashMap<Integer, Account> accounts = new HashMap<Integer, Account>();
private Integer indexForAccount(Account account)
{
for(Entry<Integer, Account> e: accounts.entrySet())
{
if(e.getValue() == account)
{
return e.getKey();
}
}
accounts.put(accumulator, account);
return accumulator++;
}
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException
{
if(manager == null)
{
manager = AccountManager.get(cordova.getActivity());
}
try
{
if("getAccountsByType".equals(action))
{
Account[] account_list = manager.getAccountsByType(args.isNull(0)? null : args.getString(0));
JSONArray result = new JSONArray();
for(Account account: account_list)
{
Integer index = indexForAccount(account);
accounts.put(index, account);
JSONObject account_object = new JSONObject();
account_object.put("_index", (int)index);
account_object.put("name", account.name);
account_object.put("type", account.type);
result.put(account_object);
}
callbackContext.success(result);
return true;
}
else if("addAccountExplicitly".equals(action))
{
if(args.isNull(0) || args.getString(0).length() == 0)
{
callbackContext.error("accountType can not be null or empty");
return true;
}
else if(args.isNull(1) || args.getString(1).length() == 0)
{
callbackContext.error("username can not be null or empty");
return true;
}
else if(args.isNull(2) || args.getString(2).length() == 0)
{
callbackContext.error("password can not be null or empty");
return true;
}
Account account = new Account(args.getString(1), args.getString(0));
Integer index = indexForAccount(account);
Bundle userdata = new Bundle();
if(!args.isNull(3))
{
JSONArray userdata_array = args.getJSONArray(3);
if(userdata_array != null)
{
for(int i = 0; i < userdata_array.length(); ++i)
{
JSONObject keyval = userdata_array.getJSONObject(i);
if(keyval == null || keyval.isNull("key") || keyval.isNull("value"))
{
continue;
}
userdata.putString(keyval.getString("key"), keyval.getString("value"));
}
}
}
if(false == manager.addAccountExplicitly(account, args.getString(2), userdata))
{
callbackContext.error("Account with username already exists!");
return true;
}
accounts.put(index, account);
JSONObject result = new JSONObject();
result.put("_index", (int)index);
result.put("name", account.name);
result.put("type", account.type);
callbackContext.success(result);
return true;
}
else if("updateCredentials".equals(action))
{
if(args.isNull(0))
{
callbackContext.error("account can not be null");
return true;
}
Account account = accounts.get(args.getInt(0));
if(account == null)
{
callbackContext.error("Invalid account");
return true;
}
callbackContext.error("Not yet implemented");
return true;
}
else if("clearPassword".equals(action))
{
if(args.isNull(0))
{
callbackContext.error("account can not be null");
return true;
}
Account account = accounts.get(args.getInt(0));
if(account == null)
{
callbackContext.error("Invalid account");
return true;
}
manager.clearPassword(account);
callbackContext.success();
return true;
}
else if("removeAccount".equals(action))
{
if(args.isNull(0))
{
callbackContext.error("account can not be null");
return true;
}
int index = args.getInt(0);
Account account = accounts.get(index);
if(account == null)
{
callbackContext.error("Invalid account");
return true;
}
// TODO: Add support for AccountManager (callback)
AccountManagerFuture<Boolean> future = manager.removeAccount(account, null, null);
try
{
if(future.getResult() == true)
{
accounts.remove(index);
callbackContext.success();
}
else
{
callbackContext.error("Failed to remove account");
}
}
catch (OperationCanceledException e)
{
callbackContext.error("Operation canceled: " + e.getLocalizedMessage());
}
catch (AuthenticatorException e)
{
callbackContext.error("Authenticator error: " + e.getLocalizedMessage());
}
catch (IOException e)
{
callbackContext.error("IO error: " + e.getLocalizedMessage());
}
return true;
}
else if("setAuthToken".equals(action))
{
if(args.isNull(0))
{
callbackContext.error("account can not be null");
return true;
}
else if(args.isNull(1) || args.getString(1).length() == 0)
{
callbackContext.error("authTokenType can not be null or empty");
return true;
}
else if(args.isNull(2) || args.getString(2).length() == 0)
{
callbackContext.error("authToken can not be null or empty");
return true;
}
Account account = accounts.get(args.getInt(0));
if(account == null)
{
callbackContext.error("Invalid account");
return true;
}
manager.setAuthToken(account, args.getString(1), args.getString(2));
callbackContext.success();
return true;
}
else if("peekAuthToken".equals(action))
{
if(args.isNull(0))
{
callbackContext.error("account can not be null");
return true;
}
else if(args.isNull(1) || args.getString(1).length() == 0)
{
callbackContext.error("authTokenType can not be null or empty");
return true;
}
Account account = accounts.get(args.getInt(0));
if(account == null)
{
callbackContext.error("Invalid account");
return true;
}
JSONObject result = new JSONObject();
result.put("value", manager.peekAuthToken(account, args.getString(1)));
callbackContext.success(result);
return true;
}
else if("getAuthToken".equals(action))
{
if(args.isNull(0))
{
callbackContext.error("account can not be null");
return true;
}
else if(args.isNull(1) || args.getString(1).length() == 0)
{
callbackContext.error("authTokenType can not be null or empty");
return true;
}
else if(args.isNull(3))
{
callbackContext.error("notifyAuthFailure can not be null");
return true;
}
Account account = accounts.get(args.getInt(0));
if(account == null)
{
callbackContext.error("Invalid account");
return true;
}
Bundle options = new Bundle();
// TODO: Options support (will be relevent when we support AccountManagers)
// TODO: AccountManager support
AccountManagerFuture<Bundle> future = manager.getAuthToken(account, args.getString(1), options, args.getBoolean(3), null, null);
try
{
JSONObject result = new JSONObject();
result.put("value", future.getResult().getString(AccountManager.KEY_AUTHTOKEN));
callbackContext.success(result);
}
catch (OperationCanceledException e)
{
callbackContext.error("Operation canceled: " + e.getLocalizedMessage());
}
catch (AuthenticatorException e)
{
callbackContext.error("Authenticator error: " + e.getLocalizedMessage());
}
catch (IOException e)
{
callbackContext.error("IO error: " + e.getLocalizedMessage());
}
return true;
}
else if("setPassword".equals(action))
{
if(args.isNull(0))
{
callbackContext.error("account can not be null");
return true;
}
else if(args.isNull(1) || args.getString(1).length() == 0)
{
callbackContext.error("password can not be null or empty");
return true;
}
Account account = accounts.get(args.getInt(0));
if(account == null)
{
callbackContext.error("Invalid account");
return true;
}
manager.setPassword(account, args.getString(1));
callbackContext.success();
return true;
}
else if("getPassword".equals(action))
{
if(args.isNull(0))
{
callbackContext.error("account can not be null");
return true;
}
Account account = accounts.get(args.getInt(0));
if(account == null)
{
callbackContext.error("Invalid account");
return true;
}
JSONObject result = new JSONObject();
result.put("value", manager.getPassword(account));
callbackContext.success(result);
return true;
}
else if("setUserData".equals(action))
{
if(args.isNull(0))
{
callbackContext.error("account can not be null");
return true;
}
else if(args.isNull(1) || args.getString(1).length() == 0)
{
callbackContext.error("key can not be null or empty");
return true;
}
else if(args.isNull(2) || args.getString(2).length() == 0)
{
callbackContext.error("value can not be null or empty");
return true;
}
Account account = accounts.get(args.getInt(0));
if(account == null)
{
callbackContext.error("Invalid account");
return true;
}
manager.setUserData(account, args.getString(1), args.getString(2));
callbackContext.success();
return true;
}
else if("getUserData".equals(action))
{
if(args.isNull(0))
{
callbackContext.error("account can not be null");
return true;
}
else if(args.isNull(1) || args.getString(1).length() == 0)
{
callbackContext.error("key can not be null or empty");
return true;
}
Account account = accounts.get(args.getInt(0));
if(account == null)
{
callbackContext.error("Invalid account");
return true;
}
JSONObject result = new JSONObject();
result.put("value", manager.getUserData(account, args.getString(1)));
callbackContext.success(result);
return true;
}
}
catch(SecurityException e)
{
callbackContext.error("Access denied");
return true;
}
return false;
}
}