1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-25 17:27:00 +00:00

Auto expire Gmail tokens

This commit is contained in:
M66B 2020-10-25 22:29:18 +01:00
parent 480eaa18f6
commit bacc2c1024

View file

@ -33,6 +33,7 @@ import net.openid.appauth.ClientSecretPost;
import net.openid.appauth.NoClientAuthentication; import net.openid.appauth.NoClientAuthentication;
import java.io.IOException; import java.io.IOException;
import java.util.Date;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
@ -47,12 +48,14 @@ class ServiceAuthenticator extends Authenticator {
private String user; private String user;
private String password; private String password;
private IAuthenticated intf; private IAuthenticated intf;
private long refreshed;
static final int AUTH_TYPE_PASSWORD = 1; static final int AUTH_TYPE_PASSWORD = 1;
static final int AUTH_TYPE_GMAIL = 2; static final int AUTH_TYPE_GMAIL = 2;
static final int AUTH_TYPE_OAUTH = 3; static final int AUTH_TYPE_OAUTH = 3;
static final String TYPE_GOOGLE = "com.google"; static final String TYPE_GOOGLE = "com.google";
private static final long GMAIL_EXPIRY = 3600 * 1000L;
ServiceAuthenticator( ServiceAuthenticator(
Context context, Context context,
@ -65,6 +68,7 @@ class ServiceAuthenticator extends Authenticator {
this.user = user; this.user = user;
this.password = password; this.password = password;
this.intf = intf; this.intf = intf;
this.refreshed = new Date().getTime();
} }
void expire() { void expire() {
@ -80,9 +84,14 @@ class ServiceAuthenticator extends Authenticator {
String token = password; String token = password;
try { try {
if (auth == AUTH_TYPE_GMAIL) { if (auth == AUTH_TYPE_GMAIL) {
long now = new Date().getTime();
if (now - refreshed > GMAIL_EXPIRY)
expire();
String oldToken = password; String oldToken = password;
token = getGmailToken(context, user); token = getGmailToken(context, user);
password = token; password = token;
if (intf != null && !Objects.equals(oldToken, token)) if (intf != null && !Objects.equals(oldToken, token))
intf.onPasswordChanged(password); intf.onPasswordChanged(password);
} else if (auth == AUTH_TYPE_OAUTH) { } else if (auth == AUTH_TYPE_OAUTH) {
@ -91,6 +100,7 @@ class ServiceAuthenticator extends Authenticator {
OAuthRefresh(context, provider, authState); OAuthRefresh(context, provider, authState);
token = authState.getAccessToken(); token = authState.getAccessToken();
password = authState.jsonSerializeString(); password = authState.jsonSerializeString();
if (intf != null && !Objects.equals(oldToken, token)) if (intf != null && !Objects.equals(oldToken, token))
intf.onPasswordChanged(password); intf.onPasswordChanged(password);
} }