Set password on token refresh

This commit is contained in:
M66B 2019-09-19 13:21:37 +02:00
parent 00bd87140c
commit 3704799646
3 changed files with 24 additions and 21 deletions

View File

@ -105,9 +105,6 @@ public interface DaoAccount {
@Update
void updateAccount(EntityAccount account);
@Query("UPDATE account SET password = :password WHERE password = :old")
int updateAccountPassword(String old, String password);
@Query("UPDATE account SET separator = :separator WHERE id = :id")
int setFolderSeparator(long id, Character separator);
@ -117,6 +114,9 @@ public interface DaoAccount {
@Query("UPDATE account SET state = :state WHERE id = :id")
int setAccountState(long id, String state);
@Query("UPDATE account SET password = :password WHERE id = :id")
int setAccountPassword(long id, String password);
@Query("UPDATE account SET last_connected = :last_connected WHERE id = :id")
int setAccountConnected(long id, long last_connected);

View File

@ -67,15 +67,15 @@ public interface DaoIdentity {
@Update
void updateIdentity(EntityIdentity identity);
@Query("UPDATE identity SET password = :password WHERE password = :old")
int updateIdentityPassword(String old, String password);
@Query("UPDATE identity SET synchronize = :synchronize WHERE id = :id")
int setIdentitySynchronize(long id, boolean synchronize);
@Query("UPDATE identity SET state = :state WHERE id = :id")
int setIdentityState(long id, String state);
@Query("UPDATE identity SET password = :password WHERE id = :id")
int setIdentityPassword(long id, String password);
@Query("UPDATE identity SET last_connected = :last_connected WHERE id = :id")
int setIdentityConnected(long id, long last_connected);

View File

@ -138,14 +138,24 @@ public class MailService implements AutoCloseable {
}
public void connect(EntityAccount account) throws MessagingException {
connect(account.host, account.port, account.auth_type, account.user, account.password);
String password = connect(account.host, account.port, account.auth_type, account.user, account.password);
if (password != null) {
DB db = DB.getInstance(context);
int count = db.account().setAccountPassword(account.id, account.password);
Log.i(account.name + " token refreshed=" + count);
}
}
public void connect(EntityIdentity identity) throws MessagingException {
connect(identity.host, identity.port, identity.auth_type, identity.user, identity.password);
String password = connect(identity.host, identity.port, identity.auth_type, identity.user, identity.password);
if (password != null) {
DB db = DB.getInstance(context);
int count = db.identity().setIdentityPassword(identity.id, identity.password);
Log.i(identity.email + " token refreshed=" + count);
}
}
public void connect(String host, int port, int auth, String user, String password) throws MessagingException {
public String connect(String host, int port, int auth, String user, String password) throws MessagingException {
try {
if (auth == AUTH_TYPE_GMAIL)
properties.put("mail." + protocol + ".auth.mechanisms", "XOAUTH2");
@ -154,6 +164,7 @@ public class MailService implements AutoCloseable {
// throw new MailConnectException(new SocketConnectException("Debug", new Exception(), host, port, 0));
_connect(context, host, port, user, password);
return null;
} catch (AuthenticationFailedException ex) {
// Refresh token
if (auth == AUTH_TYPE_GMAIL)
@ -169,19 +180,11 @@ public class MailService implements AutoCloseable {
if (token == null)
throw new IllegalArgumentException("no token");
int count = 0;
DB db = DB.getInstance(context);
if ("imap".equals(protocol) || "imaps".equals(protocol))
count = db.account().updateAccountPassword(password, token);
else if ("smtp".equals(protocol) || "smtps".equals(protocol))
count = db.identity().updateIdentityPassword(password, token);
if (count != 1)
throw new IllegalArgumentException(protocol + "=" + count);
_connect(context, host, port, user, token);
break;
return token;
}
throw new IllegalArgumentException("no account");
} catch (Throwable ex1) {
Log.e(ex1);
throw ex;
@ -198,7 +201,7 @@ public class MailService implements AutoCloseable {
try {
Log.i("Falling back to " + iaddr.getHostAddress());
_connect(context, iaddr.getHostAddress(), port, user, password);
return;
return null;
} catch (MessagingException ex1) {
Log.w(ex1);
}