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

Commit 12ec890e authored by Mohammed Althaf T's avatar Mohammed Althaf T 😊 Committed by Nishith Khanna
Browse files

feat(account): Identify Murena email and web hosts

parent 79ff3d9d
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -25,11 +25,11 @@ object AccountManagerConstants {
    const val MAIL_CONTENT_AUTHORITY = "foundation.e.mail.provider.AppContentProvider"
    const val KEY_AUTH_STATE = "auth_state"
    const val AUTH_TOKEN_TYPE = "oauth2-access-token"

    const val OPEN_APP_PACKAGE_AFTER_AUTH = "open_app_package_after_auth"
    const val OPEN_APP_ACTIVITY_AFTER_AUTH = "open_app_activity_after_auth"
    const val TO_OPEN_AFTER_AUTH_ACTIVITY = "com.fsck.k9.activity.MessageList"
    const val USERNAME_HINT = "userNameHint"
    const val KEY_OC_BASE_URL = "oc_base_url"

    val ACCOUNT_TYPES = listOf(EELO_ACCOUNT_TYPE, GOOGLE_ACCOUNT_TYPE, YAHOO_ACCOUNT_TYPE)

+19 −0
Original line number Diff line number Diff line
@@ -353,4 +353,23 @@ object AccountManagerHelper {

        return true
    }

    fun isMurenaEmailAccount(account: kAccount): Boolean {
        val incoming = account.incomingServerSettings
        val outgoing = account.outgoingServerSettings

        if (incoming.host == null || outgoing.host == null) {
            return false
        }

        return isMurenaMailHost(incoming.host!!) && isMurenaMailHost(outgoing.host!!)
    }

    private fun isMurenaMailHost(host: String): Boolean {
        return "mail.ecloud.global" == host
    }

    fun isMurenaWebHostUrl(host: String): Boolean {
        return host.contains("https://murena.io") || host.contains("http://murena.io")
    }
}
+29 −29
Original line number Diff line number Diff line
@@ -690,43 +690,43 @@ public class MessagingController implements MessagingControllerRegistry, Messagi
    }

    public boolean updatePassword(LegacyAccount account) {
        boolean isUpdated = false;
        if (!AccountManagerHelper.INSTANCE.isMurenaEmailAccount(account)) return false;
        AccountManager accountManager = AccountManager.get(context);
        android.accounts.Account[] eeloAccounts =
            accountManager.getAccountsByType(AccountManagerConstants.EELO_ACCOUNT_TYPE);

        for (String accountType : AccountManagerConstants.getAccountTypes()) {
            for (android.accounts.Account kAccount : accountManager.getAccountsByType(accountType)) {
                String emailId = accountManager.getUserData(kAccount, AccountManagerConstants.ACCOUNT_EMAIL_ADDRESS_KEY);
                if (emailId == null || emailId.isBlank() || !emailId.contains("@")) {
        for (android.accounts.Account deviceAccount : eeloAccounts) {
            String hostUrl = accountManager.getUserData(deviceAccount, AccountManagerConstants.KEY_OC_BASE_URL);
            if (hostUrl == null || !AccountManagerHelper.INSTANCE.isMurenaWebHostUrl(hostUrl)) {
                continue;
            }

                if (emailId.equalsIgnoreCase(account.getEmail())) {
                    String password = accountManager.getPassword(kAccount);

                    // Update incoming and outgoing server settings
                    ServerSettings incoming = account.getIncomingServerSettings();
                    if (incoming.authenticationType == AuthType.PLAIN && incoming.password != null
                        && !incoming.password.isEmpty() && !password.equals(incoming.password)) {
                        account.setIncomingServerSettings(incoming.newPassword(password));
                        isUpdated = true;
            // 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();
                    if (outgoing.authenticationType == AuthType.PLAIN && outgoing.password != null
                        && !outgoing.password.isEmpty() && !password.equals(outgoing.password)) {
                        account.setOutgoingServerSettings(outgoing.newPassword(password));
                        isUpdated = true;

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

                    // Save account if updated
                    if (isUpdated) {
            // 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 isUpdated;
        return false;
    }

    private void migrateAccountToOAuth(LegacyAccount account) {
+10 −0
Original line number Diff line number Diff line
@@ -155,6 +155,13 @@ internal class EeloAccountCreator(context: Context) : KoinComponent {
                continue
            }

            val hostUrl = accountManager.getUserData(account, AccountManagerConstants.KEY_OC_BASE_URL)
            // We are trying to add account that are not murena.
            if (accountType == AccountManagerConstants.EELO_ACCOUNT_TYPE &&
                (hostUrl == null || !AccountManagerHelper.isMurenaWebHostUrl(hostUrl))) {
                continue
            }

            val password = accountManager.getPassword(account)
            val existenceAccount = accounts.stream()
                .filter { acc: kAccount -> emailId.equals(acc.email, ignoreCase = true) }
@@ -178,6 +185,9 @@ internal class EeloAccountCreator(context: Context) : KoinComponent {
    }

    private fun updatePasswordIfChanged(account: kAccount, auth: String) {
        // Don't update password if its not a murena account
        if (!AccountManagerHelper.isMurenaEmailAccount(account)) return;

        // Update incoming and outgoing server settings
        val incoming: ServerSettings = account.incomingServerSettings
        if (incoming.authenticationType == AuthType.PLAIN &&