From e2aa484837fa62416db57d48ffbf7f81e3103d55 Mon Sep 17 00:00:00 2001 From: Aaron Mandle Date: Mon, 6 Feb 2017 18:41:51 -0800 Subject: [PATCH 01/17] reauthentication wip --- .../firebase/uidemo/auth/AuthUiActivity.java | 27 ++- .../uidemo/auth/SignedInActivity.java | 102 ++++++++++- app/src/main/res/layout/signed_in_layout.xml | 9 + app/src/main/res/values/strings.xml | 5 +- .../java/com/firebase/ui/auth/AuthUI.java | 165 +++++++++++++----- .../firebase/ui/auth/ui/FlowParameters.java | 29 ++- .../WelcomeBackPasswordPrompt.java | 10 +- .../auth/ui/idp/AuthMethodPickerActivity.java | 75 +++++++- .../layout-land/auth_method_picker_layout.xml | 40 +++-- .../res/layout/auth_method_picker_layout.xml | 31 ++-- auth/src/main/res/values/strings.xml | 4 + auth/src/main/res/values/styles.xml | 10 +- .../ui/auth/testhelpers/TestHelper.java | 9 +- 13 files changed, 428 insertions(+), 88 deletions(-) diff --git a/app/src/main/java/com/firebase/uidemo/auth/AuthUiActivity.java b/app/src/main/java/com/firebase/uidemo/auth/AuthUiActivity.java index 04a4ec262..edd0cfd90 100644 --- a/app/src/main/java/com/firebase/uidemo/auth/AuthUiActivity.java +++ b/app/src/main/java/com/firebase/uidemo/auth/AuthUiActivity.java @@ -126,16 +126,24 @@ public class AuthUiActivity extends AppCompatActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + setContentView(R.layout.auth_ui_layout); + ButterKnife.bind(this); FirebaseAuth auth = FirebaseAuth.getInstance(); if (auth.getCurrentUser() != null) { - startActivity(SignedInActivity.createIntent(this, null)); + startActivity( + SignedInActivity.createIntent( + this, + null, + new SignedInActivity.SignedInConfig( + getSelectedLogo(), + getSelectedTheme(), + getSelectedProviders(), + getSelectedTosUrl(), + mEnableSmartLock.isEnabled()))); finish(); } - setContentView(R.layout.auth_ui_layout); - ButterKnife.bind(this); - if (!isGoogleConfigured()) { mUseGoogleProvider.setChecked(false); mUseGoogleProvider.setEnabled(false); @@ -208,7 +216,16 @@ private void handleSignInResponse(int resultCode, Intent data) { // Successfully signed in if (resultCode == ResultCodes.OK) { - startActivity(SignedInActivity.createIntent(this, response)); + startActivity( + SignedInActivity.createIntent( + this, + response, + new SignedInActivity.SignedInConfig( + getSelectedLogo(), + getSelectedTheme(), + getSelectedProviders(), + getSelectedTosUrl(), + mEnableSmartLock.isEnabled()))); finish(); return; } else { diff --git a/app/src/main/java/com/firebase/uidemo/auth/SignedInActivity.java b/app/src/main/java/com/firebase/uidemo/auth/SignedInActivity.java index 4642ff034..fb973caa5 100644 --- a/app/src/main/java/com/firebase/uidemo/auth/SignedInActivity.java +++ b/app/src/main/java/com/firebase/uidemo/auth/SignedInActivity.java @@ -18,6 +18,8 @@ import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; +import android.os.Parcel; +import android.os.Parcelable; import android.support.annotation.MainThread; import android.support.annotation.NonNull; import android.support.annotation.StringRes; @@ -31,6 +33,7 @@ import com.bumptech.glide.Glide; import com.firebase.ui.auth.AuthUI; +import com.firebase.ui.auth.AuthUI.IdpConfig; import com.firebase.ui.auth.IdpResponse; import com.firebase.uidemo.R; import com.google.android.gms.tasks.OnCompleteListener; @@ -41,13 +44,19 @@ import com.google.firebase.auth.FirebaseUser; import com.google.firebase.auth.GoogleAuthProvider; +import java.util.ArrayList; import java.util.Iterator; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; +import java.util.List; public class SignedInActivity extends AppCompatActivity { + private static final String EXTRA_SIGNED_IN_CONFIG = "extra_signed_in_config"; + + private static final int RC_REAUTH = 100; + @BindView(android.R.id.content) View mRootView; @@ -65,6 +74,8 @@ public class SignedInActivity extends AppCompatActivity { private IdpResponse mIdpResponse; + private SignedInConfig mSignedInConfig; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -77,6 +88,7 @@ public void onCreate(Bundle savedInstanceState) { } mIdpResponse = IdpResponse.fromResultIntent(getIntent()); + mSignedInConfig = getIntent().getParcelableExtra(EXTRA_SIGNED_IN_CONFIG); setContentView(R.layout.signed_in_layout); ButterKnife.bind(this); @@ -101,6 +113,24 @@ public void onComplete(@NonNull Task task) { }); } + @OnClick(R.id.reauthenticate) + public void reauthenticate() { + FirebaseAuth auth = FirebaseAuth.getInstance(); + + Intent reauthIntent = AuthUI.getInstance() + .createReauthIntentBuilder() + .setProviders(mSignedInConfig.providerInfo) + .setIsSmartLockEnabled(mSignedInConfig.isSmartLockEnabled) + .setLogo(mSignedInConfig.logo) + .setTheme(mSignedInConfig.theme) + .setTosUrl(mSignedInConfig.tosUrl) + .setReauthEmail(auth.getCurrentUser().getEmail()) + .setReauthReason(getString(R.string.given_reauthentication_reason)) + .build(); + + startActivityForResult(reauthIntent, RC_REAUTH); + } + @OnClick(R.id.delete_account) public void deleteAccountClicked() { @@ -203,9 +233,79 @@ private void showSnackbar(@StringRes int errorMessageRes) { .show(); } - public static Intent createIntent(Context context, IdpResponse idpResponse) { + static class SignedInConfig implements Parcelable { + int logo; + int theme; + List providerInfo; + String tosUrl; + boolean isSmartLockEnabled; + + SignedInConfig( + int logo, + int theme, + List providerInfo, + String tosUrl, + boolean isSmartLockEnabled) { + this.logo = logo; + this.theme = theme; + this.providerInfo = providerInfo; + this.tosUrl = tosUrl; + this.isSmartLockEnabled = isSmartLockEnabled; + } + + SignedInConfig(Parcel in) { + logo = in.readInt(); + theme = in.readInt(); + providerInfo = new ArrayList<>(); + in.readList(providerInfo, IdpConfig.class.getClassLoader()); + tosUrl = in.readString(); + isSmartLockEnabled = in.readInt() != 0; + } + + public static final Creator CREATOR = new Creator() { + @Override + public SignedInConfig createFromParcel(Parcel in) { + return new SignedInConfig(in); + } + + @Override + public SignedInConfig[] newArray(int size) { + return new SignedInConfig[size]; + } + }; + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeInt(logo); + dest.writeInt(theme); + dest.writeList(providerInfo); + dest.writeString(tosUrl); + dest.writeInt(isSmartLockEnabled ? 1 : 0); + } + } + + public static Intent createIntent( + Context context, + IdpResponse idpResponse, + SignedInConfig signedInConfig) { Intent in = IdpResponse.getIntent(idpResponse); in.setClass(context, SignedInActivity.class); + in.putExtra(EXTRA_SIGNED_IN_CONFIG, signedInConfig); return in; } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (requestCode == RC_REAUTH) { + mIdpResponse = IdpResponse.fromResultIntent(data); + populateIdpToken(); + populateProfile(); + } + } } diff --git a/app/src/main/res/layout/signed_in_layout.xml b/app/src/main/res/layout/signed_in_layout.xml index 908966256..fe667dcaf 100644 --- a/app/src/main/res/layout/signed_in_layout.xml +++ b/app/src/main/res/layout/signed_in_layout.xml @@ -40,6 +40,15 @@ android:layout_margin="16dp" android:text="@string/sign_out"/> +