From 48ec3e7620f011d43faedacc7da248694f2d49bc Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 4 Mar 2023 22:51:14 +0100 Subject: [PATCH] Fixed switching/refreshing graph token --- .../java/eu/faircode/email/DaoIdentity.java | 2 +- .../faircode/email/ServiceAuthenticator.java | 30 +++++++++++-------- .../java/eu/faircode/email/ServiceSend.java | 2 +- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/DaoIdentity.java b/app/src/main/java/eu/faircode/email/DaoIdentity.java index e1278d1e5f..4dd88e6cd4 100644 --- a/app/src/main/java/eu/faircode/email/DaoIdentity.java +++ b/app/src/main/java/eu/faircode/email/DaoIdentity.java @@ -119,7 +119,7 @@ public interface DaoIdentity { " SET password = :password, auth_type = :new_auth_type, provider = :provider" + " WHERE account = :account" + " AND user = :user" + - " AND auth_type = :auth_type" + + " AND (auth_type = :auth_type OR auth_type = " + ServiceAuthenticator.AUTH_TYPE_GRAPH + ")" + " AND NOT (password IS :password AND auth_type IS :new_auth_type AND provider = :provider)") int setIdentityPassword(long account, String user, String password, int auth_type, int new_auth_type, String provider); diff --git a/app/src/main/java/eu/faircode/email/ServiceAuthenticator.java b/app/src/main/java/eu/faircode/email/ServiceAuthenticator.java index 9959f63548..81bdfd87c1 100644 --- a/app/src/main/java/eu/faircode/email/ServiceAuthenticator.java +++ b/app/src/main/java/eu/faircode/email/ServiceAuthenticator.java @@ -120,7 +120,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, user, authState, forceRefresh); + OAuthRefresh(context, provider, auth, user, authState, forceRefresh); Long expiration = authState.getAccessTokenExpirationTime(); if (expiration != null) EntityLog.log(context, user + " token expiration=" + new Date(expiration)); @@ -156,7 +156,7 @@ public class ServiceAuthenticator extends Authenticator { void onPasswordChanged(Context context, String newPassword); } - static void OAuthRefresh(Context context, String id, String user, AuthState authState, boolean forceRefresh) + static void OAuthRefresh(Context context, String id, int auth_type, String user, AuthState authState, boolean forceRefresh) throws MessagingException { try { long now = new Date().getTime(); @@ -171,26 +171,30 @@ public class ServiceAuthenticator extends Authenticator { if (needsRefresh) authState.setNeedsTokenRefresh(true); - EntityLog.log(context, EntityLog.Type.General, "Token user=" + id + ":" + user + + EntityLog.log(context, EntityLog.Type.General, "Token" + + " provider=" + id + ":" + getAuthTypeName(auth_type) + + " user" + user + " expiration=" + (expiration == null ? null : new Date(expiration)) + " need=" + needsRefresh + "/" + authState.getNeedsTokenRefresh() + " force=" + forceRefresh); ClientAuthentication clientAuth; EmailProvider provider = EmailProvider.getProvider(context, id); - if (provider.oauth.clientSecret == null) + EmailProvider.OAuth oauth = (auth_type == AUTH_TYPE_GRAPH ? provider.graph : provider.oauth); + + if (oauth.clientSecret == null) clientAuth = NoClientAuthentication.INSTANCE; else - clientAuth = new ClientSecretPost(provider.oauth.clientSecret); + clientAuth = new ClientSecretPost(oauth.clientSecret); ErrorHolder holder = new ErrorHolder(); Semaphore semaphore = new Semaphore(0); Map params = new LinkedHashMap<>(); - if (provider.oauth.tokenScopes) - params.put("scope", TextUtils.join(" ", provider.oauth.scopes)); + if (oauth.tokenScopes) + params.put("scope", TextUtils.join(" ", oauth.scopes)); - Log.i("OAuth refresh user=" + id + ":" + user); + Log.i("OAuth refresh provider=" + id + ":" + getAuthTypeName(auth_type) + " user=" + user); AppAuthConfiguration config = new AppAuthConfiguration.Builder() .setBrowserMatcher(new BrowserMatcher() { @Override @@ -218,17 +222,17 @@ public class ServiceAuthenticator extends Authenticator { authService.dispose(); - Log.i("OAuth refreshed user=" + id + ":" + user); - - if (holder.error != null) { + if (holder.error == null) + Log.i("OAuth refreshed provider=" + id + ":" + getAuthTypeName(auth_type) + " user=" + user); + else { Log.e(new Throwable("Token refresh failed" + - " id=" + id + + " provider=" + id + ":" + getAuthTypeName(auth_type) + " error=" + holder.error.getMessage(), holder.error)); throw holder.error; } } catch (Exception ex) { - throw new MessagingException("OAuth refresh id=" + id, ex); + throw new MessagingException("OAuth refresh provider=" + id + ":" + getAuthTypeName(auth_type), ex); } } diff --git a/app/src/main/java/eu/faircode/email/ServiceSend.java b/app/src/main/java/eu/faircode/email/ServiceSend.java index 878df12622..f71584e26e 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSend.java +++ b/app/src/main/java/eu/faircode/email/ServiceSend.java @@ -752,7 +752,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar db.identity().setIdentityState(ident.id, "connecting"); AuthState authState = AuthState.jsonDeserialize(ident.password); - ServiceAuthenticator.OAuthRefresh(ServiceSend.this, ident.provider, ident.user, authState, false); + ServiceAuthenticator.OAuthRefresh(ServiceSend.this, ident.provider, ident.auth_type, ident.user, authState, false); Long expiration = authState.getAccessTokenExpirationTime(); if (expiration != null) EntityLog.log(ServiceSend.this, ident.user + " token expiration=" + new Date(expiration));