Search in sources :

Example 6 with FirebaseUiException

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

the class EmailLinkSignInHandlerTest method testStartSignIn_differentDeviceLinkWithNoSessionId_expectInvalidLinkError.

@Test
@SuppressWarnings("all")
public void testStartSignIn_differentDeviceLinkWithNoSessionId_expectInvalidLinkError() {
    initializeHandlerWithSessionInfo(null, null, null, true);
    mHandler.getOperation().observeForever(mResponseObserver);
    when(mMockAuth.isSignInWithEmailLink(any(String.class))).thenReturn(true);
    mHandler.startSignIn();
    verify(mMockAuth).isSignInWithEmailLink(any(String.class));
    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());
    FirebaseUiException exception = (FirebaseUiException) captor.getValue().getException();
    assertThat(exception).isNotNull();
    assertThat(exception.getErrorCode()).isEqualTo(ErrorCodes.INVALID_EMAIL_LINK_ERROR);
}
Also used : InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 7 with FirebaseUiException

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

the class EmailLinkSignInHandlerTest method testStartSignIn_differentDeviceLinkWithValidSessionInfo_expectInvalidLinkError.

@Test
@SuppressWarnings("all")
public void testStartSignIn_differentDeviceLinkWithValidSessionInfo_expectInvalidLinkError() {
    String differentSessionId = SessionUtils.generateRandomAlphaNumericString(10);
    initializeHandlerWithSessionInfo(differentSessionId, null, null, false);
    mHandler.getOperation().observeForever(mResponseObserver);
    when(mMockAuth.isSignInWithEmailLink(any(String.class))).thenReturn(true);
    when(mMockAuth.checkActionCode(any(String.class))).thenReturn(AutoCompleteTask.<ActionCodeResult>forFailure(new Exception("foo")));
    mHandler.startSignIn();
    verify(mMockAuth).isSignInWithEmailLink(any(String.class));
    verify(mMockAuth).checkActionCode(any(String.class));
    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());
    FirebaseUiException exception = (FirebaseUiException) captor.getValue().getException();
    assertThat(exception).isNotNull();
    assertThat(exception.getErrorCode()).isEqualTo(ErrorCodes.INVALID_EMAIL_LINK_ERROR);
}
Also used : InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) 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 8 with FirebaseUiException

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

the class SocialProviderResponseHandler method startSignIn.

public void startSignIn(@NonNull final IdpResponse response) {
    if (!response.isSuccessful() && !response.isRecoverableErrorResponse()) {
        setResult(Resource.forFailure(response.getError()));
        return;
    }
    if (isEmailOrPhoneProvider(response.getProviderType())) {
        throw new IllegalStateException("This handler cannot be used with email or phone providers");
    }
    setResult(Resource.forLoading());
    // a credential made from the id token/access token cannot be used to sign-in.
    if (response.hasCredentialForLinking()) {
        handleGenericIdpLinkingFlow(response);
        return;
    }
    final AuthCredential credential = ProviderUtils.getAuthCredential(response);
    AuthOperationManager.getInstance().signInAndLinkWithCredential(getAuth(), getArguments(), credential).continueWithTask(new ProfileMerger(response)).addOnSuccessListener(result -> handleSuccess(response, result)).addOnFailureListener(e -> {
        // For some reason disabled users can hit FirebaseAuthUserCollisionException
        // so we have to handle this special case.
        boolean isDisabledUser = (e instanceof FirebaseAuthInvalidUserException);
        if (e instanceof FirebaseAuthException) {
            FirebaseAuthException authEx = (FirebaseAuthException) e;
            FirebaseAuthError fae = FirebaseAuthError.fromException(authEx);
            if (fae == FirebaseAuthError.ERROR_USER_DISABLED) {
                isDisabledUser = true;
            }
        }
        if (isDisabledUser) {
            setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.ERROR_USER_DISABLED)));
        } else if (e instanceof FirebaseAuthUserCollisionException) {
            final String email = response.getEmail();
            if (email == null) {
                setResult(Resource.forFailure(e));
                return;
            }
            // There can be a collision due to:
            // CASE 1: Anon user signing in with a credential that belongs to an
            // existing user.
            // CASE 2: non - anon user signing in with a credential that does not
            // belong to an existing user, but the email matches an existing user
            // that has another social IDP. We need to link this new IDP to this
            // existing user.
            // CASE 3: CASE 2 with an anonymous user. We link the new IDP to the
            // same account before handling invoking a merge failure.
            ProviderUtils.fetchSortedProviders(getAuth(), getArguments(), email).addOnSuccessListener(providers -> {
                if (providers.contains(response.getProviderType())) {
                    // Case 1
                    handleMergeFailure(credential);
                } else if (providers.isEmpty()) {
                    setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.DEVELOPER_ERROR, "No supported providers.")));
                } else {
                    // Case 2 & 3 - we need to link
                    startWelcomeBackFlowForLinking(providers.get(0), response);
                }
            }).addOnFailureListener(e1 -> setResult(Resource.forFailure(e1)));
        }
    });
}
Also used : AuthCredential(com.google.firebase.auth.AuthCredential) 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) Intent(android.content.Intent) Resource(com.firebase.ui.auth.data.model.Resource) PhoneAuthProvider(com.google.firebase.auth.PhoneAuthProvider) RestrictTo(androidx.annotation.RestrictTo) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseAuthInvalidUserException(com.google.firebase.auth.FirebaseAuthInvalidUserException) SignInViewModelBase(com.firebase.ui.auth.viewmodel.SignInViewModelBase) TextUtils(android.text.TextUtils) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) FirebaseAuthError(com.firebase.ui.auth.util.FirebaseAuthError) AuthCredential(com.google.firebase.auth.AuthCredential) List(java.util.List) 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) FirebaseAuthException(com.google.firebase.auth.FirebaseAuthException) RequestCodes(com.firebase.ui.auth.viewmodel.RequestCodes) AuthResult(com.google.firebase.auth.AuthResult) ErrorCodes(com.firebase.ui.auth.ErrorCodes) Activity(android.app.Activity) IdpResponse(com.firebase.ui.auth.IdpResponse) WelcomeBackPasswordPrompt(com.firebase.ui.auth.ui.email.WelcomeBackPasswordPrompt) FirebaseAuthInvalidUserException(com.google.firebase.auth.FirebaseAuthInvalidUserException) FirebaseAuthError(com.firebase.ui.auth.util.FirebaseAuthError) ProfileMerger(com.firebase.ui.auth.data.remote.ProfileMerger) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) FirebaseAuthException(com.google.firebase.auth.FirebaseAuthException)

Example 9 with FirebaseUiException

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

the class SocialProviderResponseHandler method onActivityResult.

public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (requestCode == RequestCodes.ACCOUNT_LINK_FLOW) {
        IdpResponse response = IdpResponse.fromResultIntent(data);
        if (resultCode == Activity.RESULT_OK) {
            setResult(Resource.forSuccess(response));
        } else {
            Exception e;
            if (response == null) {
                e = new FirebaseUiException(ErrorCodes.UNKNOWN_ERROR, "Link canceled by user.");
            } else {
                e = response.getError();
            }
            setResult(Resource.forFailure(e));
        }
    }
}
Also used : FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) IntentRequiredException(com.firebase.ui.auth.data.model.IntentRequiredException) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseAuthInvalidUserException(com.google.firebase.auth.FirebaseAuthInvalidUserException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) FirebaseAuthException(com.google.firebase.auth.FirebaseAuthException) IdpResponse(com.firebase.ui.auth.IdpResponse)

Example 10 with FirebaseUiException

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

the class SmartLockHandler method saveCredentials.

/**
 * Initialize saving a credential.
 */
public void saveCredentials(@Nullable Credential credential) {
    if (!getArguments().enableCredentials) {
        setResult(Resource.forSuccess(mResponse));
        return;
    }
    setResult(Resource.forLoading());
    if (credential == null) {
        setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.UNKNOWN_ERROR, "Failed to build credential.")));
        return;
    }
    deleteUnusedCredentials();
    getCredentialsClient().save(credential).addOnCompleteListener(task -> {
        if (task.isSuccessful()) {
            setResult(Resource.forSuccess(mResponse));
        } else if (task.getException() instanceof ResolvableApiException) {
            ResolvableApiException rae = (ResolvableApiException) task.getException();
            setResult(Resource.forFailure(new PendingIntentRequiredException(rae.getResolution(), RequestCodes.CRED_SAVE)));
        } else {
            Log.w(TAG, "Non-resolvable exception: " + task.getException());
            setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.UNKNOWN_ERROR, "Error when saving credential.", task.getException())));
        }
    });
}
Also used : ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) PendingIntentRequiredException(com.firebase.ui.auth.data.model.PendingIntentRequiredException)

Aggregations

FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)22 IdpResponse (com.firebase.ui.auth.IdpResponse)16 Resource (com.firebase.ui.auth.data.model.Resource)9 FirebaseAuthAnonymousUpgradeException (com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException)8 ViewModelProvider (androidx.lifecycle.ViewModelProvider)6 Test (org.junit.Test)6 InOrder (org.mockito.InOrder)6 User (com.firebase.ui.auth.data.model.User)5 Intent (android.content.Intent)4 AuthUI (com.firebase.ui.auth.AuthUI)4 UserCancellationException (com.firebase.ui.auth.data.model.UserCancellationException)4 FirebaseAuthError (com.firebase.ui.auth.util.FirebaseAuthError)4 AuthOperationManager (com.firebase.ui.auth.util.data.AuthOperationManager)4 FirebaseAuthException (com.google.firebase.auth.FirebaseAuthException)4 FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)4 Application (android.app.Application)3 TextView (android.widget.TextView)3 NonNull (androidx.annotation.NonNull)3 Nullable (androidx.annotation.Nullable)3 RestrictTo (androidx.annotation.RestrictTo)3