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

Commit f6a26b4f authored by Fahim M. Choudhury's avatar Fahim M. Choudhury
Browse files

Merge branch '3485-main-openid_auth' into 'main'

feat: update Murena account to use OAuth on SSO migration

See merge request !175
parents e30caf20 0da9c738
Loading
Loading
Loading
Loading
Loading
+26 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.fsck.k9.activity.setup.accountmanager;


import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import android.accounts.AccountManager;
@@ -120,13 +121,20 @@ public class EeloAccountCreator {
                continue;
            }

            @Nullable String authState =
                accountManager.getUserData(openIdAccount, AccountManagerConstants.KEY_AUTH_STATE);

            var existenceAccount = accounts.stream()
                .filter(account -> emailId.equalsIgnoreCase(account.getEmail()))
                .peek(account -> updateAccountNameIfMissing(context, emailId, account))
                .peek(account -> {
                    if (authState != null) {
                        updateAuthStateIfChanged(authState, account);
                    }
                })
                .findAny();

            if (!existenceAccount.isPresent()) {
                String authState = accountManager.getUserData(openIdAccount, AccountManagerConstants.KEY_AUTH_STATE);
                createAccount(context, emailId, "", authState, accountType);
                continue;
            }
@@ -147,6 +155,23 @@ public class EeloAccountCreator {
        }
    }

    private static void updateAuthStateIfChanged(@NonNull String authState, @NonNull Account account) {
        // Don't update authState if it's not a Murena account
        if (!OsAccountManagerUtil.INSTANCE.isMurenaEmailAccount(account)) return;

        if (!Objects.equals(account.getOAuthState(), authState)) {
            account.setOAuthState(authState);

            // Update auth type in case it was a migration.
            ServerSettings incoming = account.getIncomingServerSettings();
            ServerSettings outgoing = account.getOutgoingServerSettings();
            account.setIncomingServerSettings(incoming.newAuthenticationType(AuthType.XOAUTH2));
            account.setOutgoingServerSettings(outgoing.newAuthenticationType(AuthType.XOAUTH2));

            Preferences.getPreferences().saveAccount(account);
        }
    }

    private static void updatePasswordIfChanged(Account account, String password) {
        // Don't update password if its not a murena account
        if (!OsAccountManagerUtil.INSTANCE.isMurenaEmailAccount(account)) return;