Fixed switching/refreshing graph token

pull/212/head
M66B 2 years ago
parent c092e142d7
commit 48ec3e7620

@ -119,7 +119,7 @@ public interface DaoIdentity {
" SET password = :password, auth_type = :new_auth_type, provider = :provider" + " SET password = :password, auth_type = :new_auth_type, provider = :provider" +
" WHERE account = :account" + " WHERE account = :account" +
" AND user = :user" + " 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)") " 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); int setIdentityPassword(long account, String user, String password, int auth_type, int new_auth_type, String provider);

@ -120,7 +120,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, user, authState, forceRefresh); OAuthRefresh(context, provider, auth, user, authState, forceRefresh);
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));
@ -156,7 +156,7 @@ public class ServiceAuthenticator extends Authenticator {
void onPasswordChanged(Context context, String newPassword); 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 { throws MessagingException {
try { try {
long now = new Date().getTime(); long now = new Date().getTime();
@ -171,26 +171,30 @@ public class ServiceAuthenticator extends Authenticator {
if (needsRefresh) if (needsRefresh)
authState.setNeedsTokenRefresh(true); 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)) + " expiration=" + (expiration == null ? null : new Date(expiration)) +
" need=" + needsRefresh + "/" + authState.getNeedsTokenRefresh() + " need=" + needsRefresh + "/" + authState.getNeedsTokenRefresh() +
" force=" + forceRefresh); " force=" + forceRefresh);
ClientAuthentication clientAuth; ClientAuthentication clientAuth;
EmailProvider provider = EmailProvider.getProvider(context, id); 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; clientAuth = NoClientAuthentication.INSTANCE;
else else
clientAuth = new ClientSecretPost(provider.oauth.clientSecret); clientAuth = new ClientSecretPost(oauth.clientSecret);
ErrorHolder holder = new ErrorHolder(); ErrorHolder holder = new ErrorHolder();
Semaphore semaphore = new Semaphore(0); Semaphore semaphore = new Semaphore(0);
Map<String, String> params = new LinkedHashMap<>(); Map<String, String> params = new LinkedHashMap<>();
if (provider.oauth.tokenScopes) if (oauth.tokenScopes)
params.put("scope", TextUtils.join(" ", provider.oauth.scopes)); 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() AppAuthConfiguration config = new AppAuthConfiguration.Builder()
.setBrowserMatcher(new BrowserMatcher() { .setBrowserMatcher(new BrowserMatcher() {
@Override @Override
@ -218,17 +222,17 @@ public class ServiceAuthenticator extends Authenticator {
authService.dispose(); authService.dispose();
Log.i("OAuth refreshed user=" + id + ":" + user); if (holder.error == null)
Log.i("OAuth refreshed provider=" + id + ":" + getAuthTypeName(auth_type) + " user=" + user);
if (holder.error != null) { else {
Log.e(new Throwable("Token refresh failed" + Log.e(new Throwable("Token refresh failed" +
" id=" + id + " provider=" + id + ":" + getAuthTypeName(auth_type) +
" error=" + holder.error.getMessage(), " error=" + holder.error.getMessage(),
holder.error)); holder.error));
throw holder.error; throw holder.error;
} }
} catch (Exception ex) { } catch (Exception ex) {
throw new MessagingException("OAuth refresh id=" + id, ex); throw new MessagingException("OAuth refresh provider=" + id + ":" + getAuthTypeName(auth_type), ex);
} }
} }

@ -752,7 +752,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
db.identity().setIdentityState(ident.id, "connecting"); db.identity().setIdentityState(ident.id, "connecting");
AuthState authState = AuthState.jsonDeserialize(ident.password); 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(); Long expiration = authState.getAccessTokenExpirationTime();
if (expiration != null) if (expiration != null)
EntityLog.log(ServiceSend.this, ident.user + " token expiration=" + new Date(expiration)); EntityLog.log(ServiceSend.this, ident.user + " token expiration=" + new Date(expiration));

Loading…
Cancel
Save