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

Commit 117676c8 authored by Fahim Salam Chowdhury's avatar Fahim Salam Chowdhury 👽
Browse files

Merge branch '6287-Feature_openid_support_for_murena_account' into 'main'

6287-Feature_openid_support_for_murena_account

See merge request !150
parents 54ef44b0 67ff8c39
Loading
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -24,13 +24,14 @@ object AccountManagerConstants {
    const val MAIL_CONTENT_AUTHORITY = "foundation.e.mail.provider.AppContentProvider"
    const val AUTH_TOKEN_TYPE = "oauth2-access-token"
    const val KEY_AUTH_STATE = "auth_state"
    const val USERNAME_HINT = "userNameHint"

    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 IGNORE_ACCOUNT_SETUP = "ignore_account_setup"

    val OPENID_ACCOUNT_TYPES = listOf(GOOGLE_ACCOUNT_TYPE, YAHOO_ACCOUNT_TYPE)
    val OPENID_ACCOUNT_TYPES = listOf(EELO_ACCOUNT_TYPE, GOOGLE_ACCOUNT_TYPE, YAHOO_ACCOUNT_TYPE)
    val ALL_ACCOUNT_TYPES = listOf(EELO_ACCOUNT_TYPE, GOOGLE_ACCOUNT_TYPE, YAHOO_ACCOUNT_TYPE)

    fun getOpenIdAccountTypeByHostName(hostname: String): String? {
+9 −2
Original line number Diff line number Diff line
@@ -118,9 +118,16 @@ object EeloAccountHelper {
        val openIdAccounts = accountManager.getAccountsByType(accountType)
        for (openIdAccount in openIdAccounts) {
            val emailId = accountManager.getUserData(openIdAccount, AccountManagerConstants.ACCOUNT_EMAIL_ADDRESS_KEY)
            if (email.equals(emailId, ignoreCase = true)) {
                return openIdAccount
            if (!email.equals(emailId, ignoreCase = true)) {
                continue
            }

            val authState = accountManager.getUserData(openIdAccount, AccountManagerConstants.KEY_AUTH_STATE)
            if (authState.isNullOrEmpty()) {
                return null
            }

            return openIdAccount
        }

        return null
+1 −0
Original line number Diff line number Diff line
@@ -234,6 +234,7 @@ class AccountSetupBasics : K9Activity() {
        val options = Bundle()
        options.putString(AccountManagerConstants.OPEN_APP_PACKAGE_AFTER_AUTH, packageName)
        options.putString(AccountManagerConstants.OPEN_APP_ACTIVITY_AFTER_AUTH, MessageList::class.java.name)
        options.putString(AccountManagerConstants.USERNAME_HINT, emailView.text?.toString())
        osAccountManager.addAccount(accountType, null, null, options, this, null, null)
    }

+47 −12
Original line number Diff line number Diff line
@@ -105,6 +105,10 @@ public class EeloAccountCreator {
                continue;
            }

            if (!isOpenIdAccount(accountManager, openIdAccount)) {
                continue;
            }

            var existenceAccount = accounts.stream()
                    .filter(account -> emailId.equalsIgnoreCase(account.getEmail()))
                    .peek(account -> updateAccountNameIfMissing(context, emailId, account))
@@ -112,7 +116,7 @@ public class EeloAccountCreator {

            if (!existenceAccount.isPresent()) {
                String authState = accountManager.getUserData(openIdAccount, AccountManagerConstants.KEY_AUTH_STATE);
                createAccount(context, emailId, "", authState);
                createAccount(context, emailId, "", authState, accountType);
                continue;
            }

@@ -120,6 +124,10 @@ public class EeloAccountCreator {
        }
    }

    private static boolean isOpenIdAccount(AccountManager accountManager, android.accounts.Account account) {
        final String authState = accountManager.getUserData(account, AccountManagerConstants.KEY_AUTH_STATE);
        return authState != null && !authState.trim().isEmpty();
    }
    private static void updateAccountNameIfMissing(@NonNull Context context, String emailId, Account account) {
        if (account.getName() == null) { // we need to fix an old bug
            account.setName(emailId);
@@ -146,13 +154,17 @@ public class EeloAccountCreator {
                continue;
            }

            if (isOpenIdAccount(accountManager, eeloAccount)) {
                continue;
            }

            var existenceAccount = accounts.stream()
                    .filter(account -> emailId.equalsIgnoreCase(account.getEmail()))
                    .findAny();

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

@@ -166,7 +178,7 @@ public class EeloAccountCreator {
                .forEach(account -> accountRemover.removeAccountAsync(account.getUuid()));
    }

    private static void createAccount(Context context, String emailId, String password, @Nullable String authState) {
    private static void createAccount(Context context, String emailId, String password, @Nullable String authState, @NonNull String accountType) {
        Preferences preferences = Preferences.getPreferences();

        Account account = preferences.newAccount();
@@ -181,18 +193,24 @@ public class EeloAccountCreator {
            connectionSettings = MailAutoConfigDiscovery.retrieveConfigFromApi(emailId);
        }
        // providers.xml doesn't have the connection details & can't retrieve details from api
        // & it is google account (authState should not be null), meaning custom domain for google account is used.
        // In this case, provide default gmail configuration.
        // In this case, fall back to default configuration.
        if (connectionSettings == null && authState != null) {
            connectionSettings = providersDefaultGoogleAccountDiscover(emailId);
            connectionSettings = providersDefaultOpenIdAccountDiscover(emailId, accountType);
        }

        if (connectionSettings == null) {
            Timber.e("Error while trying to initialise account configuration.");
            return;
        }
        ServerSettings incomingSettings = connectionSettings.getIncoming().newPassword(password);
        account.setIncomingServerSettings(incomingSettings);
        ServerSettings outgoingSettings = connectionSettings.getOutgoing().newPassword(password);

        if (authState != null && !authState.trim().isEmpty()) {
            incomingSettings = incomingSettings.newAuthenticationType(AuthType.XOAUTH2);
            outgoingSettings = outgoingSettings.newAuthenticationType(AuthType.XOAUTH2);
        }

        account.setIncomingServerSettings(incomingSettings);
        account.setOutgoingServerSettings(outgoingSettings);

        account.setOAuthState(authState);
@@ -240,11 +258,28 @@ public class EeloAccountCreator {
        );
    }

    private static ConnectionSettings providersDefaultGoogleAccountDiscover(String email) {
    private static ConnectionSettings providersDefaultOpenIdAccountDiscover(@NonNull String email, @NonNull String accountType) {
        String incomingHost = "mail.ecloud.global";
        String outgoingHost = incomingHost;
        int outgoingPort = 587;
        ConnectionSecurity outgoingConnectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED;

        if (accountType.equals(AccountManagerConstants.GOOGLE_ACCOUNT_TYPE)) {
            incomingHost = "imap.gmail.com";
            outgoingHost = "smtp.gmail.com";
            outgoingPort = 465;
            outgoingConnectionSecurity =  ConnectionSecurity.SSL_TLS_REQUIRED;
        } else if (accountType.equals(AccountManagerConstants.YAHOO_ACCOUNT_TYPE)) {
            incomingHost = "imap.mail.yahoo.com";
            outgoingHost = "smtp.mail.yahoo.com";
            outgoingPort = 465;
            outgoingConnectionSecurity =  ConnectionSecurity.SSL_TLS_REQUIRED;
        }

        return new ConnectionSettings(
                new ServerSettings(
                        Protocols.IMAP,
                        "imap.gmail.com",
                        incomingHost,
                        993,
                        ConnectionSecurity.SSL_TLS_REQUIRED,
                        AuthType.XOAUTH2,
@@ -254,9 +289,9 @@ public class EeloAccountCreator {
                ),
                new ServerSettings(
                        Protocols.SMTP,
                        "smtp.gmail.com",
                        465,
                        ConnectionSecurity.SSL_TLS_REQUIRED,
                        outgoingHost,
                        outgoingPort,
                        outgoingConnectionSecurity,
                        AuthType.XOAUTH2,
                        email,
                        null,