|
| 1 | +package com.firebase.ui.auth.data.remote; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.app.Application; |
| 5 | +import android.content.Intent; |
| 6 | +import android.support.annotation.NonNull; |
| 7 | +import android.support.annotation.Nullable; |
| 8 | +import android.text.TextUtils; |
| 9 | + |
| 10 | +import com.firebase.ui.auth.AuthUI; |
| 11 | +import com.firebase.ui.auth.IdpResponse; |
| 12 | +import com.firebase.ui.auth.data.model.IntentRequiredException; |
| 13 | +import com.firebase.ui.auth.data.model.PendingIntentRequiredException; |
| 14 | +import com.firebase.ui.auth.data.model.Resource; |
| 15 | +import com.firebase.ui.auth.data.model.User; |
| 16 | +import com.firebase.ui.auth.data.model.UserCancellationException; |
| 17 | +import com.firebase.ui.auth.ui.email.EmailActivity; |
| 18 | +import com.firebase.ui.auth.ui.idp.AuthMethodPickerActivity; |
| 19 | +import com.firebase.ui.auth.ui.idp.SingleSignInActivity; |
| 20 | +import com.firebase.ui.auth.ui.phone.PhoneActivity; |
| 21 | +import com.firebase.ui.auth.util.GoogleApiUtils; |
| 22 | +import com.firebase.ui.auth.util.data.ProviderUtils; |
| 23 | +import com.firebase.ui.auth.viewmodel.AuthViewModelBase; |
| 24 | +import com.firebase.ui.auth.viewmodel.RequestCodes; |
| 25 | +import com.google.android.gms.auth.api.credentials.Credential; |
| 26 | +import com.google.android.gms.auth.api.credentials.CredentialRequest; |
| 27 | +import com.google.android.gms.auth.api.credentials.CredentialRequestResponse; |
| 28 | +import com.google.android.gms.common.api.ApiException; |
| 29 | +import com.google.android.gms.common.api.CommonStatusCodes; |
| 30 | +import com.google.android.gms.common.api.ResolvableApiException; |
| 31 | +import com.google.android.gms.tasks.OnCompleteListener; |
| 32 | +import com.google.android.gms.tasks.OnFailureListener; |
| 33 | +import com.google.android.gms.tasks.OnSuccessListener; |
| 34 | +import com.google.android.gms.tasks.Task; |
| 35 | +import com.google.firebase.auth.AuthResult; |
| 36 | +import com.google.firebase.auth.EmailAuthProvider; |
| 37 | +import com.google.firebase.auth.FacebookAuthProvider; |
| 38 | +import com.google.firebase.auth.FirebaseAuthInvalidCredentialsException; |
| 39 | +import com.google.firebase.auth.FirebaseAuthInvalidUserException; |
| 40 | +import com.google.firebase.auth.GoogleAuthProvider; |
| 41 | +import com.google.firebase.auth.PhoneAuthProvider; |
| 42 | +import com.google.firebase.auth.TwitterAuthProvider; |
| 43 | + |
| 44 | +import java.util.ArrayList; |
| 45 | +import java.util.List; |
| 46 | + |
| 47 | +public class SignInKickstarter extends AuthViewModelBase<IdpResponse> { |
| 48 | + public SignInKickstarter(Application application) { |
| 49 | + super(application); |
| 50 | + } |
| 51 | + |
| 52 | + public void start() { |
| 53 | + // Only support password credentials if email auth is enabled |
| 54 | + boolean supportPasswords = ProviderUtils.getConfigFromIdps( |
| 55 | + getArguments().providerInfo, EmailAuthProvider.PROVIDER_ID) != null; |
| 56 | + List<String> accountTypes = getCredentialAccountTypes(); |
| 57 | + |
| 58 | + // If the request will be empty, avoid the step entirely |
| 59 | + boolean willRequestCredentials = supportPasswords || accountTypes.size() > 0; |
| 60 | + |
| 61 | + if (getArguments().enableCredentials && willRequestCredentials) { |
| 62 | + setResult(Resource.<IdpResponse>forLoading()); |
| 63 | + |
| 64 | + GoogleApiUtils.getCredentialsClient(getApplication()) |
| 65 | + .request(new CredentialRequest.Builder() |
| 66 | + .setPasswordLoginSupported(supportPasswords) |
| 67 | + .setAccountTypes(accountTypes.toArray(new String[accountTypes.size()])) |
| 68 | + .build()) |
| 69 | + .addOnCompleteListener(new OnCompleteListener<CredentialRequestResponse>() { |
| 70 | + @Override |
| 71 | + public void onComplete(@NonNull Task<CredentialRequestResponse> task) { |
| 72 | + try { |
| 73 | + handleCredential( |
| 74 | + task.getResult(ApiException.class).getCredential()); |
| 75 | + } catch (ResolvableApiException e) { |
| 76 | + if (e.getStatusCode() == CommonStatusCodes.RESOLUTION_REQUIRED) { |
| 77 | + setResult(Resource.<IdpResponse>forUsableFailure( |
| 78 | + new PendingIntentRequiredException( |
| 79 | + e.getResolution(), RequestCodes.CRED_HINT))); |
| 80 | + } else { |
| 81 | + startAuthMethodChoice(); |
| 82 | + } |
| 83 | + } catch (ApiException e) { |
| 84 | + startAuthMethodChoice(); |
| 85 | + } |
| 86 | + } |
| 87 | + }); |
| 88 | + } else { |
| 89 | + startAuthMethodChoice(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private void startAuthMethodChoice() { |
| 94 | + List<AuthUI.IdpConfig> idpConfigs = getArguments().providerInfo; |
| 95 | + |
| 96 | + // If there is only one provider selected, launch the flow directly |
| 97 | + if (idpConfigs.size() == 1) { |
| 98 | + AuthUI.IdpConfig firstIdpConfig = idpConfigs.get(0); |
| 99 | + String firstProvider = firstIdpConfig.getProviderId(); |
| 100 | + switch (firstProvider) { |
| 101 | + case EmailAuthProvider.PROVIDER_ID: |
| 102 | + setResult(Resource.<IdpResponse>forUsableFailure(new IntentRequiredException( |
| 103 | + EmailActivity.createIntent(getApplication(), getArguments()), |
| 104 | + RequestCodes.EMAIL_FLOW))); |
| 105 | + break; |
| 106 | + case PhoneAuthProvider.PROVIDER_ID: |
| 107 | + setResult(Resource.<IdpResponse>forUsableFailure(new IntentRequiredException( |
| 108 | + PhoneActivity.createIntent( |
| 109 | + getApplication(), getArguments(), firstIdpConfig.getParams()), |
| 110 | + RequestCodes.PHONE_FLOW))); |
| 111 | + break; |
| 112 | + default: |
| 113 | + redirectSignIn(firstProvider, null); |
| 114 | + break; |
| 115 | + } |
| 116 | + } else { |
| 117 | + setResult(Resource.<IdpResponse>forUsableFailure(new IntentRequiredException( |
| 118 | + AuthMethodPickerActivity.createIntent(getApplication(), getArguments()), |
| 119 | + RequestCodes.AUTH_PICKER_FLOW))); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private void redirectSignIn(String provider, String email) { |
| 124 | + switch (provider) { |
| 125 | + case EmailAuthProvider.PROVIDER_ID: |
| 126 | + setResult(Resource.<IdpResponse>forUsableFailure(new IntentRequiredException( |
| 127 | + EmailActivity.createIntent(getApplication(), getArguments(), email), |
| 128 | + RequestCodes.EMAIL_FLOW))); |
| 129 | + break; |
| 130 | + case GoogleAuthProvider.PROVIDER_ID: |
| 131 | + case FacebookAuthProvider.PROVIDER_ID: |
| 132 | + case TwitterAuthProvider.PROVIDER_ID: |
| 133 | + setResult(Resource.<IdpResponse>forUsableFailure(new IntentRequiredException( |
| 134 | + SingleSignInActivity.createIntent( |
| 135 | + getApplication(), |
| 136 | + getArguments(), |
| 137 | + new User.Builder(provider, email).build()), |
| 138 | + RequestCodes.PROVIDER_FLOW))); |
| 139 | + break; |
| 140 | + default: |
| 141 | + startAuthMethodChoice(); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private List<String> getCredentialAccountTypes() { |
| 146 | + List<String> accounts = new ArrayList<>(); |
| 147 | + for (AuthUI.IdpConfig idpConfig : getArguments().providerInfo) { |
| 148 | + @AuthUI.SupportedProvider String providerId = idpConfig.getProviderId(); |
| 149 | + if (providerId.equals(GoogleAuthProvider.PROVIDER_ID)) { |
| 150 | + accounts.add(ProviderUtils.providerIdToAccountType(providerId)); |
| 151 | + } |
| 152 | + } |
| 153 | + return accounts; |
| 154 | + } |
| 155 | + |
| 156 | + public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { |
| 157 | + switch (requestCode) { |
| 158 | + case RequestCodes.CRED_HINT: |
| 159 | + if (resultCode == Activity.RESULT_OK) { |
| 160 | + handleCredential((Credential) data.getParcelableExtra(Credential.EXTRA_KEY)); |
| 161 | + } else { |
| 162 | + startAuthMethodChoice(); |
| 163 | + } |
| 164 | + break; |
| 165 | + case RequestCodes.AUTH_PICKER_FLOW: |
| 166 | + case RequestCodes.EMAIL_FLOW: |
| 167 | + case RequestCodes.PHONE_FLOW: |
| 168 | + case RequestCodes.PROVIDER_FLOW: |
| 169 | + IdpResponse response = IdpResponse.fromResultIntent(data); |
| 170 | + if (response == null) { |
| 171 | + setResult(Resource.<IdpResponse>forFailure(new UserCancellationException())); |
| 172 | + } else if (response.isSuccessful()) { |
| 173 | + setResult(Resource.forSuccess(response)); |
| 174 | + } else { |
| 175 | + setResult(Resource.<IdpResponse>forFailure(response.getError())); |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private void handleCredential(final Credential credential) { |
| 181 | + String id = credential.getId(); |
| 182 | + String password = credential.getPassword(); |
| 183 | + if (TextUtils.isEmpty(password)) { |
| 184 | + String identity = credential.getAccountType(); |
| 185 | + if (identity == null) { |
| 186 | + startAuthMethodChoice(); |
| 187 | + } else { |
| 188 | + redirectSignIn( |
| 189 | + ProviderUtils.accountTypeToProviderId(credential.getAccountType()), id); |
| 190 | + } |
| 191 | + } else { |
| 192 | + final IdpResponse response = new IdpResponse.Builder( |
| 193 | + new User.Builder(EmailAuthProvider.PROVIDER_ID, id).build()).build(); |
| 194 | + |
| 195 | + getAuth().signInWithEmailAndPassword(id, password) |
| 196 | + .addOnSuccessListener(new OnSuccessListener<AuthResult>() { |
| 197 | + @Override |
| 198 | + public void onSuccess(AuthResult result) { |
| 199 | + setResult(Resource.forSuccess(response)); |
| 200 | + } |
| 201 | + }) |
| 202 | + .addOnFailureListener(new OnFailureListener() { |
| 203 | + @Override |
| 204 | + public void onFailure(@NonNull Exception e) { |
| 205 | + if (e instanceof FirebaseAuthInvalidUserException |
| 206 | + || e instanceof FirebaseAuthInvalidCredentialsException) { |
| 207 | + // In this case the credential saved in SmartLock was not |
| 208 | + // a valid credential, we should delete it from SmartLock |
| 209 | + // before continuing. |
| 210 | + GoogleApiUtils.getCredentialsClient(getApplication()) |
| 211 | + .delete(credential); |
| 212 | + } |
| 213 | + startAuthMethodChoice(); |
| 214 | + } |
| 215 | + }); |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments