From bacc2c1024e47d6fafaf41a28e4812e656cf26e3 Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 25 Oct 2020 22:29:18 +0100 Subject: [PATCH] Auto expire Gmail tokens --- .../java/eu/faircode/email/ServiceAuthenticator.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/src/main/java/eu/faircode/email/ServiceAuthenticator.java b/app/src/main/java/eu/faircode/email/ServiceAuthenticator.java index a9e2e43545..7c354543c0 100644 --- a/app/src/main/java/eu/faircode/email/ServiceAuthenticator.java +++ b/app/src/main/java/eu/faircode/email/ServiceAuthenticator.java @@ -33,6 +33,7 @@ import net.openid.appauth.ClientSecretPost; import net.openid.appauth.NoClientAuthentication; import java.io.IOException; +import java.util.Date; import java.util.Objects; import java.util.concurrent.Semaphore; @@ -47,12 +48,14 @@ class ServiceAuthenticator extends Authenticator { private String user; private String password; private IAuthenticated intf; + private long refreshed; static final int AUTH_TYPE_PASSWORD = 1; static final int AUTH_TYPE_GMAIL = 2; static final int AUTH_TYPE_OAUTH = 3; static final String TYPE_GOOGLE = "com.google"; + private static final long GMAIL_EXPIRY = 3600 * 1000L; ServiceAuthenticator( Context context, @@ -65,6 +68,7 @@ class ServiceAuthenticator extends Authenticator { this.user = user; this.password = password; this.intf = intf; + this.refreshed = new Date().getTime(); } void expire() { @@ -80,9 +84,14 @@ class ServiceAuthenticator extends Authenticator { String token = password; try { if (auth == AUTH_TYPE_GMAIL) { + long now = new Date().getTime(); + if (now - refreshed > GMAIL_EXPIRY) + expire(); + String oldToken = password; token = getGmailToken(context, user); password = token; + if (intf != null && !Objects.equals(oldToken, token)) intf.onPasswordChanged(password); } else if (auth == AUTH_TYPE_OAUTH) { @@ -91,6 +100,7 @@ class ServiceAuthenticator extends Authenticator { OAuthRefresh(context, provider, authState); token = authState.getAccessToken(); password = authState.jsonSerializeString(); + if (intf != null && !Objects.equals(oldToken, token)) intf.onPasswordChanged(password); }