(Context context, String user, String password, JSONObject jrequest)
| 629 | } |
| 630 | |
| 631 | private static JSONObject _call(Context context, String user, String password, JSONObject jrequest) |
| 632 | throws GeneralSecurityException, JSONException, IOException, InvalidCipherTextException { |
| 633 | byte[] salt = getSalt(user); |
| 634 | String cloudUser = getCloudUser(salt); |
| 635 | |
| 636 | Pair<byte[], byte[]> key; |
| 637 | String lookup = Helper.hex(salt) + ":" + password; |
| 638 | synchronized (keyCache) { |
| 639 | key = keyCache.get(lookup); |
| 640 | } |
| 641 | if (key == null) { |
| 642 | EntityLog.log(context, EntityLog.Type.Cloud, "Cloud generating key"); |
| 643 | key = getKeyPair(salt, password); |
| 644 | synchronized (keyCache) { |
| 645 | keyCache.put(lookup, key); |
| 646 | } |
| 647 | } else { |
| 648 | EntityLog.log(context, EntityLog.Type.Cloud, "Cloud using cached key"); |
| 649 | } |
| 650 | |
| 651 | String cloudPassword = Base64.encodeToString(key.first, Base64.NO_PADDING | Base64.NO_WRAP); |
| 652 | |
| 653 | jrequest.put("version", 1); |
| 654 | jrequest.put("username", cloudUser); |
| 655 | jrequest.put("password", cloudPassword); |
| 656 | jrequest.put("debug", BuildConfig.DEBUG); |
| 657 | |
| 658 | if (jrequest.has("items")) { |
| 659 | JSONArray jitems = jrequest.getJSONArray("items"); |
| 660 | for (int i = 0; i < jitems.length(); i++) { |
| 661 | JSONObject jitem = jitems.getJSONObject(i); |
| 662 | long revision = jitem.optLong("rev", 0); |
| 663 | |
| 664 | String k = jitem.getString("key"); |
| 665 | String v = null; |
| 666 | if (jitem.has("val") && !jitem.isNull("val")) { |
| 667 | v = jitem.getString("val"); |
| 668 | jitem.put("val", transform(v, key.second, getIv(revision), getAd(k, revision), true)); |
| 669 | } |
| 670 | v = (v == null ? null : "#" + v.length()); |
| 671 | |
| 672 | Log.i("Cloud > " + k + "=" + v + " @" + revision); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | String request = jrequest.toString(); |
| 677 | EntityLog.log(context, EntityLog.Type.Cloud, |
| 678 | "Cloud request length=" + request.length()); |
| 679 | |
| 680 | URL url = new URL(BuildConfig.CLOUD_URI); |
| 681 | HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); |
| 682 | connection.setRequestMethod("POST"); |
| 683 | connection.setDoInput(true); |
| 684 | connection.setDoOutput(true); |
| 685 | connection.setReadTimeout(CLOUD_TIMEOUT); |
| 686 | connection.setConnectTimeout(CLOUD_TIMEOUT); |
| 687 | ConnectionHelper.setUserAgent(context, connection); |
| 688 | connection.setRequestProperty("Accept", "application/json"); |
no test coverage detected