Skip to content

Commit

Permalink
Attempt to solve #271
Browse files Browse the repository at this point in the history
  • Loading branch information
jobisoft committed Aug 27, 2024
1 parent 23c0330 commit 4f6c340
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
39 changes: 18 additions & 21 deletions content/includes/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 4f6c340

Please sign in to comment.