use of com.google.firebase.auth.OAuthProvider in project FirebaseUI-Android by firebase.
the class GenericIdpSignInHandler method handleAnonymousUpgradeFlow.
private void handleAnonymousUpgradeFlow(final FirebaseAuth auth, final HelperActivityBase activity, final OAuthProvider provider, final FlowParameters flowParameters) {
final boolean useEmulator = activity.getAuthUI().isUseEmulator();
auth.getCurrentUser().startActivityForLinkWithProvider(activity, provider).addOnSuccessListener(authResult -> handleSuccess(useEmulator, provider.getProviderId(), authResult.getUser(), (OAuthCredential) authResult.getCredential(), authResult.getAdditionalUserInfo().isNewUser())).addOnFailureListener(e -> {
if (!(e instanceof FirebaseAuthUserCollisionException)) {
setResult(Resource.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(providers -> {
if (providers.isEmpty()) {
String errorMessage = "Unable to complete the linkingflow -" + " the user is using " + "unsupported providers.";
setResult(Resource.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.forFailure(new FirebaseUiUserCollisionException(ErrorCodes.ERROR_GENERIC_IDP_RECOVERABLE_ERROR, "Recoverable error.", provider.getProviderId(), email, credential)));
}
});
});
}
use of com.google.firebase.auth.OAuthProvider in project FirebaseUI-Android by firebase.
the class GenericIdpSignInHandler 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)) {
handleAnonymousUpgradeFlow(auth, activity, provider, flowParameters);
return;
}
handleNormalSignInFlow(auth, activity, provider);
}
use of com.google.firebase.auth.OAuthProvider in project FirebaseUI-Android by firebase.
the class GenericIdpSignInHandlerTest method mockOAuthProvider.
private void mockOAuthProvider(String providerId) {
// TODO(samstern): I wish we did not have to do this but the OAuthProvider() builder
// throws a NPE and we can't fix it due to b/172544960
OAuthProvider mockProvider = mock(OAuthProvider.class);
when(mockProvider.getProviderId()).thenReturn(providerId);
doReturn(mockProvider).when(mHandler).buildOAuthProvider(anyString(), any(FirebaseAuth.class));
}
use of com.google.firebase.auth.OAuthProvider in project FirebaseUI-Android by firebase.
the class GenericIdpSignInHandlerTest method testStartSignIn_normalSignInFlowWithRecoverableError_expectFailure.
@Test
public void testStartSignIn_normalSignInFlowWithRecoverableError_expectFailure() {
AuthCredential credential = OAuthProvider.newCredentialBuilder(MICROSOFT_PROVIDER).setIdToken(ID_TOKEN).setAccessToken(ACCESS_TOKEN).build();
FirebaseAuthUserCollisionException collisionException = new FirebaseAuthUserCollisionException("foo", "bar");
collisionException.zzb(EMAIL).zza(credential);
when(mMockAuth.startActivityForSignInWithProvider(any(Activity.class), any(OAuthProvider.class))).thenReturn(AutoCompleteTask.forFailure(collisionException));
mockOAuthProvider(MICROSOFT_PROVIDER);
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.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);
}
use of com.google.firebase.auth.OAuthProvider in project FirebaseUI-Android by firebase.
the class GenericIdpSignInHandlerTest method testStartSignIn_normalSignInFlow_expectSuccess.
@Test
public void testStartSignIn_normalSignInFlow_expectSuccess() {
mHandler.getOperation().observeForever(mResponseObserver);
when(mMockAuth.startActivityForSignInWithProvider(any(Activity.class), any(OAuthProvider.class))).thenReturn(AutoCompleteTask.forSuccess(FakeAuthResult.INSTANCE));
mockOAuthProvider(MICROSOFT_PROVIDER);
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);
HashMap<String, String> customArgs = (HashMap<String, String>) mHandler.getArguments().getParams().getSerializable(ExtraConstants.GENERIC_OAUTH_CUSTOM_PARAMETERS);
assertThat(customArgs).isNotNull();
assertThat(customArgs).hasSize(1);
InOrder inOrder = inOrder(mResponseObserver);
inOrder.verify(mResponseObserver).onChanged(argThat(ResourceMatchers.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();
}
Aggregations