Pre expire tokens

pull/200/head
M66B 4 years ago
parent e7f870ef4d
commit 35a4b1fed9

@ -309,7 +309,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.auth_type, account.provider, account.poll_interval,
account.user, account.password, account.user, account.password,
new ServiceAuthenticator.IAuthenticated() { new ServiceAuthenticator.IAuthenticated() {
@Override @Override
@ -326,7 +326,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, identity.auth_type, identity.provider, 0,
identity.user, identity.password, identity.user, identity.password,
new ServiceAuthenticator.IAuthenticated() { new ServiceAuthenticator.IAuthenticated() {
@Override @Override
@ -342,14 +342,16 @@ public class EmailService implements AutoCloseable {
public void connect( public void connect(
String host, int port, String host, int port,
int auth, String provider, String user, String password, int auth, String provider,
String user, String password,
String certificate, String fingerprint) throws MessagingException { String certificate, String fingerprint) throws MessagingException {
connect(host, port, auth, provider, user, password, null, certificate, fingerprint); connect(host, port, auth, provider, 0, user, password, null, certificate, fingerprint);
} }
private void connect( private void connect(
String host, int port, String host, int port,
int auth, String provider, String user, String password, int auth, String provider, int keep_alive,
String user, String password,
ServiceAuthenticator.IAuthenticated intf, ServiceAuthenticator.IAuthenticated intf,
String certificate, String fingerprint) throws MessagingException { String certificate, String fingerprint) throws MessagingException {
SSLSocketFactoryService factory = null; SSLSocketFactoryService factory = null;
@ -378,7 +380,8 @@ public class EmailService implements AutoCloseable {
} }
properties.put("mail." + protocol + ".forcepasswordrefresh", "true"); properties.put("mail." + protocol + ".forcepasswordrefresh", "true");
ServiceAuthenticator authenticator = new ServiceAuthenticator(context, auth, provider, user, password, intf); ServiceAuthenticator authenticator = new ServiceAuthenticator(context,
auth, provider, keep_alive, user, password, intf);
try { try {
if (auth == AUTH_TYPE_GMAIL || auth == AUTH_TYPE_OAUTH) { if (auth == AUTH_TYPE_GMAIL || auth == AUTH_TYPE_OAUTH) {

@ -60,8 +60,17 @@ public class GmailState {
return acquired + TOKEN_LIFETIME; return acquired + TOKEN_LIFETIME;
} }
void refresh(@NonNull Context context, @NonNull String user, boolean expire) throws AuthenticatorException, OperationCanceledException, IOException { void refresh(@NonNull Context context, @NonNull String user, boolean expire, long keep_alive)
if (expire || acquired + TOKEN_LIFETIME < new Date().getTime()) throws AuthenticatorException, OperationCanceledException, IOException {
Long expiration = getAccessTokenExpirationTime();
if (expiration != null && expiration - keep_alive < new Date().getTime()) {
EntityLog.log(context, "Force invalidation" +
" expiration=" + new Date(expiration) +
" keep-alive=" + (keep_alive / 60 / 1000) + "m");
expire = true;
}
if (expire)
try { try {
if (token != null) { if (token != null) {
EntityLog.log(context, "Invalidating token user=" + user); EntityLog.log(context, "Invalidating token user=" + user);

@ -47,6 +47,7 @@ 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;
@ -57,12 +58,13 @@ public class ServiceAuthenticator extends Authenticator {
ServiceAuthenticator( ServiceAuthenticator(
Context context, Context context,
int auth, String provider, int auth, String provider, int keep_alive,
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;
@ -87,7 +89,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); authState.refresh(context, user, expire, keep_alive);
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));
@ -102,9 +104,7 @@ public class ServiceAuthenticator extends Authenticator {
return authState.getAccessToken(); return authState.getAccessToken();
} else if (auth == AUTH_TYPE_OAUTH) { } else if (auth == AUTH_TYPE_OAUTH) {
AuthState authState = AuthState.jsonDeserialize(password); AuthState authState = AuthState.jsonDeserialize(password);
if (expire) OAuthRefresh(context, provider, authState, expire, keep_alive);
authState.setNeedsTokenRefresh(true);
OAuthRefresh(context, provider, authState);
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));
@ -125,8 +125,20 @@ public class ServiceAuthenticator extends Authenticator {
void onPasswordChanged(String newPassword); void onPasswordChanged(String newPassword);
} }
private static void OAuthRefresh(Context context, String id, AuthState authState) throws MessagingException { private static void OAuthRefresh(Context context, String id, AuthState authState, boolean expire, long keep_alive)
throws MessagingException {
try { try {
Long expiration = authState.getAccessTokenExpirationTime();
if (expiration != null && expiration - keep_alive < new Date().getTime()) {
EntityLog.log(context, "OAuth force refresh" +
" expiration=" + new Date(expiration) +
" keep_alive=" + (keep_alive / 60 / 1000) + "m");
authState.setNeedsTokenRefresh(true);
}
if (expire)
authState.setNeedsTokenRefresh(true);
ClientAuthentication clientAuth; ClientAuthentication clientAuth;
EmailProvider provider = EmailProvider.getProvider(context, id); EmailProvider provider = EmailProvider.getProvider(context, id);
if (provider.oauth.clientSecret == null) if (provider.oauth.clientSecret == null)

Loading…
Cancel
Save