okHttp
This commit is contained in:
parent
4b3d584d1e
commit
78a30ed207
|
@ -2,12 +2,10 @@ package org.sufficientlysecure.keychain.linked;
|
|||
|
||||
import android.content.Context;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpRequestBase;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.params.BasicHttpParams;
|
||||
import com.squareup.okhttp.CertificatePinner;
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import com.squareup.okhttp.Request;
|
||||
import com.squareup.okhttp.Response;
|
||||
import org.json.JSONException;
|
||||
import org.sufficientlysecure.keychain.Constants;
|
||||
import org.sufficientlysecure.keychain.linked.resources.GenericHttpsResource;
|
||||
|
@ -18,12 +16,8 @@ import org.sufficientlysecure.keychain.operations.results.OperationResult.LogTyp
|
|||
import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog;
|
||||
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
|
||||
import org.sufficientlysecure.keychain.util.Log;
|
||||
import org.thoughtcrime.ssl.pinning.util.PinningHelper;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.util.HashMap;
|
||||
|
@ -233,46 +227,35 @@ public abstract class LinkedTokenResource extends LinkedResource {
|
|||
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation") // HttpRequestBase is deprecated
|
||||
public static String getResponseBody(Context context, HttpRequestBase request)
|
||||
throws IOException, HttpStatusException {
|
||||
return getResponseBody(context, request, null);
|
||||
|
||||
private static CertificatePinner getCertificatePinner(String hostname, String[] pins){
|
||||
CertificatePinner.Builder builder = new CertificatePinner.Builder();
|
||||
for(String pin : pins){
|
||||
builder.add(hostname,pin);
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation") // HttpRequestBase is deprecated
|
||||
public static String getResponseBody(Context context, HttpRequestBase request, String[] pins)
|
||||
throws IOException, HttpStatusException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
public static String getResponseBody(Request request, String... pins)
|
||||
throws IOException, HttpStatusException {
|
||||
|
||||
request.setHeader("User-Agent", "Open Keychain");
|
||||
|
||||
|
||||
HttpClient httpClient;
|
||||
if (pins == null) {
|
||||
httpClient = new DefaultHttpClient(new BasicHttpParams());
|
||||
} else {
|
||||
httpClient = PinningHelper.getPinnedHttpClient(context, pins);
|
||||
Log.d("Connection to: "+request.url().getHost(),"");
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
if(pins !=null){
|
||||
client.setCertificatePinner(getCertificatePinner(request.url().getHost(),pins));
|
||||
}
|
||||
|
||||
HttpResponse response = httpClient.execute(request);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
String reason = response.getStatusLine().getReasonPhrase();
|
||||
Response response = client.newCall(request).execute();
|
||||
|
||||
|
||||
int statusCode = response.code();
|
||||
String reason = response.message();
|
||||
|
||||
if (statusCode != 200) {
|
||||
throw new HttpStatusException(statusCode, reason);
|
||||
}
|
||||
|
||||
HttpEntity entity = response.getEntity();
|
||||
InputStream inputStream = entity.getContent();
|
||||
|
||||
BufferedReader bReader = new BufferedReader(
|
||||
new InputStreamReader(inputStream, "UTF-8"), 8);
|
||||
String line;
|
||||
while ((line = bReader.readLine()) != null) {
|
||||
sb.append(line);
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
return response.body().string();
|
||||
}
|
||||
|
||||
public static class HttpStatusException extends Throwable {
|
||||
|
|
|
@ -6,7 +6,7 @@ import android.net.Uri;
|
|||
import android.support.annotation.DrawableRes;
|
||||
import android.support.annotation.StringRes;
|
||||
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import com.squareup.okhttp.Request;
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType;
|
||||
import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog;
|
||||
|
@ -32,14 +32,16 @@ public class GenericHttpsResource extends LinkedTokenResource {
|
|||
token, "0x" + KeyFormattingUtils.convertFingerprintToHex(fingerprint).substring(24));
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation") // HttpGet is deprecated
|
||||
@Override
|
||||
protected String fetchResource (Context context, OperationLog log, int indent)
|
||||
throws HttpStatusException, IOException {
|
||||
|
||||
log.add(LogType.MSG_LV_FETCH, indent, mSubUri.toString());
|
||||
HttpGet httpGet = new HttpGet(mSubUri);
|
||||
return getResponseBody(context, httpGet);
|
||||
Request request = new Request.Builder()
|
||||
.url(mSubUri.toURL())
|
||||
.addHeader("User-Agent", "OpenKeychain")
|
||||
.build();
|
||||
return getResponseBody(request);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ import android.net.Uri;
|
|||
import android.support.annotation.DrawableRes;
|
||||
import android.support.annotation.StringRes;
|
||||
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import com.squareup.okhttp.Request;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
@ -47,7 +47,7 @@ public class GithubResource extends LinkedTokenResource {
|
|||
return String.format(context.getResources().getString(R.string.linked_id_github_text), token);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation") // HttpGet is deprecated
|
||||
|
||||
@Override
|
||||
protected String fetchResource (Context context, OperationLog log, int indent)
|
||||
throws HttpStatusException, IOException, JSONException {
|
||||
|
@ -55,8 +55,11 @@ public class GithubResource extends LinkedTokenResource {
|
|||
log.add(LogType.MSG_LV_FETCH, indent, mSubUri.toString());
|
||||
indent += 1;
|
||||
|
||||
HttpGet httpGet = new HttpGet("https://api.github.com/gists/" + mGistId);
|
||||
String response = getResponseBody(context, httpGet);
|
||||
Request request = new Request.Builder()
|
||||
.url("https://api.github.com/gists/" + mGistId)
|
||||
.addHeader("User-Agent", "OpenKeychain")
|
||||
.build();
|
||||
String response = getResponseBody(request);
|
||||
|
||||
JSONObject obj = new JSONObject(response);
|
||||
|
||||
|
@ -79,7 +82,7 @@ public class GithubResource extends LinkedTokenResource {
|
|||
|
||||
}
|
||||
|
||||
@Deprecated // not used for now, but could be used to pick up earlier posted gist if already present?
|
||||
|
||||
@SuppressWarnings({ "deprecation", "unused" })
|
||||
public static GithubResource searchInGithubStream(
|
||||
Context context, String screenName, String needle, OperationLog log) {
|
||||
|
@ -94,12 +97,12 @@ public class GithubResource extends LinkedTokenResource {
|
|||
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(context, httpGet);
|
||||
Request request = new Request.Builder()
|
||||
.url("https://api.github.com/users/" + screenName + "/gists")
|
||||
.addHeader("Content-Type", "application/json")
|
||||
.addHeader("User-Agent", "OpenKeychain")
|
||||
.build();
|
||||
String response = getResponseBody(request);
|
||||
array = new JSONArray(response);
|
||||
}
|
||||
|
||||
|
@ -116,10 +119,13 @@ public class GithubResource extends LinkedTokenResource {
|
|||
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(context, httpGet));
|
||||
Request request = new Request.Builder()
|
||||
.url("https://api.github.com/gists/" + id)
|
||||
.addHeader("User-Agent", "OpenKeychain")
|
||||
.build();
|
||||
|
||||
JSONObject gistObj = new JSONObject(getResponseBody(request));
|
||||
JSONObject gistFiles = gistObj.getJSONObject("files");
|
||||
Iterator<String> gistIt = gistFiles.keys();
|
||||
if (!gistIt.hasNext()) {
|
||||
|
|
|
@ -7,11 +7,11 @@ import android.support.annotation.DrawableRes;
|
|||
import android.support.annotation.StringRes;
|
||||
import android.util.Log;
|
||||
|
||||
import com.squareup.okhttp.MediaType;
|
||||
import com.squareup.okhttp.Request;
|
||||
import com.squareup.okhttp.RequestBody;
|
||||
import com.textuality.keybase.lib.JWalk;
|
||||
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
@ -84,18 +84,19 @@ public class TwitterResource extends LinkedTokenResource {
|
|||
return null;
|
||||
}
|
||||
|
||||
HttpGet httpGet =
|
||||
new HttpGet("https://api.twitter.com/1.1/statuses/show.json"
|
||||
+ "?id=" + mTweetId
|
||||
+ "&include_entities=false");
|
||||
|
||||
// construct a normal HTTPS request and include an Authorization
|
||||
// header with the value of Bearer <>
|
||||
httpGet.setHeader("Authorization", "Bearer " + authToken);
|
||||
httpGet.setHeader("Content-Type", "application/json");
|
||||
Request request = new Request.Builder()
|
||||
.url("https://api.twitter.com/1.1/statuses/show.json"
|
||||
+ "?id=" + mTweetId
|
||||
+ "&include_entities=false")
|
||||
.addHeader("Authorization", "Bearer " + authToken)
|
||||
.addHeader("Content-Type", "application/json")
|
||||
.addHeader("User-Agent", "OpenKeychain")
|
||||
.build();
|
||||
|
||||
try {
|
||||
String response = getResponseBody(context, httpGet, CERT_PINS);
|
||||
String response = getResponseBody(request, CERT_PINS);
|
||||
JSONObject obj = new JSONObject(response);
|
||||
JSONObject user = obj.getJSONObject("user");
|
||||
if (!mHandle.equalsIgnoreCase(user.getString("screen_name"))) {
|
||||
|
@ -157,21 +158,20 @@ public class TwitterResource extends LinkedTokenResource {
|
|||
return null;
|
||||
}
|
||||
|
||||
HttpGet httpGet =
|
||||
new HttpGet("https://api.twitter.com/1.1/statuses/user_timeline.json"
|
||||
Request request = new Request.Builder()
|
||||
.url("https://api.twitter.com/1.1/statuses/user_timeline.json"
|
||||
+ "?screen_name=" + screenName
|
||||
+ "&count=15"
|
||||
+ "&include_rts=false"
|
||||
+ "&trim_user=true"
|
||||
+ "&exclude_replies=true");
|
||||
|
||||
// construct a normal HTTPS request and include an Authorization
|
||||
// header with the value of Bearer <>
|
||||
httpGet.setHeader("Authorization", "Bearer " + authToken);
|
||||
httpGet.setHeader("Content-Type", "application/json");
|
||||
+ "&exclude_replies=true")
|
||||
.addHeader("Authorization", "Bearer " + authToken)
|
||||
.addHeader("Content-Type", "application/json")
|
||||
.addHeader("User-Agent", "OpenKeychain")
|
||||
.build();
|
||||
|
||||
try {
|
||||
String response = getResponseBody(context, httpGet, CERT_PINS);
|
||||
String response = getResponseBody(request, CERT_PINS);
|
||||
JSONArray array = new JSONArray(response);
|
||||
|
||||
for (int i = 0; i < array.length(); i++) {
|
||||
|
@ -216,12 +216,20 @@ public class TwitterResource extends LinkedTokenResource {
|
|||
String base64Encoded = rot13("D293FQqanH0jH29KIaWJER5DomqSGRE2Ewc1LJACn3cbD1c"
|
||||
+ "Fq1bmqSAQAz5MI2cIHKOuo3cPoRAQI1OyqmIVFJS6LHMXq2g6MRLkIj") + "==";
|
||||
|
||||
RequestBody requestBody = RequestBody.create(
|
||||
MediaType.parse("application/x-www-form-urlencoded;charset=UTF-8"),
|
||||
"grant_type=client_credentials");
|
||||
|
||||
// Step 2: Obtain a bearer token
|
||||
HttpPost httpPost = new HttpPost("https://api.twitter.com/oauth2/token");
|
||||
httpPost.setHeader("Authorization", "Basic " + base64Encoded);
|
||||
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
|
||||
httpPost.setEntity(new StringEntity("grant_type=client_credentials"));
|
||||
JSONObject rawAuthorization = new JSONObject(getResponseBody(context, httpPost, CERT_PINS));
|
||||
Request request = new Request.Builder()
|
||||
.url("https://api.twitter.com/oauth2/token")
|
||||
.addHeader("Authorization", "Basic " + base64Encoded)
|
||||
.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
|
||||
.addHeader("User-Agent", "OpenKeychain")
|
||||
.post(requestBody)
|
||||
.build();
|
||||
|
||||
JSONObject rawAuthorization = new JSONObject(getResponseBody(request, CERT_PINS));
|
||||
|
||||
// Applications should verify that the value associated with the
|
||||
// token_type key of the returned object is bearer
|
||||
|
|
Loading…
Reference in a new issue