diff options
| author | Dominik Schürmann <dominik@dominikschuermann.de> | 2014-06-21 21:25:03 +0200 | 
|---|---|---|
| committer | Dominik Schürmann <dominik@dominikschuermann.de> | 2014-06-21 21:25:03 +0200 | 
| commit | 4ae5c56b732ae3d1ebf57d8773563504dcad8f40 (patch) | |
| tree | 2eb4754ef15704eb9b7794e87767270a3ffbb6a8 /OpenKeychain/src/main | |
| parent | 18430bef17234be2446c66405526a158c42c5141 (diff) | |
| parent | 1e72f2cba5407cda3e563fc2122312cceb7a9bf9 (diff) | |
| download | open-keychain-4ae5c56b732ae3d1ebf57d8773563504dcad8f40.tar.gz open-keychain-4ae5c56b732ae3d1ebf57d8773563504dcad8f40.tar.bz2 open-keychain-4ae5c56b732ae3d1ebf57d8773563504dcad8f40.zip  | |
Merge pull request #668 from timbray/master
Moved Keybase stuff into KeybaseLib submodule
Diffstat (limited to 'OpenKeychain/src/main')
3 files changed, 30 insertions, 223 deletions
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 bf886cc6c..98e5111f7 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyserver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyserver.java @@ -17,19 +17,17 @@  package org.sufficientlysecure.keychain.keyimport; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; +import com.textuality.keybase.lib.KeybaseException; +import com.textuality.keybase.lib.Match; +import com.textuality.keybase.lib.Search; +import com.textuality.keybase.lib.User; +  import org.sufficientlysecure.keychain.Constants;  import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; -import org.sufficientlysecure.keychain.util.JWalk;  import org.sufficientlysecure.keychain.util.Log; -import java.net.HttpURLConnection; -import java.net.URL; -import java.net.URLEncoder;  import java.util.ArrayList; -import java.util.Locale; +import java.util.List;  public class KeybaseKeyserver extends Keyserver {      public static final String ORIGIN = "keybase:keybase.io"; @@ -44,29 +42,14 @@ public class KeybaseKeyserver extends Keyserver {              // cut off "0x" if a user is searching for a key id              query = query.substring(2);          } +        mQuery = query; -        JSONObject fromQuery = getFromKeybase("_/api/1.0/user/autocomplete.json?q=", query);          try { - -            JSONArray matches = JWalk.getArray(fromQuery, "completions"); -            for (int i = 0; i < matches.length(); i++) { -                JSONObject match = matches.getJSONObject(i); - -                // only list them if they have a key -                if (JWalk.optObject(match, "components", "key_fingerprint") != null) { -                    // TODO: needed anymore? -//                    String keybaseId = JWalk.getString(match, "components", "username", "val"); -//                    String fingerprint = JWalk.getString(match, "components", "key_fingerprint", "val"); -//                    fingerprint = fingerprint.replace(" ", "").toUpperCase(); -//                    if (keybaseId.equals(query) || fingerprint.startsWith(query.toUpperCase())) { -//                        results.add(makeEntry(match)); -//                    } else { -//                        results.add(makeEntry(match)); -//                    } -                    results.add(makeEntry(match)); -                } +            Iterable<Match> matches = Search.search(query); +            for (Match match : matches) { +                results.add(makeEntry(match));              } -        } catch (Exception e) { +        } catch (KeybaseException e) {              Log.e(Constants.TAG, "keybase result parsing error", e);              throw new QueryFailedException("Unexpected structure in keybase search result: " + e.getMessage());          } @@ -74,103 +57,48 @@ public class KeybaseKeyserver extends Keyserver {          return results;      } -    private JSONObject getUser(String keybaseId) throws QueryFailedException { -        try { -            return getFromKeybase("_/api/1.0/user/lookup.json?username=", keybaseId); -        } catch (Exception e) { -            String detail = ""; -            if (keybaseId != null) { -                detail = ". Query was for user '" + keybaseId + "'"; -            } -            throw new QueryFailedException(e.getMessage() + detail); -        } -    } - -    private ImportKeysListEntry makeEntry(JSONObject match) throws QueryFailedException, JSONException { - +    private ImportKeysListEntry makeEntry(Match match) throws KeybaseException {          final ImportKeysListEntry entry = new ImportKeysListEntry();          entry.setQuery(mQuery);          entry.setOrigin(ORIGIN); -        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(" ", "").toLowerCase(Locale.US); // not strictly necessary but doesn't hurt +        String username = null; +        username = match.getUsername(); +        String fullName = match.getFullName(); +        String fingerprint = match.getFingerprint();          entry.setFingerprintHex(fingerprint); -        entry.setKeyIdHex("0x" + fingerprint.substring(Math.max(0, fingerprint.length() - 16))); +        entry.setKeyIdHex("0x" + match.getKeyID());          // store extra info, so we can query for the keybase id directly -        entry.setExtraData(keybaseId); +        entry.setExtraData(username); -        final int algorithmId = JWalk.getInt(match, "components", "key_fingerprint", "algo"); +        final int algorithmId = match.getAlgorithmId();          entry.setAlgorithm(PgpKeyHelper.getAlgorithmInfo(algorithmId)); -        final int bitStrength = JWalk.getInt(match, "components", "key_fingerprint", "nbits"); +        final int bitStrength = match.getBitStrength();          entry.setBitStrength(bitStrength);          ArrayList<String> userIds = new ArrayList<String>(); -        String name = fullName + " <keybase.io/" + keybaseId + ">"; +        String name = fullName + " <keybase.io/" + username + ">";          userIds.add(name); -        try { -            userIds.add("github.com/" + JWalk.getString(match, "components", "github", "val")); -        } catch (JSONException e) { -            // ignore -        } -        try { -            userIds.add("twitter.com/" + JWalk.getString(match, "components", "twitter", "val")); -        } catch (JSONException e) { -            // ignore -        } -        try { -            JSONArray array = JWalk.getArray(match, "components", "websites"); -            JSONObject website = array.getJSONObject(0); -            userIds.add(JWalk.getString(website, "val")); -        } catch (JSONException e) { -            // ignore + +        List<String> proofLabels = match.getProofLabels(); +        for (String proofLabel : proofLabels) { +            userIds.add(proofLabel);          }          entry.setUserIds(userIds);          entry.setPrimaryUserId(name);          return entry;      } -    private JSONObject getFromKeybase(String path, String query) throws QueryFailedException { -        try { -            String url = "https://keybase.io/" + path + URLEncoder.encode(query, "utf8"); -            Log.d(Constants.TAG, "keybase query: " + url); - -            URL realUrl = new URL(url); -            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection(); -            conn.setConnectTimeout(5000); // TODO: Reasonable values for keybase -            conn.setReadTimeout(25000); -            conn.connect(); -            int response = conn.getResponseCode(); -            if (response >= 200 && response < 300) { -                String text = readAll(conn.getInputStream(), conn.getContentEncoding()); -                try { -                    JSONObject json = new JSONObject(text); -                    if (JWalk.getInt(json, "status", "code") != 0) { -                        throw new QueryFailedException("Keybase.io query failed: " + path + "?" + -                                query); -                    } -                    return json; -                } catch (JSONException e) { -                    throw new QueryFailedException("Keybase.io query returned broken JSON"); -                } -            } else { -                String message = readAll(conn.getErrorStream(), conn.getContentEncoding()); -                throw new QueryFailedException("Keybase.io query error (status=" + response + -                        "): " + message); -            } -        } catch (Exception e) { -            throw new QueryFailedException("Keybase.io query error"); -        } -    } -      @Override      public String get(String id) throws QueryFailedException {          try { +            /*              JSONObject user = getUser(id);              return JWalk.getString(user, "them", "public_keys", "primary", "bundle"); -        } catch (Exception e) { +            */ +            return User.keyForUsername(id); +        } catch (KeybaseException e) {              throw new QueryFailedException(e.getMessage());          }      } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/JWalk.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/JWalk.java deleted file mode 100644 index 76797811d..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/JWalk.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright (C) 2014 Tim Bray <tbray@textuality.com> - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program.  If not, see <http://www.gnu.org/licenses/>. - */ - -package org.sufficientlysecure.keychain.util; - -import org.json.JSONArray; -import org.json.JSONException; -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]); -    } - -    private static JSONObject walk(JSONObject json, String... path) throws JSONException { -        int len = path.length - 1; -        int pathIndex = 0; -        try { -            while (pathIndex < len) { -                json = json.getJSONObject(path[pathIndex]); -                pathIndex++; -            } -        } catch (JSONException e) { -            // try to give ’em a nice-looking error -            StringBuilder sb = new StringBuilder(); -            for (int i = 0; i < len; i++) { -                sb.append(path[i]).append('.'); -            } -            sb.append(path[len]); -            throw new JSONException("JWalk error at step " + pathIndex + " of " + sb); -        } -        return json; -    } -} diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index e24ac6925..91676f09c 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -332,7 +332,7 @@      <string name="hint_public_keys">Name/Email/Key ID…</string>      <string name="hint_secret_keys">Search Secret Keys</string>      <string name="action_share_key_with">Share Key with…</string> -    <string name="hint_keybase_search">Name/Keybase.io username…</string> +    <string name="hint_keybase_search">Search Keybase.io for…</string>      <!-- key bit length selections -->      <string name="key_size_512">512</string>  | 
