Allow converting to OAuth

This commit is contained in:
M66B 2021-12-24 08:18:25 +01:00
parent 4bf9e9cc85
commit 09c2fcb1c4
5 changed files with 22 additions and 16 deletions

View File

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

View File

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

View File

@ -330,8 +330,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);
int identities = db.identity().setIdentityPassword(account.id, account.user, account.password, account.auth_type);
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);
EntityLog.log(context, EntityLog.Type.Account, account,
"token refreshed=" + accounts + "/" + identities);
}

View File

@ -23,6 +23,7 @@ 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;
@ -459,7 +460,7 @@ public class FragmentGmail extends FragmentBase {
if (args.getBoolean("update")) {
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)
update = accounts.get(0);
}
@ -536,8 +537,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);
db.identity().setIdentityPassword(update.id, update.user, password, update.auth_type);
db.account().setAccountPassword(update.id, password, AUTH_TYPE_GMAIL);
db.identity().setIdentityPassword(update.id, update.user, password, update.auth_type, AUTH_TYPE_GMAIL);
}
db.setTransactionSuccessful();

View File

@ -21,6 +21,7 @@ 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;
@ -806,7 +807,7 @@ public class FragmentOAuth extends FragmentBase {
if (args.getBoolean("update")) {
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)
update = accounts.get(0);
}
@ -893,8 +894,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);
db.identity().setIdentityPassword(update.id, update.user, state, update.auth_type);
db.account().setAccountPassword(update.id, state, AUTH_TYPE_OAUTH);
db.identity().setIdentityPassword(update.id, update.user, state, update.auth_type, AUTH_TYPE_OAUTH);
}
db.setTransactionSuccessful();