Merge pull request #630 from timbray/master

Handle changed keybase.io search results format
This commit is contained in:
Dominik Schürmann 2014-05-16 09:52:34 +02:00
commit 9025de2b74
2 changed files with 60 additions and 24 deletions

View File

@ -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;
@ -34,8 +35,6 @@ import java.util.WeakHashMap;
public class KeybaseKeyServer extends KeyServer {
private WeakHashMap<String, String> mKeyCache = new WeakHashMap<String, String>();
@Override
public ArrayList<ImportKeysListEntry> search(String query) throws QueryException, TooManyResponses,
InsufficientQuery {
@ -92,25 +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
//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"));
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);
ArrayList<String> userIds = new ArrayList<String>();
String name = fullName + " <keybase.io/" + keybaseId + ">";
@ -171,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

View File

@ -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 doesnt 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 doesnt 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 doesnt 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 doesnt 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, doesnt work
*/
public static JSONObject optObject(JSONObject json, String... path) throws JSONException {
json = walk(json, path);
return json.optJSONObject(path[path.length - 1]);