Search in sources :

Example 11 with OAuthProvider

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

the class GenericIdpAnonymousUpgradeLinkingHandler method startSignIn.

@Override
public void startSignIn(@NonNull FirebaseAuth auth, @NonNull HelperActivityBase activity, @NonNull String providerId) {
    setResult(Resource.forLoading());
    FlowParameters flowParameters = activity.getFlowParams();
    OAuthProvider provider = buildOAuthProvider(providerId, auth);
    if (flowParameters != null && AuthOperationManager.getInstance().canUpgradeAnonymous(auth, flowParameters)) {
        handleAnonymousUpgradeLinkingFlow(activity, provider, flowParameters);
        return;
    }
    handleNormalSignInFlow(auth, activity, provider);
}
Also used : FlowParameters(com.firebase.ui.auth.data.model.FlowParameters) OAuthProvider(com.google.firebase.auth.OAuthProvider)

Example 12 with OAuthProvider

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

the class GenericIdpSignInHandler method handleNormalSignInFlow.

protected void handleNormalSignInFlow(final FirebaseAuth auth, final HelperActivityBase activity, final OAuthProvider provider) {
    final boolean useEmulator = activity.getAuthUI().isUseEmulator();
    auth.startActivityForSignInWithProvider(activity, provider).addOnSuccessListener(authResult -> handleSuccess(useEmulator, provider.getProviderId(), authResult.getUser(), (OAuthCredential) authResult.getCredential(), authResult.getAdditionalUserInfo().isNewUser())).addOnFailureListener(e -> {
        if (e instanceof FirebaseAuthException) {
            FirebaseAuthError error = FirebaseAuthError.fromException((FirebaseAuthException) e);
            if (e instanceof FirebaseAuthUserCollisionException) {
                FirebaseAuthUserCollisionException collisionException = (FirebaseAuthUserCollisionException) e;
                setResult(Resource.forFailure(new FirebaseUiUserCollisionException(ErrorCodes.ERROR_GENERIC_IDP_RECOVERABLE_ERROR, "Recoverable error.", provider.getProviderId(), collisionException.getEmail(), collisionException.getUpdatedCredential())));
            } else if (error == FirebaseAuthError.ERROR_WEB_CONTEXT_CANCELED) {
                setResult(Resource.forFailure(new UserCancellationException()));
            } else {
                setResult(Resource.forFailure(e));
            }
        } else {
            setResult(Resource.forFailure(e));
        }
    });
}
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) UserCancellationException(com.firebase.ui.auth.data.model.UserCancellationException) FirebaseAuthError(com.firebase.ui.auth.util.FirebaseAuthError) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseAuthException(com.google.firebase.auth.FirebaseAuthException) FirebaseUiUserCollisionException(com.firebase.ui.auth.FirebaseUiUserCollisionException)

Example 13 with OAuthProvider

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

the class GenericIdpAnonymousUpgradeLinkingHandlerTest method testStartSignIn_anonymousUpgradeLinkingFlow_expectIdpResponseWithCredential.

@Test
public void testStartSignIn_anonymousUpgradeLinkingFlow_expectIdpResponseWithCredential() {
    setupAnonymousUpgrade();
    AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
    authOperationManager.mScratchAuth = mScratchMockAuth;
    when(mScratchMockAuth.startActivityForSignInWithProvider(any(HelperActivityBase.class), any(OAuthProvider.class))).thenReturn(AutoCompleteTask.forSuccess(mMockAuthResult));
    AuthCredential credential = OAuthProvider.newCredentialBuilder(MICROSOFT_PROVIDER).setIdToken(ID_TOKEN).setAccessToken(ACCESS_TOKEN).build();
    when(mMockAuthResult.getCredential()).thenReturn(credential);
    when(mMockAuthResult.getUser()).thenReturn(mMockUser);
    when(mMockUser.getDisplayName()).thenReturn(DISPLAY_NAME);
    when(mMockUser.getPhotoUrl()).thenReturn(new Uri.Builder().build());
    mockOAuthProvider(MICROSOFT_PROVIDER);
    mHandler.startSignIn(mMockAuth, mMockActivity, MICROSOFT_PROVIDER);
    ArgumentCaptor<OAuthProvider> providerCaptor = ArgumentCaptor.forClass(OAuthProvider.class);
    verify(mScratchMockAuth).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>> resolveCaptor = ArgumentCaptor.forClass(Resource.class);
    inOrder.verify(mResponseObserver).onChanged(resolveCaptor.capture());
    IdpResponse idpResponse = resolveCaptor.getValue().getValue();
    assertThat(idpResponse.getCredentialForLinking()).isNotNull();
}
Also used : AuthCredential(com.google.firebase.auth.AuthCredential) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) InOrder(org.mockito.InOrder) HelperActivityBase(com.firebase.ui.auth.ui.HelperActivityBase) Resource(com.firebase.ui.auth.data.model.Resource) OAuthProvider(com.google.firebase.auth.OAuthProvider) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Aggregations

OAuthProvider (com.google.firebase.auth.OAuthProvider)13 Resource (com.firebase.ui.auth.data.model.Resource)9 Test (org.junit.Test)7 InOrder (org.mockito.InOrder)7 Activity (android.app.Activity)6 AuthCredential (com.google.firebase.auth.AuthCredential)6 IdpResponse (com.firebase.ui.auth.IdpResponse)5 FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)5 FirebaseUiUserCollisionException (com.firebase.ui.auth.FirebaseUiUserCollisionException)4 FlowParameters (com.firebase.ui.auth.data.model.FlowParameters)4 FirebaseAuth (com.google.firebase.auth.FirebaseAuth)4 FirebaseAuthAnonymousUpgradeException (com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException)3 HelperActivityBase (com.firebase.ui.auth.ui.HelperActivityBase)3 AuthOperationManager (com.firebase.ui.auth.util.data.AuthOperationManager)3 FirebaseAuthException (com.google.firebase.auth.FirebaseAuthException)3 Application (android.app.Application)2 Intent (android.content.Intent)2 NonNull (androidx.annotation.NonNull)2 Nullable (androidx.annotation.Nullable)2 RestrictTo (androidx.annotation.RestrictTo)2