Remove old token times

This commit is contained in:
M66B 2022-08-13 17:44:46 +02:00
parent ece898bac0
commit 0f6cd802aa
3 changed files with 17 additions and 5 deletions

View File

@ -438,7 +438,7 @@ public class EmailService implements AutoCloseable {
}
properties.put("mail." + protocol + ".forcepasswordrefresh", "true");
authenticator = new ServiceAuthenticator(context, auth, provider, user, password, intf);
authenticator = new ServiceAuthenticator(context, auth, provider, user, password, purpose == PURPOSE_CHECK, intf);
if ("imap.wp.pl".equals(host))
properties.put("mail.idledone", "false");

View File

@ -75,13 +75,13 @@ public class GmailState {
if (needsRefresh && token != null) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String key = "token." + id + "." + user;
String key = ServiceAuthenticator.getTokenKey(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));
Log.e("Blocked token refresh id=" + id + " ago=" + (ago / 1000L) + " s");
return;
}
prefs.edit().putLong(key, now).apply();

View File

@ -64,6 +64,7 @@ public class ServiceAuthenticator extends Authenticator {
Context context,
int auth, String provider,
String user, String password,
boolean check,
IAuthenticated intf) {
this.context = context.getApplicationContext();
this.auth = auth;
@ -71,6 +72,13 @@ public class ServiceAuthenticator extends Authenticator {
this.user = user;
this.password = password;
this.intf = intf;
if (check) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String key = getTokenKey(provider, user);
prefs.edit().remove(key).apply();
EntityLog.log(context, "Removed " + key);
}
}
@Override
@ -164,13 +172,13 @@ public class ServiceAuthenticator extends Authenticator {
if (needsRefresh || authState.getNeedsTokenRefresh()) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String key = "token." + id + "." + user;
String key = getTokenKey(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));
Log.e("Blocked token refresh id=" + id + " ago=" + (ago / 1000L) + " s");
return;
}
prefs.edit().putLong(key, now).apply();
@ -215,6 +223,10 @@ public class ServiceAuthenticator extends Authenticator {
}
}
static String getTokenKey(String id, String user) {
return "token." + id + "." + user;
}
static String getAuthTokenType(String type) {
// https://developers.google.com/gmail/imap/xoauth2-protocol
if (TYPE_GOOGLE.equals(type))