Search in sources :

Example 46 with IdpResponse

use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.

the class CredentialSignInHandlerTest method testSignInFailed_withPasswordAccountAlreadyLinked.

@Test
public void testSignInFailed_withPasswordAccountAlreadyLinked() {
    AppCompatBase mockActivity = mock(AppCompatBase.class);
    ActivityHelper mockActivityHelper = mock(ActivityHelper.class);
    FirebaseAuth mockFirebaseAuth = mock(FirebaseAuth.class);
    IdpResponse idpResponse = new IdpResponse(GoogleAuthProvider.PROVIDER_ID, TestConstants.EMAIL);
    CredentialSignInHandler credentialSignInHandler = new CredentialSignInHandler(mockActivity, mockActivityHelper, null, RC_ACCOUNT_LINK, idpResponse);
    Task mockTask = mock(Task.class);
    FlowParameters mockFlowParams = mock(FlowParameters.class);
    // pretend there was already an account with this email
    when(mockTask.getException()).thenReturn(new FirebaseAuthUserCollisionException(LINKING_ERROR, LINKING_EXPLANATION));
    when(mockActivityHelper.getFirebaseAuth()).thenReturn(mockFirebaseAuth);
    when(mockActivityHelper.getFlowParams()).thenReturn(mockFlowParams);
    // pretend the account has a Password account linked already
    when(mockFirebaseAuth.fetchProvidersForEmail(TestConstants.EMAIL)).thenReturn(new AutoCompleteTask<ProviderQueryResult>(new FakeProviderQueryResult(Arrays.asList(EmailAuthProvider.PROVIDER_ID)), true, null));
    credentialSignInHandler.onComplete(mockTask);
    ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
    ArgumentCaptor<Integer> intCaptor = ArgumentCaptor.forClass(Integer.class);
    verify(mockActivity).startActivityForResult(intentCaptor.capture(), intCaptor.capture());
    Intent capturedIntent = intentCaptor.getValue();
    assertEquals(RC_ACCOUNT_LINK, (int) intCaptor.getValue());
    assertEquals(WelcomeBackPasswordPrompt.class.getName(), capturedIntent.getComponent().getClassName());
    assertEquals(TestConstants.EMAIL, IdpResponse.fromResultIntent(capturedIntent).getEmail());
}
Also used : Task(com.google.android.gms.tasks.Task) AutoCompleteTask(com.firebase.ui.auth.testhelpers.AutoCompleteTask) AppCompatBase(com.firebase.ui.auth.ui.AppCompatBase) Intent(android.content.Intent) FakeProviderQueryResult(com.firebase.ui.auth.testhelpers.FakeProviderQueryResult) ProviderQueryResult(com.google.firebase.auth.ProviderQueryResult) ActivityHelper(com.firebase.ui.auth.ui.ActivityHelper) FlowParameters(com.firebase.ui.auth.ui.FlowParameters) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FakeProviderQueryResult(com.firebase.ui.auth.testhelpers.FakeProviderQueryResult) FirebaseAuth(com.google.firebase.auth.FirebaseAuth) IdpResponse(com.firebase.ui.auth.IdpResponse) WelcomeBackPasswordPrompt(com.firebase.ui.auth.ui.accountlink.WelcomeBackPasswordPrompt) Test(org.junit.Test)

Example 47 with IdpResponse

use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.

the class EmailLinkSignInHandler method finishSignIn.

private void finishSignIn(SessionRecord sessionRecord) {
    String email = sessionRecord.getEmail();
    IdpResponse response = sessionRecord.getIdpResponseForLinking();
    finishSignIn(email, response);
}
Also used : IdpResponse(com.firebase.ui.auth.IdpResponse)

Example 48 with IdpResponse

use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.

the class EmailLinkSignInHandler method handleNormalFlow.

private void handleNormalFlow(final AuthOperationManager authOperationManager, final EmailLinkPersistenceManager persistenceManager, final String email, final String link) {
    final AuthCredential emailLinkCredential = EmailAuthProvider.getCredentialWithLink(email, link);
    // Bug in core SDK - credential is mutated and won't be usable for sign in, so create
    // a new one to pass back. b/117425827
    final AuthCredential emailLinkCredentialForLinking = EmailAuthProvider.getCredentialWithLink(email, link);
    // Either regular sign in or anonymous user upgrade
    authOperationManager.signInAndLinkWithCredential(getAuth(), getArguments(), emailLinkCredential).addOnSuccessListener(authResult -> {
        persistenceManager.clearAllData(getApplication());
        FirebaseUser user = authResult.getUser();
        IdpResponse response = new IdpResponse.Builder(new User.Builder(EMAIL_LINK_PROVIDER, user.getEmail()).setName(user.getDisplayName()).setPhotoUri(user.getPhotoUrl()).build()).build();
        handleSuccess(response, authResult);
    }).addOnFailureListener(e -> {
        persistenceManager.clearAllData(getApplication());
        if (e instanceof FirebaseAuthUserCollisionException) {
            handleMergeFailure(emailLinkCredentialForLinking);
        } else {
            setResult(Resource.forFailure(e));
        }
    });
}
Also used : AuthCredential(com.google.firebase.auth.AuthCredential) ActionCodeResult(com.google.firebase.auth.ActionCodeResult) 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) EmailLinkPersistenceManager(com.firebase.ui.auth.util.data.EmailLinkPersistenceManager) Resource(com.firebase.ui.auth.data.model.Resource) EmailLinkParser(com.firebase.ui.auth.util.data.EmailLinkParser) TaskFailureLogger(com.firebase.ui.auth.util.data.TaskFailureLogger) Task(com.google.android.gms.tasks.Task) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) Continuation(com.google.android.gms.tasks.Continuation) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) SignInViewModelBase(com.firebase.ui.auth.viewmodel.SignInViewModelBase) FirebaseUser(com.google.firebase.auth.FirebaseUser) SessionRecord(com.firebase.ui.auth.util.data.EmailLinkPersistenceManager.SessionRecord) TextUtils(android.text.TextUtils) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) 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) AuthResult(com.google.firebase.auth.AuthResult) ErrorCodes(com.firebase.ui.auth.ErrorCodes) IdpResponse(com.firebase.ui.auth.IdpResponse) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseUser(com.google.firebase.auth.FirebaseUser) IdpResponse(com.firebase.ui.auth.IdpResponse)

Example 49 with IdpResponse

use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.

the class LinkingSocialProviderResponseHandler method startSignIn.

public void startSignIn(@NonNull final IdpResponse response) {
    if (!response.isSuccessful()) {
        setResult(Resource.forFailure(response.getError()));
        return;
    }
    if (isInvalidProvider(response.getProviderType())) {
        throw new IllegalStateException("This handler cannot be used to link email or phone providers.");
    }
    if (mEmail != null && !mEmail.equals(response.getEmail())) {
        setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.EMAIL_MISMATCH_ERROR)));
        return;
    }
    setResult(Resource.forLoading());
    // return a credential.
    if (isGenericIdpLinkingFlow(response.getProviderType())) {
        getAuth().getCurrentUser().linkWithCredential(mRequestedSignInCredential).addOnSuccessListener(authResult -> handleSuccess(response, authResult)).addOnFailureListener(e -> Resource.<IdpResponse>forFailure(e));
        return;
    }
    final AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
    final AuthCredential credential = ProviderUtils.getAuthCredential(response);
    if (authOperationManager.canUpgradeAnonymous(getAuth(), getArguments())) {
        if (mRequestedSignInCredential == null) {
            // The user has provided a valid credential by signing in with a federated
            // idp. linkWithCredential will fail because the user is anonymous and the account
            // exists (we're in the welcome back flow).
            // We know that they are signing in with the same IDP because requestSignInCredential
            // is null.
            // We just need to have the developer handle the merge failure.
            handleMergeFailure(credential);
        } else {
            // The user has logged in with an IDP that has the same email with another IDP
            // present on the account.
            // These IDPs belong to the same account - they must be linked, but we can't lose
            // our anonymous user session
            authOperationManager.safeLink(credential, mRequestedSignInCredential, getArguments()).addOnSuccessListener(result -> handleMergeFailure(credential)).addOnFailureListener(e -> setResult(Resource.forFailure(e)));
        }
    } else {
        getAuth().signInWithCredential(credential).continueWithTask(task -> {
            final AuthResult result = task.getResult();
            if (mRequestedSignInCredential == null) {
                return Tasks.forResult(result);
            } else {
                return result.getUser().linkWithCredential(mRequestedSignInCredential).continueWith(task1 -> {
                    if (task1.isSuccessful()) {
                        return task1.getResult();
                    } else {
                        // to backtrack so we just ignore any errors.
                        return result;
                    }
                });
            }
        }).addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                handleSuccess(response, task.getResult());
            } else {
                setResult(Resource.forFailure(task.getException()));
            }
        });
    }
}
Also used : ProviderUtils(com.firebase.ui.auth.util.data.ProviderUtils) NonNull(androidx.annotation.NonNull) SignInViewModelBase(com.firebase.ui.auth.viewmodel.SignInViewModelBase) TextUtils(android.text.TextUtils) Resource(com.firebase.ui.auth.data.model.Resource) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) PhoneAuthProvider(com.google.firebase.auth.PhoneAuthProvider) Task(com.google.android.gms.tasks.Task) AuthCredential(com.google.firebase.auth.AuthCredential) Tasks(com.google.android.gms.tasks.Tasks) Nullable(androidx.annotation.Nullable) RestrictTo(androidx.annotation.RestrictTo) EmailAuthProvider(com.google.firebase.auth.EmailAuthProvider) Application(android.app.Application) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) AuthUI(com.firebase.ui.auth.AuthUI) Continuation(com.google.android.gms.tasks.Continuation) AuthResult(com.google.firebase.auth.AuthResult) ErrorCodes(com.firebase.ui.auth.ErrorCodes) IdpResponse(com.firebase.ui.auth.IdpResponse) AuthCredential(com.google.firebase.auth.AuthCredential) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) AuthResult(com.google.firebase.auth.AuthResult) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException)

Example 50 with IdpResponse

use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.

the class EmailLinkSignInHandlerTest method testStartSignIn_normalFlowWithAnonymousUpgrade_expectSuccessfulMerge.

@Test
@SuppressWarnings("all")
public void testStartSignIn_normalFlowWithAnonymousUpgrade_expectSuccessfulMerge() {
    mHandler.getOperation().observeForever(mResponseObserver);
    setupAnonymousUpgrade();
    when(mMockAuth.isSignInWithEmailLink(any(String.class))).thenReturn(true);
    mPersistenceManager.saveEmail(ApplicationProvider.getApplicationContext(), TestConstants.EMAIL, TestConstants.SESSION_ID, TestConstants.UID);
    when(mMockAuth.getCurrentUser().linkWithCredential(any(AuthCredential.class))).thenReturn(new AutoContinueTask<>(mMockAuthResult, mMockAuthResult, true, null));
    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());
    IdpResponse response = captor.getValue().getValue();
    assertThat(response.getUser().getProviderId()).isEqualTo(AuthUI.EMAIL_LINK_PROVIDER);
    assertThat(response.getUser().getEmail()).isEqualTo(mMockAuthResult.getUser().getEmail());
    assertThat(response.getUser().getName()).isEqualTo(mMockAuthResult.getUser().getDisplayName());
    assertThat(response.getUser().getPhotoUri()).isEqualTo(mMockAuthResult.getUser().getPhotoUrl());
    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) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Aggregations

IdpResponse (com.firebase.ui.auth.IdpResponse)81 Test (org.junit.Test)45 Resource (com.firebase.ui.auth.data.model.Resource)35 AuthCredential (com.google.firebase.auth.AuthCredential)31 InOrder (org.mockito.InOrder)31 FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)22 FirebaseAuthAnonymousUpgradeException (com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException)19 FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)19 User (com.firebase.ui.auth.data.model.User)18 FirebaseUser (com.google.firebase.auth.FirebaseUser)15 AuthOperationManager (com.firebase.ui.auth.util.data.AuthOperationManager)12 EmailAuthCredential (com.google.firebase.auth.EmailAuthCredential)11 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)10 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)10 AuthResult (com.google.firebase.auth.AuthResult)10 Intent (android.content.Intent)9 Nullable (androidx.annotation.Nullable)9 IntentRequiredException (com.firebase.ui.auth.data.model.IntentRequiredException)9 Task (com.google.android.gms.tasks.Task)9 Application (android.app.Application)8