diff options
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure')
3 files changed, 70 insertions, 3 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/linked/LinkedCookieResource.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/linked/LinkedCookieResource.java index 2b9156417..940e0f7eb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/linked/LinkedCookieResource.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/linked/LinkedCookieResource.java @@ -158,6 +158,7 @@ public abstract class LinkedCookieResource extends LinkedResource { private final String mReason; HttpStatusException(int statusCode, String reason) { + super("http status " + statusCode + ": " + reason); mStatusCode = statusCode; mReason = reason; } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/linked/LinkedResource.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/linked/LinkedResource.java index f91a24d57..476d0b21e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/linked/LinkedResource.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/linked/LinkedResource.java @@ -3,6 +3,7 @@ package org.sufficientlysecure.keychain.pgp.linked; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.pgp.linked.resources.DnsResource; import org.sufficientlysecure.keychain.pgp.linked.resources.GenericHttpsResource; +import org.sufficientlysecure.keychain.pgp.linked.resources.GithubResource; import org.sufficientlysecure.keychain.pgp.linked.resources.TwitterResource; import org.sufficientlysecure.keychain.util.Log; @@ -24,7 +25,7 @@ public abstract class LinkedResource { protected final Set<String> mFlags; protected final HashMap<String,String> mParams; - static Pattern magicPattern = + public static Pattern magicPattern = Pattern.compile("\\[Verifying my PGP key: openpgp4fpr:([a-zA-Z0-9]+)]"); protected LinkedResource(Set<String> flags, HashMap<String, String> params, URI uri) { @@ -98,6 +99,10 @@ public abstract class LinkedResource { if (res != null) { return res; } + res = GithubResource.create(flags, params, subUri); + if (res != null) { + return res; + } return null; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/linked/resources/GithubResource.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/linked/resources/GithubResource.java index 43cbed958..400a0a678 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/linked/resources/GithubResource.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/linked/resources/GithubResource.java @@ -41,7 +41,7 @@ public class GithubResource extends LinkedCookieResource { mGistId = gistId; } - public static String generateText (Context context, byte[] fingerprint) { + public static String generate(Context context, byte[] fingerprint) { String cookie = LinkedCookieResource.generate(context, fingerprint); return String.format(context.getResources().getString(R.string.linked_id_github_text), cookie); @@ -56,6 +56,7 @@ public class GithubResource extends LinkedCookieResource { try { HttpGet httpGet = new HttpGet("https://api.github.com/gists/" + mGistId); + httpGet.setHeader("User-Agent", "OpenKeychain"); String response = getResponseBody(httpGet); @@ -93,7 +94,67 @@ public class GithubResource extends LinkedCookieResource { } public static GithubResource searchInGithubStream(String screenName, String needle) { - // TODO implement + + // narrow the needle down to important part + Matcher matcher = magicPattern.matcher(needle); + if (!matcher.find()) { + Log.e(Constants.TAG, "needle didn't contain cookie!"); + return null; + } + needle = matcher.group(); + + try { + + JSONArray array; { + HttpGet httpGet = + new HttpGet("https://api.github.com/users/" + screenName + "/gists"); + httpGet.setHeader("Content-Type", "application/json"); + httpGet.setHeader("User-Agent", "OpenKeychain"); + + String response = getResponseBody(httpGet); + array = new JSONArray(response); + } + + for (int i = 0, j = Math.min(array.length(), 5); i < j; i++) { + JSONObject obj = array.getJSONObject(i); + + JSONObject files = obj.getJSONObject("files"); + Iterator<String> it = files.keys(); + if (it.hasNext()) { + + JSONObject file = files.getJSONObject(it.next()); + String type = file.getString("type"); + if (!"text/plain".equals(type)) { + continue; + } + String id = obj.getString("id"); + HttpGet httpGet = new HttpGet("https://api.github.com/gists/" + id); + httpGet.setHeader("User-Agent", "OpenKeychain"); + + JSONObject gistObj = new JSONObject(getResponseBody(httpGet)); + JSONObject gistFiles = gistObj.getJSONObject("files"); + Iterator<String> gistIt = gistFiles.keys(); + if (!gistIt.hasNext()) { + continue; + } + // TODO can there be multiple candidates? + JSONObject gistFile = gistFiles.getJSONObject(gistIt.next()); + String content = gistFile.getString("content"); + if (!content.contains(needle)) { + continue; + } + + URI uri = URI.create("https://gist.github.com/" + screenName + "/" + id); + return create(uri); + } + } + + // update the results with the body of the response + return null; + } catch (JSONException | HttpStatusException | IOException e) { + Log.e(Constants.TAG, "exception parsing stream", e); + } + return null; } |