Search in sources :

Example 6 with FirebaseAuthUserCollisionException

use of com.google.firebase.auth.FirebaseAuthUserCollisionException in project FirebaseUI-Android by firebase.

the class EmailLinkSignInHandlerTest method testStartSignIn_linkingFlow_expectUserCollisionException.

@Test
@SuppressWarnings("all")
public void testStartSignIn_linkingFlow_expectUserCollisionException() {
    mHandler.getOperation().observeForever(mResponseObserver);
    when(mMockAuth.isSignInWithEmailLink(any(String.class))).thenReturn(true);
    mPersistenceManager.saveEmail(ApplicationProvider.getApplicationContext(), TestConstants.EMAIL, TestConstants.SESSION_ID, TestConstants.UID);
    mPersistenceManager.saveIdpResponseForLinking(ApplicationProvider.getApplicationContext(), buildFacebookIdpResponse());
    when(mMockAuth.signInWithCredential(any(AuthCredential.class))).thenReturn(AutoCompleteTask.forSuccess(mMockAuthResult));
    // Mock linking with Facebook to always work
    when(mMockAuthResult.getUser().linkWithCredential(any(FacebookAuthCredential.class))).thenReturn(AutoCompleteTask.<AuthResult>forFailure(new FirebaseAuthUserCollisionException("foo", "bar")));
    mHandler.startSignIn();
    ArgumentCaptor<Resource<IdpResponse>> captor = ArgumentCaptor.forClass(Resource.class);
    InOrder inOrder = inOrder(mResponseObserver);
    inOrder.verify(mResponseObserver).onChanged(argThat(ResourceMatchers.<IdpResponse>isLoading()));
    inOrder.verify(mResponseObserver).onChanged(captor.capture());
    assertThat(captor.getValue().getException()).isNotNull();
    FirebaseAuthUserCollisionException collisionException = (FirebaseAuthUserCollisionException) captor.getValue().getException();
    assertThat(mPersistenceManager.retrieveSessionRecord(ApplicationProvider.getApplicationContext())).isNull();
}
Also used : FacebookAuthCredential(com.google.firebase.auth.FacebookAuthCredential) EmailAuthCredential(com.google.firebase.auth.EmailAuthCredential) AuthCredential(com.google.firebase.auth.AuthCredential) InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FacebookAuthCredential(com.google.firebase.auth.FacebookAuthCredential) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 7 with FirebaseAuthUserCollisionException

use of com.google.firebase.auth.FirebaseAuthUserCollisionException in project FirebaseUI-Android by firebase.

the class GenericIdpSignInHandlerTest method testStartSignIn_normalSignInFlowWithRecoverableError_expectFailure.

@Test
public void testStartSignIn_normalSignInFlowWithRecoverableError_expectFailure() {
    AuthCredential credential = OAuthProvider.newCredentialBuilder(MICROSOFT_PROVIDER).setIdToken(ID_TOKEN).setAccessToken(ACCESS_TOKEN).build();
    FirebaseAuthUserCollisionException collisionException = new FirebaseAuthUserCollisionException("foo", "bar");
    collisionException.zzb(EMAIL).zza(credential);
    when(mMockAuth.startActivityForSignInWithProvider(any(Activity.class), any(OAuthProvider.class))).thenReturn(AutoCompleteTask.forFailure(collisionException));
    mockOAuthProvider(MICROSOFT_PROVIDER);
    mHandler.startSignIn(mMockAuth, mMockActivity, MICROSOFT_PROVIDER);
    ArgumentCaptor<OAuthProvider> providerCaptor = ArgumentCaptor.forClass(OAuthProvider.class);
    verify(mMockAuth).startActivityForSignInWithProvider(eq(mMockActivity), providerCaptor.capture());
    assertThat(providerCaptor.getValue().getProviderId()).isEqualTo(MICROSOFT_PROVIDER);
    InOrder inOrder = inOrder(mResponseObserver);
    inOrder.verify(mResponseObserver).onChanged(argThat(ResourceMatchers.isLoading()));
    ArgumentCaptor<Resource<IdpResponse>> resourceCaptor = ArgumentCaptor.forClass(Resource.class);
    inOrder.verify(mResponseObserver).onChanged(resourceCaptor.capture());
    FirebaseUiUserCollisionException e = (FirebaseUiUserCollisionException) resourceCaptor.getValue().getException();
    assertThat(e.getCredential()).isNotNull();
    assertThat(e.getEmail()).isEqualTo(EMAIL);
}
Also used : AuthCredential(com.google.firebase.auth.AuthCredential) InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) Activity(android.app.Activity) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) OAuthProvider(com.google.firebase.auth.OAuthProvider) FirebaseUiUserCollisionException(com.firebase.ui.auth.FirebaseUiUserCollisionException) Test(org.junit.Test)

Example 8 with FirebaseAuthUserCollisionException

use of com.google.firebase.auth.FirebaseAuthUserCollisionException in project FirebaseUI-Android by firebase.

the class GenericIdpSignInHandlerTest method testStartSignIn_anonymousUpgradeFlowWithConflict_expectRecoverableError.

@Test
public void testStartSignIn_anonymousUpgradeFlowWithConflict_expectRecoverableError() {
    setupAnonymousUpgrade();
    AuthCredential credential = OAuthProvider.newCredentialBuilder(MICROSOFT_PROVIDER).setIdToken(ID_TOKEN).setAccessToken(ACCESS_TOKEN).build();
    FirebaseAuthUserCollisionException collisionException = new FirebaseAuthUserCollisionException("foo", "bar");
    collisionException.zzb(EMAIL).zza(credential);
    when(mMockAuth.getCurrentUser().startActivityForLinkWithProvider(any(Activity.class), any(OAuthProvider.class))).thenReturn(AutoCompleteTask.forFailure(collisionException));
    // Case 1: Anon user signing in with an existing account
    when(mMockAuth.fetchSignInMethodsForEmail(any(String.class))).thenReturn(AutoCompleteTask.forSuccess(new FakeSignInMethodQueryResult(Arrays.asList(MICROSOFT_PROVIDER))));
    mockOAuthProvider(MICROSOFT_PROVIDER);
    mHandler.startSignIn(mMockAuth, mMockActivity, MICROSOFT_PROVIDER);
    ArgumentCaptor<OAuthProvider> providerCaptor = ArgumentCaptor.forClass(OAuthProvider.class);
    verify(mMockAuth.getCurrentUser()).startActivityForLinkWithProvider(eq(mMockActivity), providerCaptor.capture());
    assertThat(providerCaptor.getValue().getProviderId()).isEqualTo(MICROSOFT_PROVIDER);
    InOrder inOrder = inOrder(mResponseObserver);
    inOrder.verify(mResponseObserver).onChanged(argThat(ResourceMatchers.isLoading()));
    ArgumentCaptor<Resource<IdpResponse>> resolveCaptor = ArgumentCaptor.forClass(Resource.class);
    inOrder.verify(mResponseObserver).onChanged(resolveCaptor.capture());
    FirebaseAuthAnonymousUpgradeException e = (FirebaseAuthAnonymousUpgradeException) resolveCaptor.getValue().getException();
    assertThat(e.getResponse().getCredentialForLinking()).isNotNull();
}
Also used : AuthCredential(com.google.firebase.auth.AuthCredential) FakeSignInMethodQueryResult(com.firebase.ui.auth.testhelpers.FakeSignInMethodQueryResult) InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) Activity(android.app.Activity) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) OAuthProvider(com.google.firebase.auth.OAuthProvider) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) Test(org.junit.Test)

Example 9 with FirebaseAuthUserCollisionException

use of com.google.firebase.auth.FirebaseAuthUserCollisionException in project FirebaseUI-Android by firebase.

the class SocialProviderResponseHandlerTest method testSignInIdp_anonymousUserUpgradeEnabledAndExistingUserWithSameIdp_expectMergeFailure.

@Test
public void testSignInIdp_anonymousUserUpgradeEnabledAndExistingUserWithSameIdp_expectMergeFailure() {
    mHandler.getOperation().observeForever(mResultObserver);
    setupAnonymousUpgrade();
    when(mMockAuth.getCurrentUser().linkWithCredential(any(AuthCredential.class))).thenReturn(AutoCompleteTask.forFailure(new FirebaseAuthUserCollisionException("foo", "bar")));
    // Case 1: Anon user signing in with a Google credential that belongs to an existing user.
    when(mMockAuth.fetchSignInMethodsForEmail(any(String.class))).thenReturn(AutoCompleteTask.forSuccess(new FakeSignInMethodQueryResult(Arrays.asList(GoogleAuthProvider.GOOGLE_SIGN_IN_METHOD, FacebookAuthProvider.FACEBOOK_SIGN_IN_METHOD))));
    IdpResponse response = new IdpResponse.Builder(new User.Builder(GoogleAuthProvider.PROVIDER_ID, TestConstants.EMAIL).build()).setToken(TestConstants.TOKEN).build();
    mHandler.startSignIn(response);
    verify(mMockAuth.getCurrentUser()).linkWithCredential(any(AuthCredential.class));
    InOrder inOrder = inOrder(mResultObserver);
    inOrder.verify(mResultObserver).onChanged(argThat(ResourceMatchers.isLoading()));
    ArgumentCaptor<Resource<IdpResponse>> resolveCaptor = ArgumentCaptor.forClass(Resource.class);
    inOrder.verify(mResultObserver).onChanged(resolveCaptor.capture());
    FirebaseAuthAnonymousUpgradeException e = (FirebaseAuthAnonymousUpgradeException) resolveCaptor.getValue().getException();
    assertThat(e.getResponse().getCredentialForLinking()).isNotNull();
}
Also used : AuthCredential(com.google.firebase.auth.AuthCredential) FakeSignInMethodQueryResult(com.firebase.ui.auth.testhelpers.FakeSignInMethodQueryResult) InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 10 with FirebaseAuthUserCollisionException

use of com.google.firebase.auth.FirebaseAuthUserCollisionException in project FirebaseUI-Android by firebase.

the class GenericIdpSignInHandler method handleAnonymousUpgradeFlow.

private void handleAnonymousUpgradeFlow(final FirebaseAuth auth, final HelperActivityBase activity, final OAuthProvider provider, final FlowParameters flowParameters) {
    final boolean useEmulator = activity.getAuthUI().isUseEmulator();
    auth.getCurrentUser().startActivityForLinkWithProvider(activity, provider).addOnSuccessListener(authResult -> handleSuccess(useEmulator, provider.getProviderId(), authResult.getUser(), (OAuthCredential) authResult.getCredential(), authResult.getAdditionalUserInfo().isNewUser())).addOnFailureListener(e -> {
        if (!(e instanceof FirebaseAuthUserCollisionException)) {
            setResult(Resource.forFailure(e));
            return;
        }
        FirebaseAuthUserCollisionException collisionException = (FirebaseAuthUserCollisionException) e;
        final AuthCredential credential = collisionException.getUpdatedCredential();
        final String email = collisionException.getEmail();
        // Case 1: Anonymous user trying to link with an existing user
        // Case 2: Anonymous user trying to link with a provider keyed
        // by an email that already belongs to an existing account
        // (linking flow)
        ProviderUtils.fetchSortedProviders(auth, flowParameters, email).addOnSuccessListener(providers -> {
            if (providers.isEmpty()) {
                String errorMessage = "Unable to complete the linkingflow -" + " the user is using " + "unsupported providers.";
                setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.DEVELOPER_ERROR, errorMessage)));
                return;
            }
            if (providers.contains(provider.getProviderId())) {
                // Case 1
                handleMergeFailure(credential);
            } else {
                // Case 2 - linking flow to be handled by
                // SocialProviderResponseHandler
                setResult(Resource.forFailure(new FirebaseUiUserCollisionException(ErrorCodes.ERROR_GENERIC_IDP_RECOVERABLE_ERROR, "Recoverable error.", provider.getProviderId(), email, credential)));
            }
        });
    });
}
Also used : ProviderUtils(com.firebase.ui.auth.util.data.ProviderUtils) NonNull(androidx.annotation.NonNull) User(com.firebase.ui.auth.data.model.User) Intent(android.content.Intent) Resource(com.firebase.ui.auth.data.model.Resource) ExtraConstants(com.firebase.ui.auth.util.ExtraConstants) HashMap(java.util.HashMap) FirebaseUiUserCollisionException(com.firebase.ui.auth.FirebaseUiUserCollisionException) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) GoogleAuthProvider(com.google.firebase.auth.GoogleAuthProvider) OAuthCredential(com.google.firebase.auth.OAuthCredential) RestrictTo(androidx.annotation.RestrictTo) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) AuthUI(com.firebase.ui.auth.AuthUI) R(com.firebase.ui.auth.R) FirebaseAuth(com.google.firebase.auth.FirebaseAuth) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) UserCancellationException(com.firebase.ui.auth.data.model.UserCancellationException) HelperActivityBase(com.firebase.ui.auth.ui.HelperActivityBase) FirebaseUser(com.google.firebase.auth.FirebaseUser) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) FacebookAuthProvider(com.google.firebase.auth.FacebookAuthProvider) FirebaseAuthError(com.firebase.ui.auth.util.FirebaseAuthError) OAuthProvider(com.google.firebase.auth.OAuthProvider) AuthCredential(com.google.firebase.auth.AuthCredential) FlowParameters(com.firebase.ui.auth.data.model.FlowParameters) List(java.util.List) Nullable(androidx.annotation.Nullable) Application(android.app.Application) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) FirebaseAuthException(com.google.firebase.auth.FirebaseAuthException) ProviderSignInBase(com.firebase.ui.auth.viewmodel.ProviderSignInBase) RequestCodes(com.firebase.ui.auth.viewmodel.RequestCodes) AuthResult(com.google.firebase.auth.AuthResult) ErrorCodes(com.firebase.ui.auth.ErrorCodes) VisibleForTesting(androidx.annotation.VisibleForTesting) IdpResponse(com.firebase.ui.auth.IdpResponse) OAuthCredential(com.google.firebase.auth.OAuthCredential) AuthCredential(com.google.firebase.auth.AuthCredential) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) FirebaseUiUserCollisionException(com.firebase.ui.auth.FirebaseUiUserCollisionException)

Aggregations

FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)22 IdpResponse (com.firebase.ui.auth.IdpResponse)15 Resource (com.firebase.ui.auth.data.model.Resource)15 AuthCredential (com.google.firebase.auth.AuthCredential)14 Test (org.junit.Test)12 FirebaseUser (com.google.firebase.auth.FirebaseUser)10 InOrder (org.mockito.InOrder)10 AuthResult (com.google.firebase.auth.AuthResult)9 Intent (android.content.Intent)7 User (com.firebase.ui.auth.data.model.User)7 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)7 FirebaseAuthAnonymousUpgradeException (com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException)6 FakeSignInMethodQueryResult (com.firebase.ui.auth.testhelpers.FakeSignInMethodQueryResult)6 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)6 Application (android.app.Application)5 NonNull (androidx.annotation.NonNull)5 Nullable (androidx.annotation.Nullable)5 ErrorCodes (com.firebase.ui.auth.ErrorCodes)5 FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)5 IntentRequiredException (com.firebase.ui.auth.data.model.IntentRequiredException)5