Search in sources :

Example 1 with EmailLinkPersistenceManager

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);
}
Also used : EmailLinkPersistenceManager(com.firebase.ui.auth.util.data.EmailLinkPersistenceManager) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 2 with EmailLinkPersistenceManager

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);
}
Also used : EmailLinkPersistenceManager(com.firebase.ui.auth.util.data.EmailLinkPersistenceManager) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) EmailLinkParser(com.firebase.ui.auth.util.data.EmailLinkParser) SessionRecord(com.firebase.ui.auth.util.data.EmailLinkPersistenceManager.SessionRecord)

Example 3 with EmailLinkPersistenceManager

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)));
    }
}
Also used : AuthCredential(com.google.firebase.auth.AuthCredential) ActionCodeResult(com.google.firebase.auth.ActionCodeResult) ProviderUtils(com.firebase.ui.auth.util.data.ProviderUtils) ProfileMerger(com.firebase.ui.auth.data.remote.ProfileMerger) EMAIL_LINK_PROVIDER(com.firebase.ui.auth.AuthUI.EMAIL_LINK_PROVIDER) NonNull(androidx.annotation.NonNull) User(com.firebase.ui.auth.data.model.User) EmailLinkPersistenceManager(com.firebase.ui.auth.util.data.EmailLinkPersistenceManager) Resource(com.firebase.ui.auth.data.model.Resource) EmailLinkParser(com.firebase.ui.auth.util.data.EmailLinkParser) TaskFailureLogger(com.firebase.ui.auth.util.data.TaskFailureLogger) Task(com.google.android.gms.tasks.Task) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) Continuation(com.google.android.gms.tasks.Continuation) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) SignInViewModelBase(com.firebase.ui.auth.viewmodel.SignInViewModelBase) FirebaseUser(com.google.firebase.auth.FirebaseUser) SessionRecord(com.firebase.ui.auth.util.data.EmailLinkPersistenceManager.SessionRecord) TextUtils(android.text.TextUtils) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) AuthCredential(com.google.firebase.auth.AuthCredential) Nullable(androidx.annotation.Nullable) EmailAuthProvider(com.google.firebase.auth.EmailAuthProvider) Application(android.app.Application) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) AuthResult(com.google.firebase.auth.AuthResult) ErrorCodes(com.firebase.ui.auth.ErrorCodes) IdpResponse(com.firebase.ui.auth.IdpResponse) TaskFailureLogger(com.firebase.ui.auth.util.data.TaskFailureLogger) ProfileMerger(com.firebase.ui.auth.data.remote.ProfileMerger) FirebaseUser(com.google.firebase.auth.FirebaseUser) IdpResponse(com.firebase.ui.auth.IdpResponse)

Example 4 with EmailLinkPersistenceManager

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);
    }
}
Also used : AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) EmailLinkPersistenceManager(com.firebase.ui.auth.util.data.EmailLinkPersistenceManager) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException)

Example 5 with EmailLinkPersistenceManager

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));
        }
    });
}
Also used : AuthCredential(com.google.firebase.auth.AuthCredential) ActionCodeResult(com.google.firebase.auth.ActionCodeResult) ProviderUtils(com.firebase.ui.auth.util.data.ProviderUtils) ProfileMerger(com.firebase.ui.auth.data.remote.ProfileMerger) EMAIL_LINK_PROVIDER(com.firebase.ui.auth.AuthUI.EMAIL_LINK_PROVIDER) NonNull(androidx.annotation.NonNull) User(com.firebase.ui.auth.data.model.User) EmailLinkPersistenceManager(com.firebase.ui.auth.util.data.EmailLinkPersistenceManager) Resource(com.firebase.ui.auth.data.model.Resource) EmailLinkParser(com.firebase.ui.auth.util.data.EmailLinkParser) TaskFailureLogger(com.firebase.ui.auth.util.data.TaskFailureLogger) Task(com.google.android.gms.tasks.Task) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) Continuation(com.google.android.gms.tasks.Continuation) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) SignInViewModelBase(com.firebase.ui.auth.viewmodel.SignInViewModelBase) FirebaseUser(com.google.firebase.auth.FirebaseUser) SessionRecord(com.firebase.ui.auth.util.data.EmailLinkPersistenceManager.SessionRecord) TextUtils(android.text.TextUtils) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) AuthCredential(com.google.firebase.auth.AuthCredential) Nullable(androidx.annotation.Nullable) EmailAuthProvider(com.google.firebase.auth.EmailAuthProvider) Application(android.app.Application) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) AuthResult(com.google.firebase.auth.AuthResult) ErrorCodes(com.firebase.ui.auth.ErrorCodes) IdpResponse(com.firebase.ui.auth.IdpResponse) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseUser(com.google.firebase.auth.FirebaseUser) IdpResponse(com.firebase.ui.auth.IdpResponse)

Aggregations

EmailLinkPersistenceManager (com.firebase.ui.auth.util.data.EmailLinkPersistenceManager)5 FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)4 IdpResponse (com.firebase.ui.auth.IdpResponse)3 AuthOperationManager (com.firebase.ui.auth.util.data.AuthOperationManager)3 EmailLinkParser (com.firebase.ui.auth.util.data.EmailLinkParser)3 SessionRecord (com.firebase.ui.auth.util.data.EmailLinkPersistenceManager.SessionRecord)3 Application (android.app.Application)2 TextUtils (android.text.TextUtils)2 NonNull (androidx.annotation.NonNull)2 Nullable (androidx.annotation.Nullable)2 EMAIL_LINK_PROVIDER (com.firebase.ui.auth.AuthUI.EMAIL_LINK_PROVIDER)2 ErrorCodes (com.firebase.ui.auth.ErrorCodes)2 Resource (com.firebase.ui.auth.data.model.Resource)2 User (com.firebase.ui.auth.data.model.User)2 ProfileMerger (com.firebase.ui.auth.data.remote.ProfileMerger)2 ProviderUtils (com.firebase.ui.auth.util.data.ProviderUtils)2 TaskFailureLogger (com.firebase.ui.auth.util.data.TaskFailureLogger)2 SignInViewModelBase (com.firebase.ui.auth.viewmodel.SignInViewModelBase)2 Continuation (com.google.android.gms.tasks.Continuation)2 OnCompleteListener (com.google.android.gms.tasks.OnCompleteListener)2