From 4f6c340ba390a622829bf0065826255329073676 Mon Sep 17 00:00:00 2001 From: John Bieling Date: Tue, 27 Aug 2024 18:36:20 +0200 Subject: [PATCH] Attempt to solve #271 --- content/includes/network.js | 39 +++++++++++++++++-------------------- manifest.json | 2 +- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/content/includes/network.js b/content/includes/network.js index 7a1feea..9b35d37 100644 --- a/content/includes/network.js +++ b/content/includes/network.js @@ -202,39 +202,36 @@ var network = { oauth.asyncConnect = async function (rv) { rv.error = ""; - - // If multiple resources need to authenticate they will all end here, even though they - // might share the same token. Due to the async nature, each process will refresh - // "its own" token again, which is not needed. We force clear the token here and each - // final connect process will actually check the acccessToken and abort the refresh, - // if it is already there, generated by some other process. - if (oauth.getToken("accessToken")) { - await oauth.setToken("accessToken", ""); - } - + // This function gets called if we have no accessToken, or the token + // is expired. Load current data from password manager into the + // properties used by the OAuth module. + oauth.accessToken = oauth.getToken("accessToken"); + oauth.refreshToken = oauth.getToken("refreshToken"); + oauth.tokenExpires = oauth.getToken("tokenExpires"); try { - // refresh = false will do nothing and resolve immediately, if an accessToken - // exists already, which must have been generated by another process, as - // we cleared it beforehand. + // refresh = false will do nothing and resolve immediately, if + // a valid accessToken exists. await oauth.connect(/* with UI */ true, /* refresh */ false); - await oauth.setToken("accessToken", oauth.accessToken); - await oauth.setToken("refreshToken", oauth.refreshToken); - await oauth.setToken("tokenExpires", oauth.tokenExpires); - rv.tokens = oauth.tokens; - return true; } catch (e) { - await oauth.setToken("accessToken", oauth.accessToken); - await oauth.setToken("refreshToken", oauth.refreshToken); - await oauth.setToken("tokenExpires", oauth.tokenExpires); rv.error = eas.tools.isString(e) ? e : JSON.stringify(e); } + await oauth.setToken("accessToken", oauth.accessToken); + await oauth.setToken("refreshToken", oauth.refreshToken); + await oauth.setToken("tokenExpires", oauth.tokenExpires); + rv.tokens = oauth.tokens; + + if (!rv.error) { + return true; + } + try { switch (JSON.parse(rv.error).error) { case "invalid_grant": await oauth.setToken("accessToken", ""); await oauth.setToken("refreshToken", ""); + await oauth.setToken("tokenExpires", 0); rv.tokens = oauth.tokens; return true; diff --git a/manifest.json b/manifest.json index 9afc156..9640708 100644 --- a/manifest.json +++ b/manifest.json @@ -8,7 +8,7 @@ }, "manifest_version": 2, "name": "__MSG_extensionName__", - "version": "4.11", + "version": "4.12", "author": "John Bieling", "homepage_url": "https://github.com/jobisoft/EAS-4-TbSync/", "default_locale": "en-US",