From d9df04819480e8172515b58ff962ced9f0f4f0d6 Mon Sep 17 00:00:00 2001 From: Tim Bray Date: Sun, 11 May 2014 20:06:07 -0700 Subject: JWalk javadocs --- .../sufficientlysecure/keychain/util/JWalk.java | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/JWalk.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/JWalk.java index 7ae1d8fab..76797811d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/JWalk.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/JWalk.java @@ -23,29 +23,77 @@ import org.json.JSONObject; /** * Minimal hierarchy selector + * + * This is for picking out an item in a large multilevel JSON object, for example look at + * the Keybase.io User object, documentation at https://keybase.io/__/api-docs/1.0#user-objects + * an example available via + * curl https://keybase.io/_/api/1.0/user/lookup.json?username=timbray + * + * If you want to retrieve the ascii-armored key, you'd say + * String key = JWalk.getString(match,"them", "public_keys", "primary", "bundle"); */ public class JWalk { + /** + * Returns an int member value from the JSON sub-object addressed by the path + * + * @param json The object + * @param path list of string object member selectors + * @return the int addressed by the path, assuming such a thing exists + * @throws JSONException if any step in the path doesn’t work + */ public static int getInt(JSONObject json, String... path) throws JSONException { json = walk(json, path); return json.getInt(path[path.length - 1]); } + /** + * Returns a long member value from the JSON sub-object addressed by the path + * + * @param json The object + * @param path list of string object member selectors + * @return the int addressed by the path, assuming such a thing exists + * @throws JSONException if any step in the path doesn’t work + */ public static long getLong(JSONObject json, String... path) throws JSONException { json = walk(json, path); return json.getLong(path[path.length - 1]); } + /** + * Returns a String member value from the JSON sub-object addressed by the path + * + * @param json The object + * @param path list of string object member selectors + * @return the int addressed by the path, assuming such a thing exists + * @throws JSONException if any step in the path doesn’t work + */ public static String getString(JSONObject json, String... path) throws JSONException { json = walk(json, path); return json.getString(path[path.length - 1]); } + /** + * Returns a JSONArray member value from the JSON sub-object addressed by the path + * + * @param json The object + * @param path list of string object member selectors + * @return the int addressed by the path, assuming such a thing exists + * @throws JSONException if any step in the path doesn’t work + */ public static JSONArray getArray(JSONObject json, String... path) throws JSONException { json = walk(json, path); return json.getJSONArray(path[path.length - 1]); } + /** + * Returns a JSONObject member value from the JSON sub-object addressed by the path, or null + * + * @param json The object + * @param path list of string object member selectors + * @return the int addressed by the path, assuming such a thing exists + * @throws JSONException if any step in the path, except for the last, doesn’t work + */ public static JSONObject optObject(JSONObject json, String... path) throws JSONException { json = walk(json, path); return json.optJSONObject(path[path.length - 1]); -- cgit v1.2.3 From 9b7179167914d19eb00f6cef5b7664e54efcd5e8 Mon Sep 17 00:00:00 2001 From: Tim Bray Date: Thu, 15 May 2014 16:19:10 -0700 Subject: keybase now has key info in search output --- .../org/sufficientlysecure/keychain/keyimport/KeybaseKeyServer.java | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyServer.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyServer.java index 953cf3cbb..ba05742fb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyServer.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyServer.java @@ -21,6 +21,7 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; import org.sufficientlysecure.keychain.util.JWalk; import org.sufficientlysecure.keychain.util.Log; @@ -100,6 +101,10 @@ public class KeybaseKeyServer extends KeyServer { entry.setExtraData(keybaseId); // TODO: Fix; have suggested keybase provide this value to avoid search-time crypto calls + final int algorithmId = JWalk.getInt(match, "components", "key_fingerprint", "algo"); + entry.setAlgorithm(PgpKeyHelper.getAlgorithmInfo(algorithmId)); + final int bitStrength = JWalk.getInt(match, "components", "key_fingerprint", "nbits"); + entry.setBitStrength(bitStrength); //entry.setBitStrength(4096); //entry.setAlgorithm("RSA"); -- cgit v1.2.3 From 148a52269450e13a2c5db7670706beb86db8a631 Mon Sep 17 00:00:00 2001 From: Tim Bray Date: Thu, 15 May 2014 17:14:45 -0700 Subject: keybase cleanups for revised search output format --- .../keychain/keyimport/KeybaseKeyServer.java | 31 +++++----------------- 1 file changed, 7 insertions(+), 24 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyServer.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyServer.java index ba05742fb..5f729a05c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyServer.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyServer.java @@ -35,8 +35,6 @@ import java.util.WeakHashMap; public class KeybaseKeyServer extends KeyServer { - private WeakHashMap mKeyCache = new WeakHashMap(); - @Override public ArrayList search(String query) throws QueryException, TooManyResponses, InsufficientQuery { @@ -93,29 +91,18 @@ public class KeybaseKeyServer extends KeyServer { String keybaseId = JWalk.getString(match, "components", "username", "val"); String fullName = JWalk.getString(match, "components", "full_name", "val"); String fingerprint = JWalk.getString(match, "components", "key_fingerprint", "val"); - fingerprint = fingerprint.replace(" ", "").toUpperCase(); + fingerprint = fingerprint.replace(" ", "").toUpperCase(); // not strictly necessary but doesn't hurt + entry.setFingerprintHex(fingerprint); // in anticipation of a full fingerprint, only use the last 16 chars as 64-bit key id entry.setKeyIdHex("0x" + fingerprint.substring(Math.max(0, fingerprint.length() - 16))); // store extra info, so we can query for the keybase id directly entry.setExtraData(keybaseId); - // TODO: Fix; have suggested keybase provide this value to avoid search-time crypto calls final int algorithmId = JWalk.getInt(match, "components", "key_fingerprint", "algo"); entry.setAlgorithm(PgpKeyHelper.getAlgorithmInfo(algorithmId)); final int bitStrength = JWalk.getInt(match, "components", "key_fingerprint", "nbits"); entry.setBitStrength(bitStrength); - //entry.setBitStrength(4096); - //entry.setAlgorithm("RSA"); - - entry.setFingerprintHex(fingerprint); - - // key data - // currently there's no need to query the user right away, and it should be avoided, so the - // user doesn't experience lag and doesn't download many keys unnecessarily, but should we - // require to do it at soe point: - // (weakly) remember the key, in case the user tries to import it - //mKeyCache.put(keybaseId, JWalk.getString(match, "them", "public_keys", "primary", "bundle")); ArrayList userIds = new ArrayList(); String name = fullName + " "; @@ -176,16 +163,12 @@ public class KeybaseKeyServer extends KeyServer { @Override public String get(String id) throws QueryException { - String key = mKeyCache.get(id); - if (key == null) { - try { - JSONObject user = getUser(id); - key = JWalk.getString(user, "them", "public_keys", "primary", "bundle"); - } catch (Exception e) { - throw new QueryException(e.getMessage()); - } + try { + JSONObject user = getUser(id); + return JWalk.getString(user, "them", "public_keys", "primary", "bundle"); + } catch (Exception e) { + throw new QueryException(e.getMessage()); } - return key; } @Override -- cgit v1.2.3