From 3fd748d5137bca9903b640731a2de209806111e8 Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 12 Aug 2022 12:12:17 +0200 Subject: [PATCH] Added fail-safe --- .../java/eu/faircode/email/GmailState.java | 15 +++++++++++++ .../faircode/email/ServiceAuthenticator.java | 22 +++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/GmailState.java b/app/src/main/java/eu/faircode/email/GmailState.java index a07e1341d8..56978611b2 100644 --- a/app/src/main/java/eu/faircode/email/GmailState.java +++ b/app/src/main/java/eu/faircode/email/GmailState.java @@ -24,8 +24,10 @@ import android.accounts.AccountManager; import android.accounts.AuthenticatorException; import android.accounts.OperationCanceledException; import android.content.Context; +import android.content.SharedPreferences; import androidx.annotation.NonNull; +import androidx.preference.PreferenceManager; import org.json.JSONException; import org.json.JSONObject; @@ -71,6 +73,19 @@ public class GmailState { expiration - ServiceAuthenticator.MIN_FORCE_REFRESH_INTERVAL < now) needsRefresh = true; + if (needsRefresh) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + String key = "token." + id + "." + user; + long last_refresh = prefs.getLong(key, 0); + long ago = now - last_refresh; + EntityLog.log(context, EntityLog.Type.Debug, "Token needs refresh" + + " user=" + id + ":" + user + " ago=" + (ago / 60 / 1000L) + " min"); + if (ago < ServiceAuthenticator.MIN_FORCE_REFRESH_INTERVAL) { + Log.e("Blocked token refresh id=" + id + " ago=" + (ago / 1000L)); + return; + } + prefs.edit().putLong(key, now).apply(); + } EntityLog.log(context, EntityLog.Type.Debug, "Token user=" + id + ":" + user + " expiration=" + (expiration == null ? null : new Date(expiration)) + diff --git a/app/src/main/java/eu/faircode/email/ServiceAuthenticator.java b/app/src/main/java/eu/faircode/email/ServiceAuthenticator.java index aa1a8ac553..a2ee7f7caa 100644 --- a/app/src/main/java/eu/faircode/email/ServiceAuthenticator.java +++ b/app/src/main/java/eu/faircode/email/ServiceAuthenticator.java @@ -24,6 +24,9 @@ import static eu.faircode.email.GmailState.TYPE_GOOGLE; import android.accounts.AuthenticatorException; import android.accounts.OperationCanceledException; import android.content.Context; +import android.content.SharedPreferences; + +import androidx.preference.PreferenceManager; import net.openid.appauth.AuthState; import net.openid.appauth.AuthorizationException; @@ -150,14 +153,29 @@ public class ServiceAuthenticator extends Authenticator { long now = new Date().getTime(); Long expiration = authState.getAccessTokenExpirationTime(); boolean needsRefresh = (expiration != null && expiration < now); - if (needsRefresh) - authState.setNeedsTokenRefresh(true); if (!needsRefresh && forceRefresh && expiration != null && expiration - ServiceAuthenticator.MIN_FORCE_REFRESH_INTERVAL < now) + needsRefresh = true; + + if (needsRefresh) authState.setNeedsTokenRefresh(true); + if (needsRefresh) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + String key = "token." + id + "." + user; + long last_refresh = prefs.getLong(key, 0); + long ago = now - last_refresh; + EntityLog.log(context, EntityLog.Type.Debug, "Token needs refresh" + + " user=" + id + ":" + user + " ago=" + (ago / 60 / 1000L) + " min"); + if (ago < ServiceAuthenticator.MIN_FORCE_REFRESH_INTERVAL) { + Log.e("Blocked token refresh id=" + id + " ago=" + (ago / 1000L)); + return; + } + prefs.edit().putLong(key, now).apply(); + } + EntityLog.log(context, EntityLog.Type.Debug, "Token user=" + id + ":" + user + " expiration=" + (expiration == null ? null : new Date(expiration)) + " need=" + needsRefresh + "/" + authState.getNeedsTokenRefresh() +