From ae1a79a0e6871bb54264403d55f2133e1ad98914 Mon Sep 17 00:00:00 2001 From: Frank Chiarulli Jr Date: Thu, 5 Nov 2020 08:55:51 -0500 Subject: [PATCH 1/4] try to refresh token if get fails --- src/background.js | 8 ++++++-- src/token-pool.js | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/background.js b/src/background.js index 8f1eeb5..d18f49b 100644 --- a/src/background.js +++ b/src/background.js @@ -12,8 +12,12 @@ class AccessToken { injectLoggedInStatus(!!AccessToken.TOKEN); } - static get() { - return AccessToken.TOKEN; + static async get() { + if (AccessToken.TOKEN) { + return AccessToken.TOKEN + } + await AccessToken.refresh(); + return AccessToken.TOKEN } static parse() { diff --git a/src/token-pool.js b/src/token-pool.js index 94e1a31..cfebf19 100644 --- a/src/token-pool.js +++ b/src/token-pool.js @@ -201,7 +201,7 @@ class TokenPool { return; } - const accessToken = AccessToken.get(); + const accessToken = await AccessToken.get(); if (!accessToken) { return; } From 5fe1909b018f151ba7980ca976e6e533d42a8865 Mon Sep 17 00:00:00 2001 From: Frank Chiarulli Jr Date: Thu, 5 Nov 2020 09:21:38 -0500 Subject: [PATCH 2/4] fix race condition --- src/background.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/background.js b/src/background.js index d18f49b..b3d7d0e 100644 --- a/src/background.js +++ b/src/background.js @@ -13,11 +13,12 @@ class AccessToken { } static async get() { - if (AccessToken.TOKEN) { - return AccessToken.TOKEN + const token = AccessToken.TOKEN; + if (token) { + return token; } await AccessToken.refresh(); - return AccessToken.TOKEN + return AccessToken.TOKEN; } static parse() { From c49a4b2e886619f7ccaf795ea6c27f813f9c5461 Mon Sep 17 00:00:00 2001 From: Frank Chiarulli Jr Date: Thu, 5 Nov 2020 09:46:36 -0500 Subject: [PATCH 3/4] retry fetch properly if recieved 401 --- src/token-pool.js | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/token-pool.js b/src/token-pool.js index cfebf19..445e5f9 100644 --- a/src/token-pool.js +++ b/src/token-pool.js @@ -215,16 +215,14 @@ class TokenPool { pretokens.push({ token, blindFactor }); } - const response = await fetch(`${API_BASE_URL}/tokens/new`, { - method: 'POST', - headers: { - 'Authorization': `Bearer ${accessToken}`, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - blindTokens, - }), - }); + let response = await this._fetchNewTokens(accessToken, blindTokens); + if (response.status === 401) { + // try to refresh token and try again if authorization failed + // as the token technically could have expired by the time the request + // arives + const accessToken = await AccessToken.get(); + response = await this._fetchNewTokens(accessToken, blindTokens); + } if (response.ok) { const { tokens } = await response.json(); const res = []; @@ -246,9 +244,19 @@ class TokenPool { })); console.warn(`Adding ${res.length} tokens to acquired pool`); this.tokens.push(...res); - } else if (response.status === 401) { - // refresh the access token. This will call generateTokens if the refresh is successful - AccessToken.refresh(); } } + + async _fetchNewTokens(accessToken, blindTokens) { + return fetch(`${API_BASE_URL}/tokens/new`, { + method: 'POST', + headers: { + 'Authorization': `Bearer ${accessToken}`, + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + blindTokens, + }), + }); + } } From a9fd0c95ad7f6ef116c5ddfd26c002a000515dc7 Mon Sep 17 00:00:00 2001 From: Frank Chiarulli Jr Date: Thu, 5 Nov 2020 09:49:53 -0500 Subject: [PATCH 4/4] add early return --- src/token-pool.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/token-pool.js b/src/token-pool.js index 445e5f9..e3ab37e 100644 --- a/src/token-pool.js +++ b/src/token-pool.js @@ -221,6 +221,9 @@ class TokenPool { // as the token technically could have expired by the time the request // arives const accessToken = await AccessToken.get(); + if (!accessToken) { + return; + } response = await this._fetchNewTokens(accessToken, blindTokens); } if (response.ok) {