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

Commit 78e12f78 authored by Nihar Thakkar's avatar Nihar Thakkar
Browse files

Implement SSO for /e/ accounts

parent 6edccfe9
Loading
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ import io.eelo.mail.mail.AuthenticationFailedException;
import io.eelo.mail.mail.OAuth2NeedUserPromptException;
import io.eelo.mail.mail.oauth.OAuth2TokenProvider;

import static io.eelo.mail.account.OAuthConstants.ACCOUNT_EMAIL_ID_KEY;
import static io.eelo.mail.account.OAuthConstants.ACCOUNT_USER_NAME_KEY;
import static io.eelo.mail.account.OAuthConstants.AUTH_TOKEN_TYPE;
import static io.eelo.mail.account.OAuthConstants.EELO_ACCOUNT_TYPE;
import static io.eelo.mail.account.OAuthConstants.GOOGLE_ACCOUNT_TYPE;
@@ -88,7 +88,7 @@ public class K9OAuth2TokenProvider extends OAuth2TokenProvider {
        if (accounts.size() > 0) {
            for (Account account : accounts) {
                String accountEmailId = accountManager.getUserData(account,
                        ACCOUNT_EMAIL_ID_KEY);
                        ACCOUNT_USER_NAME_KEY);
                if (emailId.equals(accountEmailId)) {
                    final CountDownLatch countDownLatch = new CountDownLatch(1);
                    accountManager.getAuthToken(account, AUTH_TOKEN_TYPE, new Bundle(),
+1 −1
Original line number Diff line number Diff line
@@ -3,6 +3,6 @@ package io.eelo.mail.account;
public class OAuthConstants {
    public static final String EELO_ACCOUNT_TYPE = "bitfire.at.davdroid.eelo";
    public static final String GOOGLE_ACCOUNT_TYPE = "bitfire.at.davdroid.google";
    public static final String ACCOUNT_EMAIL_ID_KEY = "email_id";
    public static final String ACCOUNT_USER_NAME_KEY = "user_name";
    public static final String AUTH_TOKEN_TYPE = "oauth2-access-token";
}
+6 −4
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ import io.eelo.mail.K9;
import io.eelo.mail.Preferences;
import io.eelo.mail.activity.misc.ExtendedAsyncTask;
import io.eelo.mail.activity.misc.NonConfigurationInstance;
import io.eelo.mail.activity.setup.EeloAccountCreator;
import io.eelo.mail.activity.setup.GoogleAccountCreator;
import io.eelo.mail.controller.MessagingController;
import io.eelo.mail.mail.AuthType;
@@ -95,7 +96,7 @@ import io.eelo.mail.mailstore.StorageManager;
import io.eelo.mail.preferences.SettingsExporter;
import de.cketti.library.changelog.ChangeLog;

import static io.eelo.mail.account.OAuthConstants.ACCOUNT_EMAIL_ID_KEY;
import static io.eelo.mail.account.OAuthConstants.ACCOUNT_USER_NAME_KEY;
import static io.eelo.mail.account.OAuthConstants.EELO_ACCOUNT_TYPE;
import static io.eelo.mail.account.OAuthConstants.GOOGLE_ACCOUNT_TYPE;

@@ -479,13 +480,14 @@ public class Accounts extends K9ListActivity implements OnItemClickListener
                {
                    for (android.accounts.Account eeloAccount : eeloAccounts) {
                        String emailId = accountManager.getUserData(eeloAccount,
                                ACCOUNT_EMAIL_ID_KEY);
                        //TODO Add /e/ accounts
                                ACCOUNT_USER_NAME_KEY);
                        String password = accountManager.getPassword(eeloAccount);
                        EeloAccountCreator.createAccount(this, emailId, password);
                    }

                    for (android.accounts.Account googleAccount : googleAccounts) {
                        String emailId = accountManager.getUserData(googleAccount,
                                ACCOUNT_EMAIL_ID_KEY);
                                ACCOUNT_USER_NAME_KEY);
                        GoogleAccountCreator.createAccount(this, emailId);
                    }

+42 −0
Original line number Diff line number Diff line
package io.eelo.mail.activity.setup;

import android.content.Context;

import io.eelo.mail.Account;
import io.eelo.mail.K9;
import io.eelo.mail.Preferences;
import io.eelo.mail.controller.MessagingController;

public class EeloAccountCreator {
    public static void createAccount(Context context, String emailId, String password) {
        Preferences preferences = Preferences.getPreferences(context);

        AccountConfigImpl accountConfig = new AccountConfigImpl(preferences);
        accountConfig.setEmail(emailId);
        accountConfig.setDescription(emailId);
        accountConfig.setTrashFolderName("Trash");
        accountConfig.setArchiveFolderName("Archive");
        accountConfig.setDraftsFolderName("Drafts");
        accountConfig.setInboxFolderName("INBOX");
        accountConfig.setSentFolderName("Sent");
        accountConfig.setSpamFolderName("Spam");
        accountConfig.setAutoExpandFolderName("INBOX");
        accountConfig.setStoreUri("imap+ssl+://" + emailId.substring(0,
                emailId.indexOf("@")) + "%40eelo.io:" + password + "@mail.eelo.io");
        accountConfig.setTransportUri("smtp+tls+://" + emailId.substring(0,
                emailId.indexOf("@")) + "%40eelo.io:" + password + "@mail.eelo.io");

        Account account = preferences.newAccount();
        account.loadConfig(accountConfig);

        MessagingController.getInstance(context).listFoldersSynchronous(account, true, null);
        MessagingController.getInstance(context)
                .synchronizeMailbox(account, account.getInboxFolderName(), null, null);

        account.save(preferences);

        preferences.setDefaultAccount(account);

        K9.setServicesEnabled(context);
    }
}