Fixed switching/refreshing graph token

This commit is contained in:
M66B 2023-03-04 22:51:14 +01:00
parent c092e142d7
commit 48ec3e7620
3 changed files with 19 additions and 15 deletions

View File

@ -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);

View File

@ -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<String, String> 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);
}
}

View File

@ -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));