Allow switching auth types

pull/209/head
M66B 3 years ago
parent c10abc0db0
commit 7d92c52bb9

@ -19,6 +19,7 @@ package eu.faircode.email;
Copyright 2018-2022 by Marcel Bokhorst (M66B)
*/
import static eu.faircode.email.ServiceAuthenticator.AUTH_TYPE_GMAIL;
import static eu.faircode.email.ServiceAuthenticator.AUTH_TYPE_PASSWORD;
import android.annotation.TargetApi;
@ -202,6 +203,8 @@ public class AdapterAccount extends RecyclerView.Adapter<AdapterAccount.ViewHold
ivSync.setImageResource(account.synchronize ? R.drawable.twotone_sync_24 : R.drawable.twotone_sync_disabled_24);
ivSync.setContentDescription(context.getString(account.synchronize ? R.string.title_legend_synchronize_on : R.string.title_legend_synchronize_off));
ivOAuth.setImageDrawable(context.getDrawable(account.auth_type == AUTH_TYPE_GMAIL
? R.drawable.twotone_android_24 : R.drawable.twotone_security_24));
ivOAuth.setVisibility(
settings && account.auth_type != AUTH_TYPE_PASSWORD ? View.VISIBLE : View.GONE);
ivPrimary.setVisibility(account.primary ? View.VISIBLE : View.GONE);

@ -156,9 +156,8 @@ public interface DaoAccount {
@Query("SELECT * FROM account" +
" WHERE user = :user" +
" AND pop = :protocol" +
" AND auth_type IN (:auth_type)" +
" AND tbd IS NULL")
List<EntityAccount> getAccounts(String user, int protocol, int[] auth_type);
List<EntityAccount> getAccounts(String user, int protocol);
@Query("SELECT * FROM account WHERE `primary`")
EntityAccount getPrimaryAccount();
@ -220,10 +219,10 @@ public interface DaoAccount {
int setAccountColor(long id, Integer color);
@Query("UPDATE account" +
" SET password = :password, auth_type = :auth_type" +
" SET password = :password, auth_type = :auth_type, provider = :provider" +
" WHERE id = :id" +
" AND NOT (password IS :password AND auth_type = :auth_type)")
int setAccountPassword(long id, String password, int auth_type);
" AND NOT (password IS :password AND auth_type = :auth_type AND provider = :provider)")
int setAccountPassword(long id, String password, int auth_type, String provider);
@Query("UPDATE account" +
" SET fingerprint = :fingerprint" +

@ -116,12 +116,12 @@ public interface DaoIdentity {
int setIdentityPassword(long account, String user, String password, int auth_type, String domain);
@Query("UPDATE identity" +
" SET password = :password, auth_type = :new_auth_type" +
" SET password = :password, auth_type = :new_auth_type, provider = :provider" +
" WHERE account = :account" +
" AND user = :user" +
" AND auth_type = :auth_type" +
" AND NOT (password IS :password AND auth_type IS :new_auth_type)")
int setIdentityPassword(long account, String user, String password, int auth_type, int new_auth_type);
" AND NOT (password IS :password AND auth_type IS :new_auth_type AND provider = :provider)")
int setIdentityPassword(long account, String user, String password, int auth_type, int new_auth_type, String provider);
@Query("UPDATE identity" +
" SET fingerprint = :fingerprint" +

@ -356,8 +356,8 @@ public class EmailService implements AutoCloseable {
public void onPasswordChanged(Context context, String newPassword) {
DB db = DB.getInstance(context);
account.password = newPassword;
int accounts = db.account().setAccountPassword(account.id, account.password, account.auth_type);
int identities = db.identity().setIdentityPassword(account.id, account.user, account.password, account.auth_type, account.auth_type);
int accounts = db.account().setAccountPassword(account.id, account.password, account.auth_type, account.provider);
int identities = db.identity().setIdentityPassword(account.id, account.user, account.password, account.auth_type, account.auth_type, account.provider);
EntityLog.log(context, EntityLog.Type.Account, account,
"token refreshed=" + accounts + "/" + identities);
}

@ -23,7 +23,6 @@ import static android.accounts.AccountManager.newChooseAccountIntent;
import static android.app.Activity.RESULT_OK;
import static eu.faircode.email.GmailState.TYPE_GOOGLE;
import static eu.faircode.email.ServiceAuthenticator.AUTH_TYPE_GMAIL;
import static eu.faircode.email.ServiceAuthenticator.AUTH_TYPE_PASSWORD;
import android.Manifest;
import android.accounts.Account;
@ -504,10 +503,7 @@ public class FragmentGmail extends FragmentBase {
db.beginTransaction();
if (args.getBoolean("update")) {
List<EntityAccount> accounts =
db.account().getAccounts(user,
protocol,
new int[]{AUTH_TYPE_GMAIL, AUTH_TYPE_PASSWORD});
List<EntityAccount> accounts = db.account().getAccounts(user, protocol);
if (accounts != null && accounts.size() == 1)
update = accounts.get(0);
}
@ -596,8 +592,8 @@ public class FragmentGmail extends FragmentBase {
args.putLong("account", update.id);
EntityLog.log(context, "Gmail update account=" + update.name);
db.account().setAccountSynchronize(update.id, true);
db.account().setAccountPassword(update.id, password, AUTH_TYPE_GMAIL);
db.identity().setIdentityPassword(update.id, update.user, password, update.auth_type, AUTH_TYPE_GMAIL);
db.account().setAccountPassword(update.id, password, AUTH_TYPE_GMAIL, null);
db.identity().setIdentityPassword(update.id, update.user, password, update.auth_type, AUTH_TYPE_GMAIL, null);
}
db.setTransactionSuccessful();
@ -605,12 +601,8 @@ public class FragmentGmail extends FragmentBase {
db.endTransaction();
}
if (update == null)
ServiceSynchronize.eval(context, "Gmail");
else {
args.putBoolean("updated", true);
ServiceSynchronize.reload(context, update.id, true, "Gmail");
}
ServiceSynchronize.eval(context, "Gmail");
args.putBoolean("updated", update != null);
return null;
}

@ -21,7 +21,6 @@ package eu.faircode.email;
import static android.app.Activity.RESULT_OK;
import static eu.faircode.email.ServiceAuthenticator.AUTH_TYPE_OAUTH;
import static eu.faircode.email.ServiceAuthenticator.AUTH_TYPE_PASSWORD;
import android.content.ActivityNotFoundException;
import android.content.Context;
@ -804,10 +803,7 @@ public class FragmentOAuth extends FragmentBase {
db.beginTransaction();
if (args.getBoolean("update")) {
List<EntityAccount> accounts =
db.account().getAccounts(username,
protocol,
new int[]{AUTH_TYPE_OAUTH, AUTH_TYPE_PASSWORD});
List<EntityAccount> accounts = db.account().getAccounts(username, protocol);
if (accounts != null && accounts.size() == 1)
update = accounts.get(0);
}
@ -906,8 +902,8 @@ public class FragmentOAuth extends FragmentBase {
args.putLong("account", update.id);
EntityLog.log(context, "OAuth update account=" + update.name);
db.account().setAccountSynchronize(update.id, true);
db.account().setAccountPassword(update.id, state, AUTH_TYPE_OAUTH);
db.identity().setIdentityPassword(update.id, update.user, state, update.auth_type, AUTH_TYPE_OAUTH);
db.account().setAccountPassword(update.id, state, AUTH_TYPE_OAUTH, provider.id);
db.identity().setIdentityPassword(update.id, update.user, state, update.auth_type, AUTH_TYPE_OAUTH, provider.id);
}
db.setTransactionSuccessful();
@ -915,12 +911,8 @@ public class FragmentOAuth extends FragmentBase {
db.endTransaction();
}
if (update == null)
ServiceSynchronize.eval(context, "OAuth");
else {
args.putBoolean("updated", true);
ServiceSynchronize.reload(context, update.id, true, "OAuth");
}
ServiceSynchronize.eval(context, "OAuth");
args.putBoolean("updated", update != null);
return null;
}

@ -510,10 +510,7 @@ public class FragmentQuickSetup extends FragmentBase {
EntityAccount primary = db.account().getPrimaryAccount();
if (args.getBoolean("update")) {
List<EntityAccount> accounts =
db.account().getAccounts(user,
EntityAccount.TYPE_IMAP,
new int[]{AUTH_TYPE_PASSWORD});
List<EntityAccount> accounts = db.account().getAccounts(user, EntityAccount.TYPE_IMAP);
if (accounts != null && accounts.size() == 1)
update = accounts.get(0);
}
@ -593,9 +590,9 @@ public class FragmentQuickSetup extends FragmentBase {
args.putLong("account", update.id);
EntityLog.log(context, "Quick setup update account=" + update.name);
db.account().setAccountSynchronize(update.id, true);
db.account().setAccountPassword(update.id, password, AUTH_TYPE_PASSWORD);
db.account().setAccountPassword(update.id, password, AUTH_TYPE_PASSWORD, null);
db.account().setAccountFingerprint(update.id, imap_fingerprint);
db.identity().setIdentityPassword(update.id, update.user, password, update.auth_type, AUTH_TYPE_PASSWORD);
db.identity().setIdentityPassword(update.id, update.user, password, update.auth_type, AUTH_TYPE_PASSWORD, null);
db.identity().setIdentityFingerprint(update.id, smtp_fingerprint);
}
@ -604,12 +601,8 @@ public class FragmentQuickSetup extends FragmentBase {
db.endTransaction();
}
if (update == null)
ServiceSynchronize.eval(context, "quick setup");
else {
args.putBoolean("updated", true);
ServiceSynchronize.reload(context, update.id, true, "quick setup");
}
ServiceSynchronize.eval(context, "quick setup");
args.putBoolean("updated", update != null);
return provider;
} catch (Throwable ex) {

@ -37,7 +37,8 @@ public class TupleAccountState extends EntityAccount {
this.insecure.equals(other.insecure) &&
this.port.equals(other.port) &&
this.user.equals(other.user) &&
(auth_type != AUTH_TYPE_PASSWORD || this.password.equals(other.password)) &&
Objects.equals(this.auth_type, other.auth_type) &&
this.password.equals(other.password) &&
Objects.equals(this.certificate_alias, other.certificate_alias) &&
Objects.equals(this.realm, other.realm) &&
Objects.equals(this.fingerprint, other.fingerprint) &&

Loading…
Cancel
Save