From 3704799646e2917714abbb7edbc21d26f7577476 Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 19 Sep 2019 13:21:37 +0200 Subject: [PATCH] Set password on token refresh --- .../java/eu/faircode/email/DaoAccount.java | 6 ++-- .../java/eu/faircode/email/DaoIdentity.java | 6 ++-- .../java/eu/faircode/email/MailService.java | 33 ++++++++++--------- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/DaoAccount.java b/app/src/main/java/eu/faircode/email/DaoAccount.java index 4107cc56f0..f38ce4cb1f 100644 --- a/app/src/main/java/eu/faircode/email/DaoAccount.java +++ b/app/src/main/java/eu/faircode/email/DaoAccount.java @@ -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); diff --git a/app/src/main/java/eu/faircode/email/DaoIdentity.java b/app/src/main/java/eu/faircode/email/DaoIdentity.java index 32f6fbc6cf..2c46b8d8e5 100644 --- a/app/src/main/java/eu/faircode/email/DaoIdentity.java +++ b/app/src/main/java/eu/faircode/email/DaoIdentity.java @@ -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); diff --git a/app/src/main/java/eu/faircode/email/MailService.java b/app/src/main/java/eu/faircode/email/MailService.java index 26f479e391..43e50d3454 100644 --- a/app/src/main/java/eu/faircode/email/MailService.java +++ b/app/src/main/java/eu/faircode/email/MailService.java @@ -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); }