com.google.firebase.auth.OAuthProvider Java Examples

The following examples show how to use com.google.firebase.auth.OAuthProvider. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GenericIdpAnonymousUpgradeLinkingHandler.java    From FirebaseUI-Android with Apache License 2.0 6 votes vote down vote up
@Override
public void startSignIn(@NonNull FirebaseAuth auth,
                        @NonNull HelperActivityBase activity,
                        @NonNull String providerId) {
    setResult(Resource.<IdpResponse>forLoading());

    FlowParameters flowParameters = activity.getFlowParams();
    OAuthProvider provider = buildOAuthProvider(providerId);
    if (flowParameters != null
            && AuthOperationManager.getInstance().canUpgradeAnonymous(auth, flowParameters)) {
            handleAnonymousUpgradeLinkingFlow(activity, provider, flowParameters);
        return;
    }

    handleNormalSignInFlow(auth, activity, provider);
}
 
Example #2
Source File: GenericIdpAnonymousUpgradeLinkingHandler.java    From FirebaseUI-Android with Apache License 2.0 6 votes vote down vote up
private void handleAnonymousUpgradeLinkingFlow(final HelperActivityBase activity,
                                               final OAuthProvider provider,
                                               final FlowParameters flowParameters) {
    AuthOperationManager.getInstance().safeGenericIdpSignIn(activity, provider, flowParameters)
            .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                @Override
                public void onSuccess(AuthResult authResult) {
                    // Pass the credential so we can sign-in on the after the merge
                    // conflict is resolved.
                    handleSuccess(provider.getProviderId(),
                            authResult.getUser(), (OAuthCredential) authResult.getCredential(),
                            /* setPendingCredential= */true);
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    setResult(Resource.<IdpResponse>forFailure(e));
                }
            });

}
 
Example #3
Source File: GenericIdpSignInHandler.java    From FirebaseUI-Android with Apache License 2.0 6 votes vote down vote up
@Override
public void startSignIn(@NonNull FirebaseAuth auth,
                        @NonNull HelperActivityBase activity,
                        @NonNull String providerId) {
    setResult(Resource.<IdpResponse>forLoading());

    FlowParameters flowParameters = activity.getFlowParams();
    OAuthProvider provider = buildOAuthProvider(providerId);
    if (flowParameters != null
            && AuthOperationManager.getInstance().canUpgradeAnonymous(auth, flowParameters)) {
        handleAnonymousUpgradeFlow(auth, activity, provider, flowParameters);
        return;
    }

    handleNormalSignInFlow(auth, activity, provider);
}
 
Example #4
Source File: GenericIdpSignInHandler.java    From FirebaseUI-Android with Apache License 2.0 6 votes vote down vote up
protected OAuthProvider buildOAuthProvider(String providerId) {
    OAuthProvider.Builder providerBuilder =
            OAuthProvider.newBuilder(providerId);

    List<String> scopes =
            getArguments().getParams().getStringArrayList(ExtraConstants.GENERIC_OAUTH_SCOPES);
    Map<String, String> customParams =
            getArguments().getParams()
                    .getParcelable(ExtraConstants.GENERIC_OAUTH_CUSTOM_PARAMETERS);

    if (scopes != null) {
        providerBuilder.setScopes(scopes);
    }
    if (customParams != null) {
        providerBuilder.addCustomParameters(customParams);
    }

    return providerBuilder.build();
}
 
Example #5
Source File: GenericIdpActivity.java    From quickstart-android with Apache License 2.0 5 votes vote down vote up
private void signIn() {
    // Could add custom scopes here
    ArrayList<String> scopes = new ArrayList<>();

    // Examples of provider ID: apple.com (Apple), microsoft.com (Microsoft), yahoo.com (Yahoo)
    String providerId = getProviderId();

    mAuth.startActivityForSignInWithProvider(this,
            OAuthProvider.newBuilder(providerId, mAuth)
                    .setScopes(scopes)
                    .build())
            .addOnSuccessListener(
                    new OnSuccessListener<AuthResult>() {
                        @Override
                        public void onSuccess(AuthResult authResult) {
                            Log.d(TAG, "activitySignIn:onSuccess:" + authResult.getUser());
                            updateUI(authResult.getUser());
                        }
                    })
            .addOnFailureListener(
                    new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Log.w(TAG, "activitySignIn:onFailure", e);
                            showToast(getString(R.string.error_sign_in_failed));
                        }
                    });
}
 
Example #6
Source File: GenericIdpSignInHandlerTest.java    From FirebaseUI-Android with Apache License 2.0 5 votes vote down vote up
@Test
public void testStartSignIn_anonymousUpgradeFlowWithConflict_expectRecoverableError() {
    setupAnonymousUpgrade();

    AuthCredential credential
            = OAuthProvider.getCredential(MICROSOFT_PROVIDER, ID_TOKEN, ACCESS_TOKEN);
    FirebaseAuthUserCollisionException collisionException
            = new FirebaseAuthUserCollisionException("foo", "bar");
    collisionException.zza(EMAIL).zza(credential);
    when(mMockAuth.getCurrentUser().startActivityForLinkWithProvider(
            any(Activity.class), any(OAuthProvider.class)))
            .thenReturn(AutoCompleteTask.<AuthResult>forFailure(collisionException));

    // Case 1: Anon user signing in with an existing account
    when(mMockAuth.fetchSignInMethodsForEmail(any(String.class)))
            .thenReturn(AutoCompleteTask.<SignInMethodQueryResult>forSuccess(
                    new FakeSignInMethodQueryResult(Arrays.asList(
                            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.<IdpResponse>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();
}
 
Example #7
Source File: GenericIdpSignInHandlerTest.java    From FirebaseUI-Android with Apache License 2.0 5 votes vote down vote up
@Test
public void testStartSignIn_anonymousUpgradeFlow_expectSuccess() {
    setupAnonymousUpgrade();

    when(mMockAuth.getCurrentUser().startActivityForLinkWithProvider(
            any(Activity.class), any(OAuthProvider.class)))
            .thenReturn(AutoCompleteTask.forSuccess(FakeAuthResult.INSTANCE));


    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.<IdpResponse>isLoading()));

    ArgumentCaptor<Resource<IdpResponse>> resourceCaptor =
            ArgumentCaptor.forClass(Resource.class);
    inOrder.verify(mResponseObserver)
            .onChanged(resourceCaptor.capture());

    IdpResponse returnedIdpResponse = resourceCaptor.getValue().getValue();

    assertThat(returnedIdpResponse.isSuccessful()).isTrue();
    assertThat(returnedIdpResponse.getUser()).isNotNull();

}
 
Example #8
Source File: GenericIdpSignInHandlerTest.java    From FirebaseUI-Android with Apache License 2.0 5 votes vote down vote up
@Test
public void testStartSignIn_normalSignInFlowWithError_expectFailure() {
    FirebaseAuthException firebaseAuthException
            = new FirebaseAuthException("foo", "bar");
    when(mMockAuth.startActivityForSignInWithProvider(any(Activity.class),
            any(OAuthProvider.class)))
            .thenReturn(AutoCompleteTask.<AuthResult>forFailure(firebaseAuthException));

    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.<IdpResponse>isLoading()));

    ArgumentCaptor<Resource<IdpResponse>> resourceCaptor =
            ArgumentCaptor.forClass(Resource.class);
    inOrder.verify(mResponseObserver)
            .onChanged(resourceCaptor.capture());

    assertThat(resourceCaptor.getValue().getException())
            .isInstanceOf(FirebaseAuthException.class);
}
 
Example #9
Source File: GenericIdpSignInHandlerTest.java    From FirebaseUI-Android with Apache License 2.0 5 votes vote down vote up
@Test
public void testStartSignIn_normalSignInFlowWithRecoverableError_expectFailure() {
    AuthCredential credential
            = OAuthProvider.getCredential(MICROSOFT_PROVIDER, ID_TOKEN, ACCESS_TOKEN);
    FirebaseAuthUserCollisionException collisionException
            = new FirebaseAuthUserCollisionException("foo", "bar");
    collisionException.zza(EMAIL).zza(credential);

    when(mMockAuth.startActivityForSignInWithProvider(any(Activity.class), any(OAuthProvider.class)))
            .thenReturn(AutoCompleteTask.<AuthResult>forFailure(collisionException));

    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.<IdpResponse>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);
}
 
Example #10
Source File: GenericIdpSignInHandlerTest.java    From FirebaseUI-Android with Apache License 2.0 5 votes vote down vote up
@Test
public void testStartSignIn_normalSignInFlow_expectSuccess() {
    mHandler.getOperation().observeForever(mResponseObserver);

    when(mMockAuth.startActivityForSignInWithProvider(any(Activity.class), any(OAuthProvider.class)))
            .thenReturn(AutoCompleteTask.forSuccess(FakeAuthResult.INSTANCE));

    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.<IdpResponse>isLoading()));

    ArgumentCaptor<Resource<IdpResponse>> resourceCaptor =
            ArgumentCaptor.forClass(Resource.class);
    inOrder.verify(mResponseObserver)
            .onChanged(resourceCaptor.capture());

    IdpResponse returnedIdpResponse = resourceCaptor.getValue().getValue();

    assertThat(returnedIdpResponse.isSuccessful()).isTrue();
    assertThat(returnedIdpResponse.getUser()).isNotNull();
}
 
Example #11
Source File: GenericIdpAnonymousUpgradeLinkingHandlerTest.java    From FirebaseUI-Android with Apache License 2.0 5 votes vote down vote up
@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.getCredential(MICROSOFT_PROVIDER, ID_TOKEN, ACCESS_TOKEN);
    when(mMockAuthResult.getCredential()).thenReturn(credential);
    when(mMockAuthResult.getUser()).thenReturn(mMockUser);
    when(mMockUser.getDisplayName()).thenReturn(DISPLAY_NAME);
    when(mMockUser.getPhotoUrl()).thenReturn(new Uri.Builder().build());

    mHandler.startSignIn(mMockAuth, mMockWelcomeBackIdpPrompt, MICROSOFT_PROVIDER);

    ArgumentCaptor<OAuthProvider> providerCaptor = ArgumentCaptor.forClass(OAuthProvider.class);
    verify(mScratchMockAuth).startActivityForSignInWithProvider(eq(mMockWelcomeBackIdpPrompt),
            providerCaptor.capture());
    assertThat(providerCaptor.getValue().getProviderId()).isEqualTo(MICROSOFT_PROVIDER);

    InOrder inOrder = inOrder(mResponseObserver);
    inOrder.verify(mResponseObserver)
            .onChanged(argThat(ResourceMatchers.<IdpResponse>isLoading()));

    ArgumentCaptor<Resource<IdpResponse>> resolveCaptor =
            ArgumentCaptor.forClass(Resource.class);
    inOrder.verify(mResponseObserver).onChanged(resolveCaptor.capture());

    IdpResponse idpResponse = resolveCaptor.getValue().getValue();
    assertThat(idpResponse.getCredentialForLinking()).isNotNull();
}
 
Example #12
Source File: AuthOperationManager.java    From FirebaseUI-Android with Apache License 2.0 5 votes vote down vote up
@NonNull
public Task<AuthResult> safeGenericIdpSignIn(@NonNull final HelperActivityBase activity,
                                             @NonNull final OAuthProvider provider,
                                             @NonNull final FlowParameters flowParameters) {
    return getScratchAuth(flowParameters)
            .startActivityForSignInWithProvider(activity, provider);
}
 
Example #13
Source File: TwitterProviderHandler.java    From capacitor-firebase-auth with MIT License 5 votes vote down vote up
@Override
public void init(CapacitorFirebaseAuth plugin) {
    this.plugin = plugin;

    String languageCode = Config.getString(CapacitorFirebaseAuth.CONFIG_KEY_PREFIX +"languageCode", "en");

    this.provider = OAuthProvider.newBuilder("twitter.com");
    this.provider.addCustomParameter("lang", languageCode);
    this.firebaseAuth = FirebaseAuth.getInstance();
}
 
Example #14
Source File: GenericIdpSignInHandler.java    From FirebaseUI-Android with Apache License 2.0 4 votes vote down vote up
private void handleAnonymousUpgradeFlow(final FirebaseAuth auth,
                                        final HelperActivityBase activity,
                                        final OAuthProvider provider,
                                        final FlowParameters flowParameters) {
    auth.getCurrentUser()
            .startActivityForLinkWithProvider(activity, provider)
            .addOnSuccessListener(
                    new OnSuccessListener<AuthResult>() {
                        @Override
                        public void onSuccess(@NonNull AuthResult authResult) {
                            handleSuccess(provider.getProviderId(),
                                    authResult.getUser(), (OAuthCredential)
                                            authResult.getCredential());
                        }
                    })
            .addOnFailureListener(
                    new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            if (!(e instanceof FirebaseAuthUserCollisionException)) {
                                setResult(Resource.<IdpResponse>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(new OnSuccessListener<List<String>>() {
                                        @Override
                                        public void onSuccess(List<String> providers) {
                                            if (providers.isEmpty()) {
                                                String errorMessage =
                                                        "Unable to complete the linkingflow -" +
                                                                " the user is using " +
                                                                "unsupported providers.";
                                                setResult(Resource.<IdpResponse>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.<IdpResponse>forFailure(
                                                        new FirebaseUiUserCollisionException(
                                                                ErrorCodes.ERROR_GENERIC_IDP_RECOVERABLE_ERROR,
                                                                "Recoverable error.",
                                                                provider.getProviderId(),
                                                                email,
                                                                credential)));
                                            }
                                        }
                                    });
                        }
                    });
}
 
Example #15
Source File: GenericIdpSignInHandler.java    From FirebaseUI-Android with Apache License 2.0 4 votes vote down vote up
protected void handleNormalSignInFlow(final FirebaseAuth auth,
                                      final HelperActivityBase activity,
                                      final OAuthProvider provider) {
    auth.startActivityForSignInWithProvider(activity, provider)
            .addOnSuccessListener(
                    new OnSuccessListener<AuthResult>() {
                        @Override
                        public void onSuccess(@NonNull AuthResult authResult) {
                            handleSuccess(provider.getProviderId(),
                                    authResult.getUser(), (OAuthCredential)
                                            authResult.getCredential());
                        }
                    })
            .addOnFailureListener(
                    new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            if (e instanceof FirebaseAuthException) {
                                FirebaseAuthError error =
                                        FirebaseAuthError.fromException((FirebaseAuthException) e);

                                if (e instanceof FirebaseAuthUserCollisionException) {
                                    FirebaseAuthUserCollisionException collisionException =
                                            (FirebaseAuthUserCollisionException) e;

                                    setResult(Resource.<IdpResponse>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.<IdpResponse>forFailure(
                                            new UserCancellationException()));
                                } else {
                                    setResult(Resource.<IdpResponse>forFailure(e));
                                }
                            } else {
                                setResult(Resource.<IdpResponse>forFailure(e));
                            }
                        }
                    });

}
 
Example #16
Source File: GenericIdpSignInHandlerTest.java    From FirebaseUI-Android with Apache License 2.0 4 votes vote down vote up
@Test
public void testStartSignIn_anonymousUpgradeFlowWithConflict_expectRecoverableLinkingError() {
    setupAnonymousUpgrade();

    AuthCredential credential
            = OAuthProvider.getCredential(MICROSOFT_PROVIDER, ID_TOKEN, ACCESS_TOKEN);
    FirebaseAuthUserCollisionException collisionException
            = new FirebaseAuthUserCollisionException("foo", "bar");
    collisionException.zza(EMAIL).zza(credential);

    when(mMockAuth.getCurrentUser().startActivityForLinkWithProvider(
            any(Activity.class), any(OAuthProvider.class)))
            .thenReturn(AutoCompleteTask.<AuthResult>forFailure(collisionException));

    // Case 2:  Anonymous user trying to link with a provider keyed by an email that already
    // belongs to an existing account
    when(mMockAuth.fetchSignInMethodsForEmail(any(String.class)))
            .thenReturn(AutoCompleteTask.<SignInMethodQueryResult>forSuccess(
                    new FakeSignInMethodQueryResult(Arrays.asList(
                            GoogleAuthProvider.PROVIDER_ID))));

    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.<IdpResponse>isLoading()));

    ArgumentCaptor<Resource<IdpResponse>> resolveCaptor =
            ArgumentCaptor.forClass(Resource.class);
    inOrder.verify(mResponseObserver).onChanged(resolveCaptor.capture());

    FirebaseUiUserCollisionException e =
            (FirebaseUiUserCollisionException) resolveCaptor.getValue().getException();
    assertThat(e.getCredential()).isNotNull();
    assertThat(e.getEmail()).isEqualTo(EMAIL);
}
 
Example #17
Source File: FakeAuthResult.java    From FirebaseUI-Android with Apache License 2.0 4 votes vote down vote up
@Override
public AuthCredential getCredential() {
    return OAuthProvider.getCredential("provider", "foo", "bar");
}