work on cookie scanning during creation

This commit is contained in:
Vincent Breitmoser 2015-03-24 16:41:01 +01:00
parent f9ef1160ca
commit e573cd774a
12 changed files with 155 additions and 151 deletions

View File

@ -771,6 +771,12 @@ public abstract class OperationResult implements Parcelable {
MSG_LV_FP_OK (LogLevel.DEBUG, R.string.msg_lv_fp_ok),
MSG_LV_FP_ERROR (LogLevel.ERROR, R.string.msg_lv_fp_error),
MSG_LV_ERROR_TWITTER_AUTH (LogLevel.ERROR, R.string.msg_lv_error_twitter_auth),
MSG_LV_ERROR_TWITTER_HANDLE (LogLevel.ERROR, R.string.msg_lv_error_twitter_handle),
MSG_LV_ERROR_TWITTER_RESPONSE (LogLevel.ERROR, R.string.msg_lv_error_twitter_response),
MSG_LV_ERROR_GITHUB_HANDLE (LogLevel.ERROR, R.string.msg_lv_error_github_handle),
MSG_LV_ERROR_GITHUB_NOT_FOUND (LogLevel.ERROR, R.string.msg_lv_error_github_not_found),
MSG_LV_FETCH (LogLevel.DEBUG, R.string.msg_lv_fetch),
MSG_LV_FETCH_REDIR (LogLevel.DEBUG, R.string.msg_lv_fetch_redir),
MSG_LV_FETCH_OK (LogLevel.DEBUG, R.string.msg_lv_fetch_ok),
@ -778,14 +784,14 @@ public abstract class OperationResult implements Parcelable {
MSG_LV_FETCH_ERROR_URL (LogLevel.ERROR, R.string.msg_lv_fetch_error_url),
MSG_LV_FETCH_ERROR_IO (LogLevel.ERROR, R.string.msg_lv_fetch_error_io),
MSG_LV_FETCH_ERROR_FORMAT(LogLevel.ERROR, R.string.msg_lv_fetch_error_format),
MSG_LV_FETCH_ERROR_NOTHING (LogLevel.ERROR, R.string.msg_lv_fetch_error_nothing),
//export log
MSG_EXPORT_LOG(LogLevel.START,R.string.msg_export_log_start),
MSG_EXPORT_LOG_EXPORT_ERROR_NO_FILE(LogLevel.ERROR,R.string.msg_export_log_error_no_file),
MSG_EXPORT_LOG_EXPORT_ERROR_FOPEN(LogLevel.ERROR,R.string.msg_export_log_error_fopen),
MSG_EXPORT_LOG_EXPORT_ERROR_WRITING(LogLevel.ERROR,R.string.msg_export_log_error_writing),
MSG_EXPORT_LOG_EXPORT_SUCCESS (LogLevel.OK, R.string.msg_export_log_success),
;
MSG_EXPORT_LOG_EXPORT_SUCCESS (LogLevel.OK, R.string.msg_export_log_success);
public final int mMsgId;
public final LogLevel mLevel;

View File

@ -4,10 +4,10 @@ import android.content.Context;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONException;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.operations.results.LinkedVerifyResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType;
@ -19,6 +19,7 @@ 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;
import java.util.Map.Entry;
@ -86,7 +87,23 @@ public abstract class LinkedCookieResource extends LinkedResource {
log.add(LogType.MSG_LV, 0);
// Try to fetch resource. Logs for itself
String res = fetchResource(log, 1);
String res = null;
try {
res = fetchResource(log, 1);
} catch (HttpStatusException e) {
// log verbose output to logcat
Log.e(Constants.TAG, "http error (" + e.getStatus() + "): " + e.getReason());
log.add(LogType.MSG_LV_FETCH_ERROR, 2, Integer.toString(e.getStatus()));
} catch (MalformedURLException e) {
log.add(LogType.MSG_LV_FETCH_ERROR_URL, 2);
} catch (IOException e) {
Log.e(Constants.TAG, "io error", e);
log.add(LogType.MSG_LV_FETCH_ERROR_IO, 2);
} catch (JSONException e) {
Log.e(Constants.TAG, "json error", e);
log.add(LogType.MSG_LV_FETCH_ERROR_FORMAT, 2);
}
if (res == null) {
// if this is null, an error was recorded in fetchResource above
return new LinkedVerifyResult(LinkedVerifyResult.RESULT_ERROR, log);
@ -98,7 +115,8 @@ public abstract class LinkedCookieResource extends LinkedResource {
}
protected abstract String fetchResource (OperationLog log, int indent);
protected abstract String fetchResource (OperationLog log, int indent) throws HttpStatusException, IOException,
JSONException;
protected Matcher matchResource (OperationLog log, int indent, String res) {
return magicPattern.matcher(res);
@ -130,6 +148,8 @@ public abstract class LinkedCookieResource extends LinkedResource {
public static String getResponseBody(HttpRequestBase request) throws IOException, HttpStatusException {
StringBuilder sb = new StringBuilder();
request.setHeader("User-Agent", "Open Keychain");
DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
HttpResponse response = httpClient.execute(request);
int statusCode = response.getStatusLine().getStatusCode();

View File

@ -8,6 +8,7 @@ import android.support.annotation.StringRes;
import com.textuality.keybase.lib.Search;
import org.apache.http.client.methods.HttpGet;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType;
@ -40,53 +41,15 @@ public class GenericHttpsResource extends LinkedCookieResource {
}
@Override
protected String fetchResource (OperationLog log, int indent) {
protected String fetchResource (OperationLog log, int indent) throws HttpStatusException, IOException {
log.add(LogType.MSG_LV_FETCH, indent, mSubUri.toString());
indent += 1;
try {
HttpGet httpGet = new HttpGet(mSubUri);
return getResponseBody(httpGet);
HttpsURLConnection conn = null;
URL url = mSubUri.toURL();
int status = 0;
int redirects = 0;
while (redirects < 5) {
conn = (HttpsURLConnection) url.openConnection();
conn.addRequestProperty("User-Agent", "OpenKeychain");
conn.setConnectTimeout(5000);
conn.setReadTimeout(25000);
conn.connect();
status = conn.getResponseCode();
if (status == 301) {
redirects++;
url = new URL(conn.getHeaderFields().get("Location").get(0));
log.add(LogType.MSG_LV_FETCH_REDIR, indent, url.toString());
} else {
break;
}
}
if (status >= 200 && status < 300) {
log.add(LogType.MSG_LV_FETCH_OK, indent, Integer.toString(status));
return Search.snarf(conn.getInputStream());
} else {
// log verbose output to logcat
Log.e(Constants.TAG, Search.snarf(conn.getErrorStream()));
log.add(LogType.MSG_LV_FETCH_ERROR, indent, Integer.toString(status));
return null;
}
} catch (MalformedURLException e) {
log.add(LogType.MSG_LV_FETCH_ERROR_URL, indent);
return null;
} catch (IOException e) {
Log.e(Constants.TAG, "io error", e);
e.printStackTrace();
log.add(LogType.MSG_LV_FETCH_ERROR_IO, indent);
return null;
}
// log.add(LogType.MSG_LV_FETCH_REDIR, indent, url.toString());
}

View File

@ -48,23 +48,20 @@ public class GithubResource extends LinkedCookieResource {
}
@Override
protected String fetchResource (OperationLog log, int indent) {
protected String fetchResource (OperationLog log, int indent)
throws HttpStatusException, IOException, JSONException {
log.add(LogType.MSG_LV_FETCH, indent, mSubUri.toString());
indent += 1;
try {
HttpGet httpGet = new HttpGet("https://api.github.com/gists/" + mGistId);
httpGet.setHeader("User-Agent", "OpenKeychain");
String response = getResponseBody(httpGet);
JSONObject obj = new JSONObject(response);
JSONObject owner = obj.getJSONObject("owner");
if (!mHandle.equals(owner.getString("login"))) {
log.add(LogType.MSG_LV_FETCH_ERROR_FORMAT, indent);
log.add(LogType.MSG_LV_ERROR_GITHUB_HANDLE, indent);
return null;
}
@ -76,30 +73,18 @@ public class GithubResource extends LinkedCookieResource {
return file.getString("content");
}
} catch (HttpStatusException e) {
// log verbose output to logcat
Log.e(Constants.TAG, "http error (" + e.getStatus() + "): " + e.getReason());
log.add(LogType.MSG_LV_FETCH_ERROR, indent, Integer.toString(e.getStatus()));
} catch (MalformedURLException e) {
log.add(LogType.MSG_LV_FETCH_ERROR_URL, indent);
} catch (IOException e) {
Log.e(Constants.TAG, "io error", e);
log.add(LogType.MSG_LV_FETCH_ERROR_IO, indent);
} catch (JSONException e) {
Log.e(Constants.TAG, "json error", e);
log.add(LogType.MSG_LV_FETCH_ERROR_FORMAT, indent);
}
log.add(LogType.MSG_LV_ERROR_GITHUB_NOT_FOUND, indent);
return null;
}
public static GithubResource searchInGithubStream(String screenName, String needle) {
public static GithubResource searchInGithubStream(String screenName, String needle,
OperationLog log) {
// 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;
throw new AssertionError("Needle must contain cookie pattern! This is a programming error, please report.");
}
needle = matcher.group();
@ -150,9 +135,21 @@ public class GithubResource extends LinkedCookieResource {
}
// update the results with the body of the response
log.add(LogType.MSG_LV_FETCH_ERROR_NOTHING, 2);
return null;
} catch (JSONException | HttpStatusException | IOException e) {
Log.e(Constants.TAG, "exception parsing stream", e);
} catch (HttpStatusException e) {
// log verbose output to logcat
Log.e(Constants.TAG, "http error (" + e.getStatus() + "): " + e.getReason());
log.add(LogType.MSG_LV_FETCH_ERROR, 2, Integer.toString(e.getStatus()));
} catch (MalformedURLException e) {
log.add(LogType.MSG_LV_FETCH_ERROR_URL, 2);
} catch (IOException e) {
Log.e(Constants.TAG, "io error", e);
log.add(LogType.MSG_LV_FETCH_ERROR_IO, 2);
} catch (JSONException e) {
Log.e(Constants.TAG, "json error", e);
log.add(LogType.MSG_LV_FETCH_ERROR_FORMAT, 2);
}
return null;

View File

@ -22,6 +22,7 @@ import org.sufficientlysecure.keychain.operations.results.OperationResult.Operat
import org.sufficientlysecure.keychain.pgp.linked.LinkedCookieResource;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.HashMap;
import java.util.HashSet;
@ -66,11 +67,14 @@ public class TwitterResource extends LinkedCookieResource {
}
@Override
protected String fetchResource(OperationLog log, int indent) {
protected String fetchResource(OperationLog log, int indent) throws IOException, HttpStatusException,
JSONException {
String authToken = getAuthToken();
if (authToken == null) {
String authToken;
try {
authToken = getAuthToken();
} catch (IOException | HttpStatusException | JSONException e) {
log.add(LogType.MSG_LV_ERROR_TWITTER_AUTH, indent);
return null;
}
@ -87,32 +91,19 @@ public class TwitterResource extends LinkedCookieResource {
try {
String response = getResponseBody(httpGet);
JSONObject obj = new JSONObject(response);
if (!obj.has("text")) {
return null;
}
JSONObject user = obj.getJSONObject("user");
if (!mHandle.equalsIgnoreCase(user.getString("screen_name"))) {
log.add(LogType.MSG_LV_FETCH_ERROR_FORMAT, indent);
log.add(LogType.MSG_LV_ERROR_TWITTER_HANDLE, indent);
return null;
}
// update the results with the body of the response
return obj.getString("text");
} catch (HttpStatusException e) {
// log verbose output to logcat
Log.e(Constants.TAG, "http error (" + e.getStatus() + "): " + e.getReason());
log.add(LogType.MSG_LV_FETCH_ERROR, indent, Integer.toString(e.getStatus()));
} catch (IOException e) {
Log.e(Constants.TAG, "io error", e);
log.add(LogType.MSG_LV_FETCH_ERROR_IO, indent);
} catch (JSONException e) {
Log.e(Constants.TAG, "json error", e);
log.add(LogType.MSG_LV_FETCH_ERROR_FORMAT, indent);
log.add(LogType.MSG_LV_ERROR_TWITTER_RESPONSE, indent);
return null;
}
return null;
}
@Override
@ -148,11 +139,14 @@ public class TwitterResource extends LinkedCookieResource {
return intent;
}
public static TwitterResource searchInTwitterStream(String screenName, String needle) {
public static TwitterResource searchInTwitterStream(
String screenName, String needle, OperationLog log) {
String authToken = getAuthToken();
if (authToken == null) {
String authToken;
try {
authToken = getAuthToken();
} catch (IOException | HttpStatusException | JSONException e) {
log.add(LogType.MSG_LV_ERROR_TWITTER_AUTH, 1);
return null;
}
@ -184,22 +178,32 @@ public class TwitterResource extends LinkedCookieResource {
}
// update the results with the body of the response
log.add(LogType.MSG_LV_FETCH_ERROR_NOTHING, 1);
return null;
} catch (JSONException | HttpStatusException | IOException e) {
Log.e(Constants.TAG, "exception parsing stream", e);
} catch (HttpStatusException e) {
// log verbose output to logcat
Log.e(Constants.TAG, "http error (" + e.getStatus() + "): " + e.getReason());
log.add(LogType.MSG_LV_FETCH_ERROR, 1, Integer.toString(e.getStatus()));
} catch (MalformedURLException e) {
log.add(LogType.MSG_LV_FETCH_ERROR_URL, 1);
} catch (IOException e) {
Log.e(Constants.TAG, "io error", e);
log.add(LogType.MSG_LV_FETCH_ERROR_IO, 1);
} catch (JSONException e) {
Log.e(Constants.TAG, "json error", e);
log.add(LogType.MSG_LV_FETCH_ERROR_FORMAT, 1);
}
return null;
}
private static String authToken;
private static String cachedAuthToken;
private static String getAuthToken() {
if (authToken != null) {
return authToken;
private static String getAuthToken() throws IOException, HttpStatusException, JSONException {
if (cachedAuthToken != null) {
return cachedAuthToken;
}
try {
String base64Encoded = rot13("D293FQqanH0jH29KIaWJER5DomqSGRE2Ewc1LJACn3cbD1c"
+ "Fq1bmqSAQAz5MI2cIHKOuo3cPoRAQI1OyqmIVFJS6LHMXq2g6MRLkIj") + "==";
@ -213,16 +217,11 @@ public class TwitterResource extends LinkedCookieResource {
// Applications should verify that the value associated with the
// token_type key of the returned object is bearer
if (!"bearer".equals(JWalk.getString(rawAuthorization, "token_type"))) {
return null;
throw new JSONException("Expected bearer token in response!");
}
authToken = JWalk.getString(rawAuthorization, "access_token");
return authToken;
} catch (JSONException | IllegalStateException | HttpStatusException | IOException ex) {
Log.e(Constants.TAG, "exception fetching auth token", ex);
return null;
}
cachedAuthToken = rawAuthorization.getString("access_token");
return cachedAuthToken;
}

View File

@ -31,6 +31,7 @@ import android.widget.TextView;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.compatibility.ClipboardReflection;
import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog;
import org.sufficientlysecure.keychain.pgp.linked.LinkedCookieResource;
import org.sufficientlysecure.keychain.pgp.linked.resources.DnsResource;
import org.sufficientlysecure.keychain.ui.util.Notify;
@ -104,7 +105,7 @@ public class LinkedIdCreateDnsStep2Fragment extends LinkedIdCreateFinalFragment
}
@Override
LinkedCookieResource getResource() {
LinkedCookieResource getResource(OperationLog log) {
return DnsResource.createNew(mResourceDomain);
}

View File

@ -20,6 +20,7 @@ import android.widget.ViewAnimator;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.operations.results.LinkedVerifyResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog;
import org.sufficientlysecure.keychain.pgp.WrappedUserAttribute;
import org.sufficientlysecure.keychain.pgp.linked.LinkedCookieResource;
import org.sufficientlysecure.keychain.pgp.linked.LinkedIdentity;
@ -28,6 +29,8 @@ import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler;
import org.sufficientlysecure.keychain.service.SaveKeyringParcel;
import org.sufficientlysecure.keychain.ui.PassphraseDialogActivity;
import org.sufficientlysecure.keychain.ui.util.Notify;
import org.sufficientlysecure.keychain.util.Passphrase;
public abstract class LinkedIdCreateFinalFragment extends Fragment {
@ -95,7 +98,7 @@ public abstract class LinkedIdCreateFinalFragment extends Fragment {
return view;
}
abstract LinkedCookieResource getResource();
abstract LinkedCookieResource getResource(OperationLog log);
private void setVerifyProgress(boolean on, Boolean success) {
if (success == null) {
@ -133,7 +136,12 @@ public abstract class LinkedIdCreateFinalFragment extends Fragment {
protected LinkedVerifyResult doInBackground(Void... params) {
long timer = System.currentTimeMillis();
LinkedCookieResource resource = getResource();
OperationLog log = new OperationLog();
LinkedCookieResource resource = getResource(log);
if (resource == null) {
return new LinkedVerifyResult(LinkedVerifyResult.RESULT_ERROR, log);
}
LinkedVerifyResult result = resource.verify(mLinkedIdWizard.mFingerprint);
// ux flow: this operation should take at last a second
@ -178,7 +186,7 @@ public abstract class LinkedIdCreateFinalFragment extends Fragment {
}
private void certifyLinkedIdentity (String passphrase) {
private void certifyLinkedIdentity (Passphrase passphrase) {
KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(
getActivity(),
getString(R.string.progress_saving),
@ -227,7 +235,7 @@ public abstract class LinkedIdCreateFinalFragment extends Fragment {
// fill values for this action
Bundle data = new Bundle();
data.putString(KeychainIntentService.EDIT_KEYRING_PASSPHRASE, passphrase);
data.putParcelable(KeychainIntentService.EDIT_KEYRING_PASSPHRASE, passphrase);
data.putParcelable(KeychainIntentService.EDIT_KEYRING_PARCEL, skp);
intent.putExtra(KeychainIntentService.EXTRA_DATA, data);
@ -249,8 +257,8 @@ public abstract class LinkedIdCreateFinalFragment extends Fragment {
switch (requestCode) {
case REQUEST_CODE_PASSPHRASE:
if (resultCode == Activity.RESULT_OK && data != null) {
String passphrase =
data.getStringExtra(PassphraseDialogActivity.MESSAGE_DATA_PASSPHRASE);
Passphrase passphrase =
data.getParcelableExtra(PassphraseDialogActivity.MESSAGE_DATA_PASSPHRASE);
certifyLinkedIdentity(passphrase);
}
break;

View File

@ -26,6 +26,7 @@ import android.view.View.OnClickListener;
import android.view.ViewGroup;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog;
import org.sufficientlysecure.keychain.pgp.linked.LinkedCookieResource;
import org.sufficientlysecure.keychain.pgp.linked.resources.GithubResource;
@ -82,8 +83,8 @@ public class LinkedIdCreateGithubStep2Fragment extends LinkedIdCreateFinalFragme
}
@Override
LinkedCookieResource getResource() {
return GithubResource.searchInGithubStream(mResourceHandle, mResourceString);
LinkedCookieResource getResource(OperationLog log) {
return GithubResource.searchInGithubStream(mResourceHandle, mResourceString, log);
}
@Override

View File

@ -30,6 +30,7 @@ import android.widget.EditText;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog;
import org.sufficientlysecure.keychain.pgp.linked.resources.GenericHttpsResource;
import org.sufficientlysecure.keychain.ui.util.Notify;
import org.sufficientlysecure.keychain.ui.util.Notify.Style;
@ -66,7 +67,7 @@ public class LinkedIdCreateHttpsStep2Fragment extends LinkedIdCreateFinalFragmen
}
@Override
GenericHttpsResource getResource() {
GenericHttpsResource getResource(OperationLog log) {
return GenericHttpsResource.createNew(mResourceUri);
}

View File

@ -27,6 +27,7 @@ import android.view.ViewGroup;
import android.widget.EditText;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog;
import org.sufficientlysecure.keychain.pgp.linked.LinkedCookieResource;
import org.sufficientlysecure.keychain.pgp.linked.resources.TwitterResource;
@ -82,8 +83,8 @@ public class LinkedIdCreateTwitterStep2Fragment extends LinkedIdCreateFinalFragm
}
@Override
LinkedCookieResource getResource() {
return TwitterResource.searchInTwitterStream(mResourceHandle, mResourceString);
LinkedCookieResource getResource(OperationLog log) {
return TwitterResource.searchInTwitterStream(mResourceHandle, mResourceString, log);
}
@Override

View File

@ -133,7 +133,7 @@ public class LinkedIdViewFragment extends Fragment implements
case LOADER_ID_LINKED_ID:
if (!cursor.moveToFirst()) {
Notify.createNotify(getActivity(), "Error loading identity!",
Notify.create(getActivity(), "Error loading identity!",
Notify.LENGTH_LONG, Style.ERROR).show();
finishFragment();
break;

View File

@ -1175,6 +1175,12 @@
<string name="msg_lv_fp_ok">"Fingerprint ok."</string>
<string name="msg_lv_fp_error">"Fingerprint mismatch!"</string>
<string name="msg_lv_error_twitter_auth">"Error obtaining Twitter auth token!"</string>
<string name="msg_lv_error_twitter_handle">"Twitter account handle mismatch in response!"</string>
<string name="msg_lv_error_twitter_response">"Unexpected response from Twitter API!"</string>
<string name="msg_lv_error_github_handle">"Github account handle mismatch in response!"</string>
<string name="msg_lv_error_github_not_found">"Gist contains no matching files!"</string>
<string name="msg_lv_fetch">"Fetching URI '%s'"</string>
<string name="msg_lv_fetch_redir">"Following redirect to '%s'"</string>
<string name="msg_lv_fetch_ok">"Successfully fetched (HTTP %s)"</string>
@ -1182,6 +1188,7 @@
<string name="msg_lv_fetch_error_url">"URL is malformed!"</string>
<string name="msg_lv_fetch_error_io">"IO Error!"</string>
<string name="msg_lv_fetch_error_format">"Format error!"</string>
<string name="msg_lv_fetch_error_nothing">"Resource not found!"</string>
<string name="msg_acc_saved">"Account saved"</string>