Browse Source

Refresh tokens

main
M66B 5 years ago
parent
commit
8dbf639a48
4 changed files with 40 additions and 5 deletions
  1. +19
    -2
      app/src/main/java/eu/faircode/email/FragmentAccount.java
  2. +10
    -1
      app/src/main/java/eu/faircode/email/FragmentIdentity.java
  3. +1
    -1
      app/src/main/java/eu/faircode/email/Helper.java
  4. +10
    -1
      app/src/main/java/eu/faircode/email/ServiceSynchronize.java

+ 19
- 2
app/src/main/java/eu/faircode/email/FragmentAccount.java View File

@ -75,6 +75,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Properties;
import javax.mail.AuthenticationFailedException;
import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.Session;
@ -472,7 +473,15 @@ public class FragmentAccount extends FragmentEx {
IMAPStore istore = null;
try {
istore = (IMAPStore) isession.getStore("imaps");
istore.connect(host, Integer.parseInt(port), user, password);
try {
istore.connect(host, Integer.parseInt(port), user, password);
} catch (AuthenticationFailedException ex) {
if (auth_type == Helper.AUTH_TYPE_GMAIL) {
password = Helper.refreshToken(context, "com.google", user, password);
istore.connect(host, Integer.parseInt(port), user, password);
} else
throw ex;
}
if (!istore.hasCapability("IDLE"))
throw new MessagingException(getContext().getString(R.string.title_no_idle));
@ -708,7 +717,15 @@ public class FragmentAccount extends FragmentEx {
IMAPStore istore = null;
try {
istore = (IMAPStore) isession.getStore("imaps");
istore.connect(host, Integer.parseInt(port), user, password);
try {
istore.connect(host, Integer.parseInt(port), user, password);
} catch (AuthenticationFailedException ex) {
if (auth_type == Helper.AUTH_TYPE_GMAIL) {
password = Helper.refreshToken(context, "com.google", user, password);
istore.connect(host, Integer.parseInt(port), user, password);
} else
throw ex;
}
if (!istore.hasCapability("UIDPLUS"))
throw new MessagingException(getContext().getString(R.string.title_no_uidplus));


+ 10
- 1
app/src/main/java/eu/faircode/email/FragmentIdentity.java View File

@ -49,6 +49,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.mail.AuthenticationFailedException;
import javax.mail.Session;
import javax.mail.Transport;
@ -342,7 +343,15 @@ public class FragmentIdentity extends FragmentEx {
isession.setDebug(true);
Transport itransport = isession.getTransport(starttls ? "smtp" : "smtps");
try {
itransport.connect(host, Integer.parseInt(port), user, password);
try {
itransport.connect(host, Integer.parseInt(port), user, password);
} catch (AuthenticationFailedException ex) {
if (auth_type == Helper.AUTH_TYPE_GMAIL) {
password = Helper.refreshToken(context, "com.google", user, password);
itransport.connect(host, Integer.parseInt(port), user, password);
} else
throw ex;
}
} finally {
itransport.close();
}


+ 1
- 1
app/src/main/java/eu/faircode/email/Helper.java View File

@ -230,7 +230,7 @@ public class Helper {
}
}
private static String refreshToken(Context context, String type, String name, String current) {
static String refreshToken(Context context, String type, String name, String current) {
try {
AccountManager am = AccountManager.get(context);
Account[] accounts = am.getAccountsByType(type);


+ 10
- 1
app/src/main/java/eu/faircode/email/ServiceSynchronize.java View File

@ -1261,7 +1261,16 @@ public class ServiceSynchronize extends LifecycleService {
try {
// Connect transport
db.identity().setIdentityState(ident.id, "connecting");
itransport.connect(ident.host, ident.port, ident.user, ident.password);
try {
itransport.connect(ident.host, ident.port, ident.user, ident.password);
} catch (AuthenticationFailedException ex) {
if (ident.auth_type == Helper.AUTH_TYPE_GMAIL) {
ident.password = Helper.refreshToken(this, "com.google", ident.user, ident.password);
DB.getInstance(this).identity().setIdentityPassword(ident.id, ident.password);
itransport.connect(ident.host, ident.port, ident.user, ident.password);
} else
throw ex;
}
db.identity().setIdentityState(ident.id, "connected");
db.identity().setIdentityError(ident.id, null);


Loading…
Cancel
Save