linked: redesign github resource creation, implement ouath flow

This commit is contained in:
Vincent Breitmoser
2015-09-01 13:58:04 +02:00
parent 47ef8cbe55
commit 6225c940f9
3 changed files with 105 additions and 42 deletions

View File

@@ -26,12 +26,13 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.net.URI;
import java.net.URL; import java.net.URL;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.Fragment; import android.support.annotation.Nullable;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
@@ -44,20 +45,34 @@ import org.json.JSONObject;
import org.sufficientlysecure.keychain.BuildConfig; import org.sufficientlysecure.keychain.BuildConfig;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R; 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.ui.widget.StatusIndicator;
import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Log;
public class LinkedIdCreateGithubFragment extends Fragment { public class LinkedIdCreateGithubFragment extends CryptoOperationFragment<SaveKeyringParcel,EditKeyResult> {
ViewAnimator mButtonContainer; ViewAnimator mButtonContainer;
StatusIndicator mStatus1, mStatus2, mStatus3; StatusIndicator mStatus1, mStatus2, mStatus3;
byte[] mFingerprint;
long mMasterKeyId;
private SaveKeyringParcel mSaveKeyringParcel;
public static LinkedIdCreateGithubFragment newInstance() { public static LinkedIdCreateGithubFragment newInstance() {
return new LinkedIdCreateGithubFragment(); return new LinkedIdCreateGithubFragment();
} }
public LinkedIdCreateGithubFragment() {
super(null);
}
@Override @NonNull @Override @NonNull
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.linked_create_github_fragment, container, false); View view = inflater.inflate(R.layout.linked_create_github_fragment, container, false);
@@ -83,6 +98,9 @@ public class LinkedIdCreateGithubFragment extends Fragment {
super.onResume(); super.onResume();
LinkedIdWizard wizard = (LinkedIdWizard) getActivity(); LinkedIdWizard wizard = (LinkedIdWizard) getActivity();
mFingerprint = wizard.mFingerprint;
mMasterKeyId = wizard.mMasterKeyId;
final String oAuthCode = wizard.oAuthGetCode(); final String oAuthCode = wizard.oAuthGetCode();
final String oAuthState = wizard.oAuthGetState(); final String oAuthState = wizard.oAuthGetState();
if (oAuthCode == null) { if (oAuthCode == null) {
@@ -90,6 +108,8 @@ public class LinkedIdCreateGithubFragment extends Fragment {
return; return;
} }
final String gistText = GithubResource.generate(wizard, mFingerprint);
Log.d(Constants.TAG, "got code: " + oAuthCode); Log.d(Constants.TAG, "got code: " + oAuthCode);
new AsyncTask<Void,Void,JSONObject>() { new AsyncTask<Void,Void,JSONObject>() {
@@ -137,7 +157,7 @@ public class LinkedIdCreateGithubFragment extends Fragment {
} }
mStatus1.setDisplayedChild(2); mStatus1.setDisplayedChild(2);
step2PostGist(result.optString("access_token")); step2PostGist(result.optString("access_token"), gistText);
} }
}.execute(); }.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); mStatus2.setDisplayedChild(1);
@@ -189,7 +209,7 @@ public class LinkedIdCreateGithubFragment extends Fragment {
long timer = System.currentTimeMillis(); long timer = System.currentTimeMillis();
JSONObject file = new JSONObject(); JSONObject file = new JSONObject();
file.put("content", "hello!"); file.put("content", gistText);
JSONObject files = new JSONObject(); JSONObject files = new JSONObject();
files.put("file1.txt", file); files.put("file1.txt", file);
@@ -230,12 +250,57 @@ public class LinkedIdCreateGithubFragment extends Fragment {
return; 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(); }.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) private static JSONObject jsonHttpRequest(String url, JSONObject params, String accessToken)
throws IOException { throws IOException {

View File

@@ -138,16 +138,14 @@ public class LinkedIdWizard extends BaseActivity {
super.onNewIntent(intent); super.onNewIntent(intent);
Uri uri = intent.getData(); 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"); String state = uri.getQueryParameter("state");
if (!mOAuthState.equalsIgnoreCase(state)) { if (!mOAuthState.equalsIgnoreCase(state)) {
Notify.create(this, "Authentication Error!", Style.ERROR).show(); Notify.create(this, "OAuth State Error!", Style.ERROR).show();
return; return;
} }
mOAuthCode = uri.getQueryParameter("code"); mOAuthCode = uri.getQueryParameter("code");
} else {
Log.d(Constants.TAG, "received oauth uri: null");
} }
} }

View File

@@ -9,37 +9,6 @@
android:paddingRight="16dp" android:paddingRight="16dp"
android:paddingTop="16dp"> android:paddingTop="16dp">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
card_view:cardBackgroundColor="?attr/colorCardViewBackground"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="2dp"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
style="@style/CardViewHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/section_user_ids" />
<org.sufficientlysecure.keychain.ui.widget.FixedListView
android:id="@+id/view_key_user_ids"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView <android.support.v7.widget.CardView
android:id="@+id/card_linked_ids" android:id="@+id/card_linked_ids"
android:transitionName="card_linked_ids" android:transitionName="card_linked_ids"
@@ -94,6 +63,37 @@
</android.support.v7.widget.CardView> </android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
card_view:cardBackgroundColor="?attr/colorCardViewBackground"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="2dp"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
style="@style/CardViewHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/section_user_ids" />
<org.sufficientlysecure.keychain.ui.widget.FixedListView
android:id="@+id/view_key_user_ids"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView <android.support.v7.widget.CardView
android:id="@+id/linked_system_contact_card" android:id="@+id/linked_system_contact_card"
android:layout_width="match_parent" android:layout_width="match_parent"