Expedite keep alive time for token refresh (2)

pull/209/head
M66B 2 years ago
parent 86bf62a758
commit 425fe9fca2

@ -349,7 +349,7 @@ public class EmailService implements AutoCloseable {
public void connect(EntityAccount account) throws MessagingException { public void connect(EntityAccount account) throws MessagingException {
connect( connect(
account.host, account.port, account.host, account.port,
account.auth_type, account.provider, account.poll_interval, account.auth_type, account.provider,
account.user, account.password, account.user, account.password,
new ServiceAuthenticator.IAuthenticated() { new ServiceAuthenticator.IAuthenticated() {
@Override @Override
@ -368,7 +368,7 @@ public class EmailService implements AutoCloseable {
public void connect(EntityIdentity identity) throws MessagingException { public void connect(EntityIdentity identity) throws MessagingException {
connect( connect(
identity.host, identity.port, identity.host, identity.port,
identity.auth_type, identity.provider, 0, identity.auth_type, identity.provider,
identity.user, identity.password, identity.user, identity.password,
new ServiceAuthenticator.IAuthenticated() { new ServiceAuthenticator.IAuthenticated() {
@Override @Override
@ -389,12 +389,12 @@ public class EmailService implements AutoCloseable {
int auth, String provider, int auth, String provider,
String user, String password, String user, String password,
String certificate, String fingerprint) throws MessagingException { String certificate, String fingerprint) throws MessagingException {
connect(host, port, auth, provider, 0, user, password, null, certificate, fingerprint); connect(host, port, auth, provider, user, password, null, certificate, fingerprint);
} }
private void connect( private void connect(
String host, int port, String host, int port,
int auth, String provider, int keep_alive, int auth, String provider,
String user, String password, String user, String password,
ServiceAuthenticator.IAuthenticated intf, ServiceAuthenticator.IAuthenticated intf,
String certificate, String fingerprint) throws MessagingException { String certificate, String fingerprint) throws MessagingException {
@ -442,8 +442,7 @@ public class EmailService implements AutoCloseable {
} }
properties.put("mail." + protocol + ".forcepasswordrefresh", "true"); properties.put("mail." + protocol + ".forcepasswordrefresh", "true");
authenticator = new ServiceAuthenticator(context, authenticator = new ServiceAuthenticator(context, auth, provider, user, password, intf);
auth, provider, keep_alive, user, password, intf);
if ("imap.wp.pl".equals(host)) if ("imap.wp.pl".equals(host))
properties.put("mail.idledone", "false"); properties.put("mail.idledone", "false");

@ -39,7 +39,7 @@ public class GmailState {
private long acquired; private long acquired;
static final String TYPE_GOOGLE = "com.google"; static final String TYPE_GOOGLE = "com.google";
private static final long TOKEN_LIFETIME = 45 * 60 * 1000L; // milliseconds private static final long TOKEN_LIFETIME = 60 * 60 * 1000L; // milliseconds
private GmailState(String token, long acquired) { private GmailState(String token, long acquired) {
this.token = token; this.token = token;
@ -60,17 +60,10 @@ public class GmailState {
return acquired + TOKEN_LIFETIME; return acquired + TOKEN_LIFETIME;
} }
void refresh(@NonNull Context context, @NonNull String user, boolean expire, long keep_alive) void refresh(@NonNull Context context, @NonNull String user, boolean expire)
throws AuthenticatorException, OperationCanceledException, IOException { throws AuthenticatorException, OperationCanceledException, IOException {
long now = new Date().getTime(); long now = new Date().getTime();
Long expiration = getAccessTokenExpirationTime(); Long expiration = getAccessTokenExpirationTime();
if (expiration != null && expiration - keep_alive < now) {
EntityLog.log(context, "Force invalidation" +
" expiration=" + new Date(expiration) +
" keep-alive=" + (keep_alive / 60 / 1000) + "m");
expire = true;
}
if (expiration != null && expiration - ServiceAuthenticator.MIN_EXPIRE_INTERVAL > now) if (expiration != null && expiration - ServiceAuthenticator.MIN_EXPIRE_INTERVAL > now)
expire = false; expire = false;

@ -47,7 +47,6 @@ public class ServiceAuthenticator extends Authenticator {
private Context context; private Context context;
private int auth; private int auth;
private String provider; private String provider;
private long keep_alive;
private String user; private String user;
private String password; private String password;
private IAuthenticated intf; private IAuthenticated intf;
@ -60,13 +59,12 @@ public class ServiceAuthenticator extends Authenticator {
ServiceAuthenticator( ServiceAuthenticator(
Context context, Context context,
int auth, String provider, int keep_alive, int auth, String provider,
String user, String password, String user, String password,
IAuthenticated intf) { IAuthenticated intf) {
this.context = context.getApplicationContext(); this.context = context.getApplicationContext();
this.auth = auth; this.auth = auth;
this.provider = provider; this.provider = provider;
this.keep_alive = keep_alive * 60 * 1000L;
this.user = user; this.user = user;
this.password = password; this.password = password;
this.intf = intf; this.intf = intf;
@ -93,7 +91,7 @@ public class ServiceAuthenticator extends Authenticator {
String refreshToken(boolean expire) throws AuthenticatorException, OperationCanceledException, IOException, JSONException, MessagingException { String refreshToken(boolean expire) throws AuthenticatorException, OperationCanceledException, IOException, JSONException, MessagingException {
if (auth == AUTH_TYPE_GMAIL) { if (auth == AUTH_TYPE_GMAIL) {
GmailState authState = GmailState.jsonDeserialize(password); GmailState authState = GmailState.jsonDeserialize(password);
authState.refresh(context, user, expire, keep_alive); authState.refresh(context, user, expire);
Long expiration = authState.getAccessTokenExpirationTime(); Long expiration = authState.getAccessTokenExpirationTime();
if (expiration != null) if (expiration != null)
EntityLog.log(context, user + " token expiration=" + new Date(expiration)); EntityLog.log(context, user + " token expiration=" + new Date(expiration));
@ -108,7 +106,7 @@ public class ServiceAuthenticator extends Authenticator {
return authState.getAccessToken(); return authState.getAccessToken();
} else if (auth == AUTH_TYPE_OAUTH && provider != null) { } else if (auth == AUTH_TYPE_OAUTH && provider != null) {
AuthState authState = AuthState.jsonDeserialize(password); AuthState authState = AuthState.jsonDeserialize(password);
OAuthRefresh(context, provider, authState, expire, keep_alive); OAuthRefresh(context, provider, authState, expire);
Long expiration = authState.getAccessTokenExpirationTime(); Long expiration = authState.getAccessTokenExpirationTime();
if (expiration != null) if (expiration != null)
EntityLog.log(context, user + " token expiration=" + new Date(expiration)); EntityLog.log(context, user + " token expiration=" + new Date(expiration));
@ -144,7 +142,7 @@ public class ServiceAuthenticator extends Authenticator {
void onPasswordChanged(Context context, String newPassword); void onPasswordChanged(Context context, String newPassword);
} }
private static void OAuthRefresh(Context context, String id, AuthState authState, boolean expire, long keep_alive) private static void OAuthRefresh(Context context, String id, AuthState authState, boolean expire)
throws MessagingException { throws MessagingException {
try { try {
if ("gmail".equals(id) && !BuildConfig.DEBUG) if ("gmail".equals(id) && !BuildConfig.DEBUG)
@ -152,13 +150,6 @@ public class ServiceAuthenticator extends Authenticator {
long now = new Date().getTime(); long now = new Date().getTime();
Long expiration = authState.getAccessTokenExpirationTime(); Long expiration = authState.getAccessTokenExpirationTime();
if (expiration != null && expiration - keep_alive < now) {
EntityLog.log(context, "OAuth force refresh" +
" expiration=" + new Date(expiration) +
" keep_alive=" + (keep_alive / 60 / 1000) + "m");
expire = true;
}
if (expiration != null && expiration - MIN_EXPIRE_INTERVAL > now) if (expiration != null && expiration - MIN_EXPIRE_INTERVAL > now)
expire = false; expire = false;

Loading…
Cancel
Save