Search in sources :

Example 1 with AuthOperationManager

use of com.firebase.ui.auth.util.data.AuthOperationManager in project FirebaseUI-Android by firebase.

the class EmailLinkSignInHandlerTest method testStartSignIn_linkingFlowWithAnonymousUpgradeEnabled_failedSignInPropagated.

@Test
@SuppressWarnings("all")
public void testStartSignIn_linkingFlowWithAnonymousUpgradeEnabled_failedSignInPropagated() {
    mHandler.getOperation().observeForever(mResponseObserver);
    setupAnonymousUpgrade();
    when(mMockAuth.isSignInWithEmailLink(any(String.class))).thenReturn(true);
    mPersistenceManager.saveEmail(ApplicationProvider.getApplicationContext(), TestConstants.EMAIL, TestConstants.SESSION_ID, TestConstants.UID);
    mPersistenceManager.saveIdpResponseForLinking(ApplicationProvider.getApplicationContext(), buildFacebookIdpResponse());
    // Need to control FirebaseAuth's return values
    AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
    authOperationManager.mScratchAuth = mScratchMockAuth;
    when(mScratchMockAuth.signInWithCredential(any(AuthCredential.class))).thenReturn(AutoContinueTask.<AuthResult>forFailure(new Exception("FAILED")));
    mHandler.startSignIn();
    // Verify sign in was called
    verify(mScratchMockAuth).signInWithCredential(any(AuthCredential.class));
    // Validate that the data was cleared
    assertThat(mPersistenceManager.retrieveSessionRecord(ApplicationProvider.getApplicationContext())).isNull();
    // Validate failure
    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();
}
Also used : FacebookAuthCredential(com.google.firebase.auth.FacebookAuthCredential) EmailAuthCredential(com.google.firebase.auth.EmailAuthCredential) AuthCredential(com.google.firebase.auth.AuthCredential) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 2 with AuthOperationManager

use of com.firebase.ui.auth.util.data.AuthOperationManager in project FirebaseUI-Android by firebase.

the class EmailLinkSignInHandlerTest method testStartSignIn_linkingFlowWithAnonymousUpgradeEnabled_expectMergeFailure.

@Test
@SuppressWarnings("all")
public void testStartSignIn_linkingFlowWithAnonymousUpgradeEnabled_expectMergeFailure() {
    mHandler.getOperation().observeForever(mResponseObserver);
    setupAnonymousUpgrade();
    when(mMockAuth.isSignInWithEmailLink(any(String.class))).thenReturn(true);
    mPersistenceManager.saveEmail(ApplicationProvider.getApplicationContext(), TestConstants.EMAIL, TestConstants.SESSION_ID, TestConstants.UID);
    mPersistenceManager.saveIdpResponseForLinking(ApplicationProvider.getApplicationContext(), buildFacebookIdpResponse());
    // Need to control FirebaseAuth's return values
    AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
    authOperationManager.mScratchAuth = mScratchMockAuth;
    when(mScratchMockAuth.signInWithCredential(any(AuthCredential.class))).thenReturn(AutoCompleteTask.forSuccess(mMockAuthResult));
    // Mock linking with Facebook to always work
    when(mMockAuthResult.getUser().linkWithCredential(any(AuthCredential.class))).thenReturn(new AutoContinueTask<>(mMockAuthResult, mMockAuthResult, true, null));
    mHandler.startSignIn();
    // Validate regular sign in
    ArgumentCaptor<EmailAuthCredential> credentialCaptor = ArgumentCaptor.forClass(EmailAuthCredential.class);
    verify(mScratchMockAuth).signInWithCredential(credentialCaptor.capture());
    // TODO: EmailAuthCredential no longer exposes .getEmail() or .getPassword()
    // assertThat(credentialCaptor.getValue().getEmail()).isEqualTo(TestConstants.EMAIL);
    assertThat(credentialCaptor.getValue().getSignInMethod()).isEqualTo(EmailAuthProvider.EMAIL_LINK_SIGN_IN_METHOD);
    // Validate linking was called
    verify(mMockAuthResult.getUser()).linkWithCredential(any(FacebookAuthCredential.class));
    // Validate that the data was cleared
    assertThat(mPersistenceManager.retrieveSessionRecord(ApplicationProvider.getApplicationContext())).isNull();
    // Validate IdpResponse
    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());
    FirebaseAuthAnonymousUpgradeException mergeException = ((FirebaseAuthAnonymousUpgradeException) captor.getValue().getException());
    IdpResponse response = mergeException.getResponse();
    assertThat(response.getCredentialForLinking()).isNotNull();
    assertThat(response.getCredentialForLinking().getProvider()).isEqualTo(FacebookAuthProvider.PROVIDER_ID);
}
Also used : FacebookAuthCredential(com.google.firebase.auth.FacebookAuthCredential) EmailAuthCredential(com.google.firebase.auth.EmailAuthCredential) AuthCredential(com.google.firebase.auth.AuthCredential) EmailAuthCredential(com.google.firebase.auth.EmailAuthCredential) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) FacebookAuthCredential(com.google.firebase.auth.FacebookAuthCredential) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 3 with AuthOperationManager

use of com.firebase.ui.auth.util.data.AuthOperationManager in project FirebaseUI-Android by firebase.

the class WelcomeBackPasswordHandlerTest method testSignIn_anonymousUserUpgradeEnabledWithSocialFlow_expectSafeLinksIdpCredentialAndMergeFailure.

@Test
public void testSignIn_anonymousUserUpgradeEnabledWithSocialFlow_expectSafeLinksIdpCredentialAndMergeFailure() {
    setupAnonymousUpgrade();
    mHandler.getOperation().observeForever(mResponseObserver);
    // Fake social response from Facebook
    User user = new User.Builder(FacebookAuthProvider.PROVIDER_ID, TestConstants.EMAIL).build();
    IdpResponse response = new IdpResponse.Builder(user).setToken(TestConstants.TOKEN).setSecret(TestConstants.SECRET).build();
    AuthCredential credential = FacebookAuthProvider.getCredential(TestConstants.TOKEN);
    // Need to control FirebaseAuth's return values
    AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
    authOperationManager.mScratchAuth = mScratchMockAuth;
    when(mScratchMockAuth.signInWithCredential(any(AuthCredential.class))).thenReturn(AutoCompleteTask.forSuccess(FakeAuthResult.INSTANCE));
    // Mock linking with Facebook to always work
    when(FakeAuthResult.INSTANCE.getUser().linkWithCredential(credential)).thenReturn(new AutoContinueTask<>(FakeAuthResult.INSTANCE, FakeAuthResult.INSTANCE, true, null));
    // Kick off the sign in flow
    mHandler.startSignIn(TestConstants.EMAIL, TestConstants.PASSWORD, response, credential);
    // Verify that signInWithCredential was called
    ArgumentCaptor<EmailAuthCredential> credentialCaptor = ArgumentCaptor.forClass(EmailAuthCredential.class);
    verify(mScratchMockAuth).signInWithCredential(credentialCaptor.capture());
    EmailAuthCredential capturedCredential = credentialCaptor.getValue();
    // TODO: EmailAuthCredential no longer exposes .getEmail() or .getPassword()
    // assertThat(capturedCredential.getEmail()).isEqualTo(TestConstants.EMAIL);
    // assertThat(capturedCredential.getPassword()).isEqualTo(TestConstants.PASSWORD);
    // Verify that account linking is attempted
    verify(FakeAuthResult.INSTANCE.getUser()).linkWithCredential(credential);
    verifyMergeFailure();
}
Also used : EmailAuthCredential(com.google.firebase.auth.EmailAuthCredential) AuthCredential(com.google.firebase.auth.AuthCredential) EmailAuthCredential(com.google.firebase.auth.EmailAuthCredential) User(com.firebase.ui.auth.data.model.User) FirebaseUser(com.google.firebase.auth.FirebaseUser) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 4 with AuthOperationManager

use of com.firebase.ui.auth.util.data.AuthOperationManager in project FirebaseUI-Android by firebase.

the class WelcomeBackPasswordHandlerTest method testSignIn_anonymousUserUpgradeEnabledWithEmailFlow_expectValidateCredentialAndMergeFailure.

@Test
public void testSignIn_anonymousUserUpgradeEnabledWithEmailFlow_expectValidateCredentialAndMergeFailure() {
    setupAnonymousUpgrade();
    mHandler.getOperation().observeForever(mResponseObserver);
    // Fake user with password provider
    User user = new User.Builder(EmailAuthProvider.PROVIDER_ID, TestConstants.EMAIL).build();
    IdpResponse response = new IdpResponse.Builder(user).build();
    // Need to control FirebaseAuth's return values
    AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
    authOperationManager.mScratchAuth = mScratchMockAuth;
    when(mScratchMockAuth.signInWithCredential(any(AuthCredential.class))).thenReturn(AutoCompleteTask.forSuccess(FakeAuthResult.INSTANCE));
    // Kick off the sign in flow
    mHandler.startSignIn(TestConstants.EMAIL, TestConstants.PASSWORD, response, null);
    // Verify that signInWithCredential was called
    ArgumentCaptor<EmailAuthCredential> credentialCaptor = ArgumentCaptor.forClass(EmailAuthCredential.class);
    verify(mScratchMockAuth).signInWithCredential(credentialCaptor.capture());
    EmailAuthCredential capturedCredential = credentialCaptor.getValue();
    // TODO: EmailAuthCredential no longer exposes .getEmail() or .getPassword()
    // assertThat(capturedCredential.getEmail()).isEqualTo(TestConstants.EMAIL);
    // assertThat(capturedCredential.getPassword()).isEqualTo(TestConstants.PASSWORD);
    verifyMergeFailure();
}
Also used : EmailAuthCredential(com.google.firebase.auth.EmailAuthCredential) AuthCredential(com.google.firebase.auth.AuthCredential) EmailAuthCredential(com.google.firebase.auth.EmailAuthCredential) User(com.firebase.ui.auth.data.model.User) FirebaseUser(com.google.firebase.auth.FirebaseUser) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 5 with AuthOperationManager

use of com.firebase.ui.auth.util.data.AuthOperationManager in project FirebaseUI-Android by firebase.

the class EmailProviderResponseHandler method startSignIn.

public void startSignIn(@NonNull final IdpResponse response, @NonNull final String password) {
    if (!response.isSuccessful()) {
        setResult(Resource.forFailure(response.getError()));
        return;
    }
    if (!response.getProviderType().equals(EmailAuthProvider.PROVIDER_ID)) {
        throw new IllegalStateException("This handler can only be used with the email provider");
    }
    setResult(Resource.forLoading());
    final AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
    final String email = response.getEmail();
    authOperationManager.createOrLinkUserWithEmailAndPassword(getAuth(), getArguments(), email, password).continueWithTask(new ProfileMerger(response)).addOnFailureListener(new TaskFailureLogger(TAG, "Error creating user")).addOnSuccessListener(result -> handleSuccess(response, result)).addOnFailureListener(e -> {
        if (e instanceof FirebaseAuthUserCollisionException) {
            if (authOperationManager.canUpgradeAnonymous(getAuth(), getArguments())) {
                AuthCredential credential = EmailAuthProvider.getCredential(email, password);
                handleMergeFailure(credential);
            } else {
                Log.w(TAG, "Got a collision error during a non-upgrade flow", e);
                // Collision with existing user email without anonymous upgrade
                // it should be very hard for the user to even get to this error
                // due to CheckEmailFragment.
                ProviderUtils.fetchTopProvider(getAuth(), getArguments(), email).addOnSuccessListener(new StartWelcomeBackFlow(email)).addOnFailureListener(e1 -> setResult(Resource.forFailure(e1)));
            }
        } else {
            setResult(Resource.forFailure(e));
        }
    });
}
Also used : WelcomeBackIdpPrompt(com.firebase.ui.auth.ui.idp.WelcomeBackIdpPrompt) ProviderUtils(com.firebase.ui.auth.util.data.ProviderUtils) ProfileMerger(com.firebase.ui.auth.data.remote.ProfileMerger) EMAIL_LINK_PROVIDER(com.firebase.ui.auth.AuthUI.EMAIL_LINK_PROVIDER) NonNull(androidx.annotation.NonNull) User(com.firebase.ui.auth.data.model.User) IntentRequiredException(com.firebase.ui.auth.data.model.IntentRequiredException) Resource(com.firebase.ui.auth.data.model.Resource) TaskFailureLogger(com.firebase.ui.auth.util.data.TaskFailureLogger) RestrictTo(androidx.annotation.RestrictTo) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) Log(android.util.Log) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) SignInViewModelBase(com.firebase.ui.auth.viewmodel.SignInViewModelBase) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) AuthCredential(com.google.firebase.auth.AuthCredential) Nullable(androidx.annotation.Nullable) EmailAuthProvider(com.google.firebase.auth.EmailAuthProvider) Application(android.app.Application) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) WelcomeBackEmailLinkPrompt(com.firebase.ui.auth.ui.email.WelcomeBackEmailLinkPrompt) RequestCodes(com.firebase.ui.auth.viewmodel.RequestCodes) AuthResult(com.google.firebase.auth.AuthResult) ErrorCodes(com.firebase.ui.auth.ErrorCodes) IdpResponse(com.firebase.ui.auth.IdpResponse) WelcomeBackPasswordPrompt(com.firebase.ui.auth.ui.email.WelcomeBackPasswordPrompt) AuthCredential(com.google.firebase.auth.AuthCredential) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) TaskFailureLogger(com.firebase.ui.auth.util.data.TaskFailureLogger) ProfileMerger(com.firebase.ui.auth.data.remote.ProfileMerger) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException)

Aggregations

AuthOperationManager (com.firebase.ui.auth.util.data.AuthOperationManager)12 IdpResponse (com.firebase.ui.auth.IdpResponse)11 AuthCredential (com.google.firebase.auth.AuthCredential)11 Resource (com.firebase.ui.auth.data.model.Resource)9 FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)7 User (com.firebase.ui.auth.data.model.User)6 Test (org.junit.Test)6 Application (android.app.Application)5 NonNull (androidx.annotation.NonNull)5 Nullable (androidx.annotation.Nullable)5 SignInViewModelBase (com.firebase.ui.auth.viewmodel.SignInViewModelBase)5 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)5 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)5 AuthResult (com.google.firebase.auth.AuthResult)5 EmailAuthCredential (com.google.firebase.auth.EmailAuthCredential)5 EmailAuthProvider (com.google.firebase.auth.EmailAuthProvider)5 FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)5 ErrorCodes (com.firebase.ui.auth.ErrorCodes)4 ProfileMerger (com.firebase.ui.auth.data.remote.ProfileMerger)4 ProviderUtils (com.firebase.ui.auth.util.data.ProviderUtils)4