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

@ -39,7 +39,7 @@ public class GmailState {
private long acquired;
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) {
this.token = token;
@ -60,17 +60,10 @@ public class GmailState {
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 {
long now = new Date().getTime();
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)
expire = false;

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

Loading…
Cancel
Save