From 6225c940f9dbc639a90e4627f312329dd99dd2a3 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Tue, 1 Sep 2015 13:58:04 +0200 Subject: linked: redesign github resource creation, implement ouath flow --- .../ui/linked/LinkedIdCreateGithubFragment.java | 77 ++++++++++++++++++++-- .../keychain/ui/linked/LinkedIdWizard.java | 8 +-- 2 files changed, 74 insertions(+), 11 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/linked') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/linked/LinkedIdCreateGithubFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/linked/LinkedIdCreateGithubFragment.java index b30fc091e..de3493e0a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/linked/LinkedIdCreateGithubFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/linked/LinkedIdCreateGithubFragment.java @@ -26,12 +26,13 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; +import java.net.URI; import java.net.URL; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.NonNull; -import android.support.v4.app.Fragment; +import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; @@ -44,20 +45,34 @@ import org.json.JSONObject; import org.sufficientlysecure.keychain.BuildConfig; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.linked.LinkedAttribute; +import org.sufficientlysecure.keychain.linked.resources.GithubResource; +import org.sufficientlysecure.keychain.operations.results.EditKeyResult; +import org.sufficientlysecure.keychain.pgp.WrappedUserAttribute; +import org.sufficientlysecure.keychain.service.SaveKeyringParcel; +import org.sufficientlysecure.keychain.ui.base.CryptoOperationFragment; import org.sufficientlysecure.keychain.ui.widget.StatusIndicator; import org.sufficientlysecure.keychain.util.Log; -public class LinkedIdCreateGithubFragment extends Fragment { +public class LinkedIdCreateGithubFragment extends CryptoOperationFragment { ViewAnimator mButtonContainer; StatusIndicator mStatus1, mStatus2, mStatus3; + byte[] mFingerprint; + long mMasterKeyId; + private SaveKeyringParcel mSaveKeyringParcel; + public static LinkedIdCreateGithubFragment newInstance() { return new LinkedIdCreateGithubFragment(); } + public LinkedIdCreateGithubFragment() { + super(null); + } + @Override @NonNull public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.linked_create_github_fragment, container, false); @@ -83,6 +98,9 @@ public class LinkedIdCreateGithubFragment extends Fragment { super.onResume(); LinkedIdWizard wizard = (LinkedIdWizard) getActivity(); + mFingerprint = wizard.mFingerprint; + mMasterKeyId = wizard.mMasterKeyId; + final String oAuthCode = wizard.oAuthGetCode(); final String oAuthState = wizard.oAuthGetState(); if (oAuthCode == null) { @@ -90,6 +108,8 @@ public class LinkedIdCreateGithubFragment extends Fragment { return; } + final String gistText = GithubResource.generate(wizard, mFingerprint); + Log.d(Constants.TAG, "got code: " + oAuthCode); new AsyncTask() { @@ -137,7 +157,7 @@ public class LinkedIdCreateGithubFragment extends Fragment { } mStatus1.setDisplayedChild(2); - step2PostGist(result.optString("access_token")); + step2PostGist(result.optString("access_token"), gistText); } }.execute(); @@ -177,7 +197,7 @@ public class LinkedIdCreateGithubFragment extends Fragment { } - private void step2PostGist(final String accessToken) { + private void step2PostGist(final String accessToken, final String gistText) { mStatus2.setDisplayedChild(1); @@ -189,7 +209,7 @@ public class LinkedIdCreateGithubFragment extends Fragment { long timer = System.currentTimeMillis(); JSONObject file = new JSONObject(); - file.put("content", "hello!"); + file.put("content", gistText); JSONObject files = new JSONObject(); files.put("file1.txt", file); @@ -230,12 +250,57 @@ public class LinkedIdCreateGithubFragment extends Fragment { return; } - mStatus2.setDisplayedChild(2); + try { + String gistId = result.getString("id"); + JSONObject owner = result.getJSONObject("owner"); + String gistLogin = owner.getString("login"); + + URI uri = URI.create("https://gist.github.com/" + gistLogin + "/" + gistId); + GithubResource resource = GithubResource.create(uri); + + mStatus2.setDisplayedChild(2); + step3EditKey(resource); + + } catch (JSONException e) { + mStatus2.setDisplayedChild(3); + e.printStackTrace(); + } + } }.execute(); } + private void step3EditKey(GithubResource resource) { + + mStatus3.setDisplayedChild(1); + + WrappedUserAttribute ua = LinkedAttribute.fromResource(resource).toUserAttribute(); + mSaveKeyringParcel = new SaveKeyringParcel(mMasterKeyId, mFingerprint); + mSaveKeyringParcel.mAddUserAttribute.add(ua); + + cryptoOperation(); + + } + + @Nullable + @Override + public SaveKeyringParcel createOperationInput() { + // if this is null, the cryptoOperation silently aborts - which is what we want in that case + return mSaveKeyringParcel; + } + + @Override + public void onCryptoOperationSuccess(EditKeyResult result) { + mStatus3.setDisplayedChild(2); + } + + @Override + public void onCryptoOperationError(EditKeyResult result) { + result.createNotify(getActivity()).show(this); + mStatus3.setDisplayedChild(3); + } + private static JSONObject jsonHttpRequest(String url, JSONObject params, String accessToken) throws IOException { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/linked/LinkedIdWizard.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/linked/LinkedIdWizard.java index 2fb6384b2..3441bb399 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/linked/LinkedIdWizard.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/linked/LinkedIdWizard.java @@ -138,16 +138,14 @@ public class LinkedIdWizard extends BaseActivity { super.onNewIntent(intent); Uri uri = intent.getData(); - if (uri != null) { - Log.d(Constants.TAG, "received oauth uri: " + uri); + Log.d(Constants.TAG, "received oauth uri: " + uri); + if (mOAuthState != null && uri != null) { String state = uri.getQueryParameter("state"); if (!mOAuthState.equalsIgnoreCase(state)) { - Notify.create(this, "Authentication Error!", Style.ERROR).show(); + Notify.create(this, "OAuth State Error!", Style.ERROR).show(); return; } mOAuthCode = uri.getQueryParameter("code"); - } else { - Log.d(Constants.TAG, "received oauth uri: null"); } } -- cgit v1.2.3