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());
}
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);
}
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));
}
});
}
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()));
}
});
}
}
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();
}
Aggregations