From ec1a1e0fcfb84b9010a866b76b1d2f6341a44d85 Mon Sep 17 00:00:00 2001 From: Frank Chiarulli Jr Date: Thu, 5 Nov 2020 08:55:51 -0500 Subject: [PATCH 1/9] 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 a82e5a6..816300b 100644 --- a/src/background.js +++ b/src/background.js @@ -11,8 +11,12 @@ class AccessToken { tokenPool.generateTokens(); } - static get() { - return AccessToken.TOKEN; + static async get() { + if (AccessToken.TOKEN) { + return AccessToken.TOKEN + } + await AccessToken.refresh(); + return AccessToken.TOKEN } static destroy() { diff --git a/src/token-pool.js b/src/token-pool.js index 6a88d70..1f9bad6 100644 --- a/src/token-pool.js +++ b/src/token-pool.js @@ -39,7 +39,7 @@ class TokenPool { } async generateTokens() { - const accessToken = AccessToken.get(); + const accessToken = await AccessToken.get(); if (!accessToken) { return; } From 815336345df8b314e9fb00be794db4705490a236 Mon Sep 17 00:00:00 2001 From: Frank Chiarulli Jr Date: Thu, 5 Nov 2020 09:21:38 -0500 Subject: [PATCH 2/9] 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 816300b..e4ed01a 100644 --- a/src/background.js +++ b/src/background.js @@ -12,11 +12,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 destroy() { From 5161065f357aa453f95789d86f612615104ffc79 Mon Sep 17 00:00:00 2001 From: Frank Chiarulli Jr Date: Thu, 5 Nov 2020 09:46:36 -0500 Subject: [PATCH 3/9] 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 1f9bad6..3907410 100644 --- a/src/token-pool.js +++ b/src/token-pool.js @@ -53,16 +53,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 = []; @@ -83,9 +81,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, + }), + }); + } } \ No newline at end of file From e84525c1775ff40f8aba5984598f64398432f6fc Mon Sep 17 00:00:00 2001 From: Frank Chiarulli Jr Date: Thu, 5 Nov 2020 09:49:53 -0500 Subject: [PATCH 4/9] 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 3907410..e37cff1 100644 --- a/src/token-pool.js +++ b/src/token-pool.js @@ -59,6 +59,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) { From bffc2e5b1633e37fe5b1b52fcb7bd56ebdab9381 Mon Sep 17 00:00:00 2001 From: Frank Chiarulli Jr Date: Thu, 5 Nov 2020 11:56:36 -0500 Subject: [PATCH 5/9] move refresh to cookieListener --- src/background.js | 14 ++++++++------ src/token-pool.js | 37 ++++++++++++------------------------- 2 files changed, 20 insertions(+), 31 deletions(-) diff --git a/src/background.js b/src/background.js index e4ed01a..bde6d8a 100644 --- a/src/background.js +++ b/src/background.js @@ -11,12 +11,7 @@ class AccessToken { tokenPool.generateTokens(); } - static async get() { - const token = AccessToken.TOKEN; - if (token) { - return token; - } - await AccessToken.refresh(); + static get() { return AccessToken.TOKEN; } @@ -55,6 +50,9 @@ const cookieListener = (changeInfo) => { if (removed) { AccessToken.destroy(); + // try to refresh the token incase remove was caused by + // token expiring + AccessToken.refresh(); return; } @@ -69,6 +67,10 @@ const lookForAccessToken = async () => { }); if (cookie) { AccessToken.set(cookie.value); + } else { + // if token is not found on startup try to refresh + // as it can just be expired + AccessToken.refresh(); }; } diff --git a/src/token-pool.js b/src/token-pool.js index e37cff1..9c64331 100644 --- a/src/token-pool.js +++ b/src/token-pool.js @@ -39,7 +39,7 @@ class TokenPool { } async generateTokens() { - const accessToken = await AccessToken.get(); + const accessToken = AccessToken.get(); if (!accessToken) { return; } @@ -53,17 +53,17 @@ class TokenPool { pretokens.push({ token, blindFactor }); } - 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(); - if (!accessToken) { - return; - } - response = await this._fetchNewTokens(accessToken, blindTokens); - } + const response = await fetch(`${API_BASE_URL}/tokens/new`, { + method: 'POST', + headers: { + 'Authorization': `Bearer ${accessToken}`, + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + blindTokens, + }), + }); + if (response.ok) { const { tokens } = await response.json(); const res = []; @@ -86,17 +86,4 @@ class TokenPool { this.tokens.push(...res); } } - - 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, - }), - }); - } } \ No newline at end of file From 38b17bb6487516096de24b77961e1992226c591a Mon Sep 17 00:00:00 2001 From: Frank Chiarulli Jr Date: Thu, 5 Nov 2020 12:08:44 -0500 Subject: [PATCH 6/9] remove extra whitespace --- src/token-pool.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/token-pool.js b/src/token-pool.js index 9c64331..41f1a24 100644 --- a/src/token-pool.js +++ b/src/token-pool.js @@ -63,7 +63,6 @@ class TokenPool { blindTokens, }), }); - if (response.ok) { const { tokens } = await response.json(); const res = []; From 86b9395693f05954346d6d3bbf0d105779de06b7 Mon Sep 17 00:00:00 2001 From: Frank Chiarulli Jr Date: Thu, 5 Nov 2020 12:15:06 -0500 Subject: [PATCH 7/9] avoid exponential growth of token pool --- src/token-pool.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/token-pool.js b/src/token-pool.js index 41f1a24..b146341 100644 --- a/src/token-pool.js +++ b/src/token-pool.js @@ -1,4 +1,5 @@ const PUBLIC_EXP = 65537; +const MIN_TOKENS = 6; function bnToBase64(bn) { return sjcl.codec.base64.fromBits(bn.toBits()); @@ -16,7 +17,7 @@ class TokenPool { async getToken() { if (this.tokens.length === 0) { await this.generateTokens(); - } else if (this.tokens.length < 6) { + } else if (this.tokens.length < MIN_TOKENS) { this.generateTokens(); } return this.tokens.pop(); @@ -39,6 +40,11 @@ class TokenPool { } async generateTokens() { + // avoid exponential growth of the token pool + if (this.tokens.length >= MIN_TOKENS) { + return; + } + const accessToken = AccessToken.get(); if (!accessToken) { return; From 9d5af50a9b52ea7167de23b829341b56f47fbdc3 Mon Sep 17 00:00:00 2001 From: Frank Chiarulli Jr Date: Thu, 5 Nov 2020 12:17:03 -0500 Subject: [PATCH 8/9] update comment --- src/token-pool.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/token-pool.js b/src/token-pool.js index b146341..0d5e70a 100644 --- a/src/token-pool.js +++ b/src/token-pool.js @@ -40,7 +40,7 @@ class TokenPool { } async generateTokens() { - // avoid exponential growth of the token pool + // avoid endless growth of the token pool if (this.tokens.length >= MIN_TOKENS) { return; } From 133de0098cbde3b467576e1d27fe50bf4b10e6a9 Mon Sep 17 00:00:00 2001 From: Frank Chiarulli Jr Date: Thu, 5 Nov 2020 12:27:25 -0500 Subject: [PATCH 9/9] add back extra refresh --- src/token-pool.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/token-pool.js b/src/token-pool.js index 0d5e70a..2290448 100644 --- a/src/token-pool.js +++ b/src/token-pool.js @@ -89,6 +89,9 @@ 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(); } } } \ No newline at end of file