Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0eadf3c8 authored by Mohammed Althaf T's avatar Mohammed Althaf T 😊
Browse files

feat: Update password on change

parent 5e151e26
Loading
Loading
Loading
Loading
Loading
+47 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import java.util.concurrent.Future;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

import android.accounts.AccountManager;
import android.content.Context;
import android.os.Process;
import android.os.SystemClock;
@@ -28,6 +29,7 @@ import android.os.SystemClock;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.work.ListenableWorker.Result;
import app.k9mail.core.android.common.accountmanager.AccountManagerConstants;
import com.fsck.k9.Account;
import com.fsck.k9.Account.DeletePolicy;
import com.fsck.k9.Account.Expunge;
@@ -645,6 +647,11 @@ public class MessagingController {
            migrateAccountToOAuth(account);
        }

        if (updatePassword(account)) {
            notificationController.clearAuthenticationErrorNotification(account, true);
            return;
        }

        notificationController.showAuthenticationErrorNotification(account, incoming);
    }

@@ -653,11 +660,51 @@ public class MessagingController {
            migrateAccountToOAuth(account);
        }

        if (updatePassword(account)) {
            notificationController.clearAuthenticationErrorNotification(account, true);
            return;
        }

        if (shouldShowErrorNotification(account, exception)) {
            notificationController.showAuthenticationErrorNotification(account, incoming);
        }
    }

    private boolean updatePassword(Account account) {
        AccountManager accountManager = AccountManager.get(context);

        for (String accountType : AccountManagerConstants.INSTANCE.getALL_ACCOUNT_TYPES()) {
            for (android.accounts.Account deviceAccount : accountManager.getAccountsByType(accountType)) {
                // Check if the account email matches with account manager
                String emailId = accountManager.getUserData(deviceAccount,
                        AccountManagerConstants.ACCOUNT_EMAIL_ADDRESS_KEY);
                if (emailId != null && !emailId.equalsIgnoreCase(account.getEmail())) {
                    continue;
                }

                ServerSettings incoming = account.getIncomingServerSettings();
                ServerSettings outgoing = account.getOutgoingServerSettings();

                // Only set if its plain password
                if (incoming.authenticationType != AuthType.PLAIN
                    && outgoing.authenticationType != AuthType.PLAIN) {
                    continue;
                }

                // Update incoming and outgoing server settings
                String password = accountManager.getPassword(deviceAccount);
                if (password != null && (!password.equals(incoming.password) || !password.equals(outgoing.password))) {
                    account.setIncomingServerSettings(incoming.newPassword(password));
                    account.setOutgoingServerSettings(outgoing.newPassword(password));
                    preferences.saveAccount(account);
                    return true;
                }
            }
        }

        return false;
    }

    // on network switch, sometime mail app failed to authenticate with auth2 accounts, but other operations works perfectly.
    // It is ignorable, because oauth2 authentication is handled via accountManager
    private boolean shouldShowErrorNotification(Account account, Exception exception) {
+22 −1
Original line number Diff line number Diff line
@@ -139,6 +139,26 @@ public class EeloAccountCreator {
        }
    }

    private static void updatePasswordIfChanged(Account account, String password) {
        ServerSettings incoming = account.getIncomingServerSettings();
        ServerSettings outgoing = account.getOutgoingServerSettings();

        // Only set if its plain password
        if (incoming.authenticationType != AuthType.PLAIN
            && outgoing.authenticationType != AuthType.PLAIN) {
            return;
        }

        // Update incoming and outgoing server settings
        if (!password.equals(incoming.password) || !password.equals(outgoing.password)) {
            account.setIncomingServerSettings(incoming.newPassword(password));
            account.setOutgoingServerSettings(outgoing.newPassword(password));

            // Save account since its updated
            Preferences.getPreferences().saveAccount(account);
        }
    }

    private static boolean isInvalidEmail(String emailId) {
        return emailId == null || !emailId.contains("@");
    }
@@ -163,12 +183,13 @@ public class EeloAccountCreator {
                continue;
            }

            String password = accountManager.getPassword(eeloAccount);
            var existenceAccount = accounts.stream()
                .filter(account -> emailId.equalsIgnoreCase(account.getEmail()))
                .peek(account -> updatePasswordIfChanged(account, password))
                .findAny();

            if (!existenceAccount.isPresent()) {
                String password = accountManager.getPassword(eeloAccount);
                createAccount(context, emailId, password, null, AccountManagerConstants.EELO_ACCOUNT_TYPE);
                continue;
            }