use of com.firebase.ui.auth.util.data.EmailLinkPersistenceManager in project FirebaseUI-Android by firebase.
the class EmailActivityTest method testOnCreate_emailLinkLinkingFlow_expectSendEmailLinkFlowStarted.
@Test
public void testOnCreate_emailLinkLinkingFlow_expectSendEmailLinkFlowStarted() {
// This is normally done by EmailLinkSendEmailHandler, saving the IdpResponse is done
// in EmailActivity but it will not be saved if we haven't previously set the email
EmailLinkPersistenceManager.getInstance().saveEmail(ApplicationProvider.getApplicationContext(), EMAIL, TestConstants.SESSION_ID, TestConstants.UID);
EmailActivity emailActivity = createActivity(AuthUI.EMAIL_LINK_PROVIDER, true, false);
EmailLinkFragment fragment = (EmailLinkFragment) emailActivity.getSupportFragmentManager().findFragmentByTag(EmailLinkFragment.TAG);
assertThat(fragment).isNotNull();
EmailLinkPersistenceManager persistenceManager = EmailLinkPersistenceManager.getInstance();
IdpResponse response = persistenceManager.retrieveSessionRecord(ApplicationProvider.getApplicationContext()).getIdpResponseForLinking();
assertThat(response.getProviderType()).isEqualTo(GoogleAuthProvider.PROVIDER_ID);
assertThat(response.getEmail()).isEqualTo(EMAIL);
assertThat(response.getIdpToken()).isEqualTo(ID_TOKEN);
assertThat(response.getIdpSecret()).isEqualTo(SECRET);
}
use of com.firebase.ui.auth.util.data.EmailLinkPersistenceManager in project FirebaseUI-Android by firebase.
the class EmailLinkSignInHandler method startSignIn.
public void startSignIn() {
setResult(Resource.forLoading());
String link = getArguments().emailLink;
if (!getAuth().isSignInWithEmailLink(link)) {
setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.INVALID_EMAIL_LINK_ERROR)));
return;
}
final EmailLinkPersistenceManager persistenceManager = EmailLinkPersistenceManager.getInstance();
SessionRecord sessionRecord = persistenceManager.retrieveSessionRecord(getApplication());
EmailLinkParser parser = new EmailLinkParser(link);
String sessionIdFromLink = parser.getSessionId();
String anonymousUserIdFromLink = parser.getAnonymousUserId();
String oobCodeFromLink = parser.getOobCode();
String providerIdFromLink = parser.getProviderId();
boolean forceSameDevice = parser.getForceSameDeviceBit();
if (isDifferentDeviceFlow(sessionRecord, sessionIdFromLink)) {
if (TextUtils.isEmpty(sessionIdFromLink)) {
// There should always be a valid session ID in the link
setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.INVALID_EMAIL_LINK_ERROR)));
return;
}
if (forceSameDevice || !TextUtils.isEmpty(anonymousUserIdFromLink)) {
// In both cases, the link was meant to be completed on the same device.
// For anonymous user upgrade, we don't support the cross device flow.
setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.EMAIL_LINK_WRONG_DEVICE_ERROR)));
return;
}
// If we have no SessionRecord/there is a session ID mismatch, this means that we were
// not the ones to send the link. The only way forward is to prompt the user for their
// email before continuing the flow. We should only do that after validating the link.
determineDifferentDeviceErrorFlowAndFinish(oobCodeFromLink, providerIdFromLink);
return;
}
if (anonymousUserIdFromLink != null) {
// Same device flow, need to ensure uids match
if (getAuth().getCurrentUser() == null || (getAuth().getCurrentUser().isAnonymous() && !anonymousUserIdFromLink.equals(getAuth().getCurrentUser().getUid()))) {
setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.EMAIL_LINK_DIFFERENT_ANONYMOUS_USER_ERROR)));
return;
}
}
finishSignIn(sessionRecord);
}
use of com.firebase.ui.auth.util.data.EmailLinkPersistenceManager in project FirebaseUI-Android by firebase.
the class EmailLinkSignInHandler method handleLinkingFlow.
private void handleLinkingFlow(final AuthOperationManager authOperationManager, final EmailLinkPersistenceManager persistenceManager, final IdpResponse response, final String link) {
final AuthCredential storedCredentialForLink = ProviderUtils.getAuthCredential(response);
final AuthCredential emailLinkCredential = EmailAuthProvider.getCredentialWithLink(response.getEmail(), link);
if (authOperationManager.canUpgradeAnonymous(getAuth(), getArguments())) {
authOperationManager.safeLink(emailLinkCredential, storedCredentialForLink, getArguments()).addOnCompleteListener(task -> {
persistenceManager.clearAllData(getApplication());
if (task.isSuccessful()) {
handleMergeFailure(storedCredentialForLink);
} else {
setResult(Resource.forFailure(task.getException()));
}
});
} else {
getAuth().signInWithCredential(emailLinkCredential).continueWithTask(task -> {
persistenceManager.clearAllData(getApplication());
if (!task.isSuccessful()) {
return task;
}
return task.getResult().getUser().linkWithCredential(storedCredentialForLink).continueWithTask(new ProfileMerger(response)).addOnFailureListener(new TaskFailureLogger(TAG, "linkWithCredential+merge failed."));
}).addOnSuccessListener(authResult -> {
FirebaseUser user = authResult.getUser();
IdpResponse response1 = new IdpResponse.Builder(new User.Builder(EmailAuthProvider.EMAIL_LINK_SIGN_IN_METHOD, user.getEmail()).setName(user.getDisplayName()).setPhotoUri(user.getPhotoUrl()).build()).build();
handleSuccess(response1, authResult);
}).addOnFailureListener(e -> setResult(Resource.forFailure(e)));
}
}
use of com.firebase.ui.auth.util.data.EmailLinkPersistenceManager in project FirebaseUI-Android by firebase.
the class EmailLinkSignInHandler method finishSignIn.
private void finishSignIn(@NonNull String email, @Nullable IdpResponse response) {
if (TextUtils.isEmpty(email)) {
setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.EMAIL_MISMATCH_ERROR)));
return;
}
final AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
final EmailLinkPersistenceManager persistenceManager = EmailLinkPersistenceManager.getInstance();
String link = getArguments().emailLink;
if (response == null) {
handleNormalFlow(authOperationManager, persistenceManager, email, link);
} else {
handleLinkingFlow(authOperationManager, persistenceManager, response, link);
}
}
use of com.firebase.ui.auth.util.data.EmailLinkPersistenceManager 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));
}
});
}
Aggregations