Allow converting to OAuth

pull/194/merge
M66B 3 years ago
parent 4bf9e9cc85
commit 09c2fcb1c4

@ -150,8 +150,8 @@ public interface DaoAccount {
@Query("SELECT * FROM account" + @Query("SELECT * FROM account" +
" WHERE user = :user" + " WHERE user = :user" +
" AND auth_type = :auth_type") " AND auth_type IN (:auth_type)")
List<EntityAccount> getAccounts(String user, int auth_type); List<EntityAccount> getAccounts(String user, int[] auth_type);
@Query("SELECT * FROM account WHERE `primary`") @Query("SELECT * FROM account WHERE `primary`")
EntityAccount getPrimaryAccount(); EntityAccount getPrimaryAccount();
@ -204,8 +204,11 @@ public interface DaoAccount {
@Query("UPDATE account SET name = :name WHERE id = :id AND NOT (name IS :name)") @Query("UPDATE account SET name = :name WHERE id = :id AND NOT (name IS :name)")
int setAccountName(long id, String name); int setAccountName(long id, String name);
@Query("UPDATE account SET password = :password WHERE id = :id AND NOT (password IS :password)") @Query("UPDATE account" +
int setAccountPassword(long id, String password); " SET password = :password, auth_type = :auth_type" +
" WHERE id = :id" +
" AND NOT (password IS :password AND auth_type = :auth_type)")
int setAccountPassword(long id, String password, int auth_type);
@Query("UPDATE account SET last_connected = :last_connected WHERE id = :id AND NOT (last_connected IS :last_connected)") @Query("UPDATE account SET last_connected = :last_connected WHERE id = :id AND NOT (last_connected IS :last_connected)")
int setAccountConnected(long id, Long last_connected); int setAccountConnected(long id, Long last_connected);

@ -111,12 +111,13 @@ public interface DaoIdentity {
" AND host LIKE :domain") " AND host LIKE :domain")
int setIdentityPassword(long account, String user, String password, String domain); int setIdentityPassword(long account, String user, String password, String domain);
@Query("UPDATE identity SET password = :password" + @Query("UPDATE identity" +
" SET password = :password, auth_type = :new_auth_type" +
" WHERE account = :account" + " WHERE account = :account" +
" AND user = :user" + " AND user = :user" +
" AND NOT (password IS :password)" + " AND auth_type = :auth_type" +
" 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 setIdentityPassword(long account, String user, String password, int auth_type, int new_auth_type);
@Query("UPDATE identity SET last_connected = :last_connected WHERE id = :id AND NOT (last_connected IS :last_connected)") @Query("UPDATE identity SET last_connected = :last_connected WHERE id = :id AND NOT (last_connected IS :last_connected)")
int setIdentityConnected(long id, long last_connected); int setIdentityConnected(long id, long last_connected);

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

@ -23,6 +23,7 @@ import static android.accounts.AccountManager.newChooseAccountIntent;
import static android.app.Activity.RESULT_OK; import static android.app.Activity.RESULT_OK;
import static eu.faircode.email.GmailState.TYPE_GOOGLE; 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_GMAIL;
import static eu.faircode.email.ServiceAuthenticator.AUTH_TYPE_PASSWORD;
import android.Manifest; import android.Manifest;
import android.accounts.Account; import android.accounts.Account;
@ -459,7 +460,7 @@ public class FragmentGmail extends FragmentBase {
if (args.getBoolean("update")) { if (args.getBoolean("update")) {
List<EntityAccount> accounts = List<EntityAccount> accounts =
db.account().getAccounts(user, AUTH_TYPE_GMAIL); db.account().getAccounts(user, new int[]{AUTH_TYPE_GMAIL, AUTH_TYPE_PASSWORD});
if (accounts != null && accounts.size() == 1) if (accounts != null && accounts.size() == 1)
update = accounts.get(0); update = accounts.get(0);
} }
@ -536,8 +537,8 @@ public class FragmentGmail extends FragmentBase {
args.putLong("account", update.id); args.putLong("account", update.id);
EntityLog.log(context, "Gmail update account=" + update.name); EntityLog.log(context, "Gmail update account=" + update.name);
db.account().setAccountSynchronize(update.id, true); db.account().setAccountSynchronize(update.id, true);
db.account().setAccountPassword(update.id, password); db.account().setAccountPassword(update.id, password, AUTH_TYPE_GMAIL);
db.identity().setIdentityPassword(update.id, update.user, password, update.auth_type); db.identity().setIdentityPassword(update.id, update.user, password, update.auth_type, AUTH_TYPE_GMAIL);
} }
db.setTransactionSuccessful(); db.setTransactionSuccessful();

@ -21,6 +21,7 @@ package eu.faircode.email;
import static android.app.Activity.RESULT_OK; import static android.app.Activity.RESULT_OK;
import static eu.faircode.email.ServiceAuthenticator.AUTH_TYPE_OAUTH; 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.ActivityNotFoundException;
import android.content.Context; import android.content.Context;
@ -806,7 +807,7 @@ public class FragmentOAuth extends FragmentBase {
if (args.getBoolean("update")) { if (args.getBoolean("update")) {
List<EntityAccount> accounts = List<EntityAccount> accounts =
db.account().getAccounts(username, AUTH_TYPE_OAUTH); db.account().getAccounts(username, new int[]{AUTH_TYPE_OAUTH, AUTH_TYPE_PASSWORD});
if (accounts != null && accounts.size() == 1) if (accounts != null && accounts.size() == 1)
update = accounts.get(0); update = accounts.get(0);
} }
@ -893,8 +894,8 @@ public class FragmentOAuth extends FragmentBase {
args.putLong("account", update.id); args.putLong("account", update.id);
EntityLog.log(context, "OAuth update account=" + update.name); EntityLog.log(context, "OAuth update account=" + update.name);
db.account().setAccountSynchronize(update.id, true); db.account().setAccountSynchronize(update.id, true);
db.account().setAccountPassword(update.id, state); db.account().setAccountPassword(update.id, state, AUTH_TYPE_OAUTH);
db.identity().setIdentityPassword(update.id, update.user, state, update.auth_type); db.identity().setIdentityPassword(update.id, update.user, state, update.auth_type, AUTH_TYPE_OAUTH);
} }
db.setTransactionSuccessful(); db.setTransactionSuccessful();

Loading…
Cancel
Save