From 6b0add45b816f529716aa0b36ca050deeb564853 Mon Sep 17 00:00:00 2001 From: Sam Macbeth Date: Mon, 26 Oct 2020 12:17:41 +0100 Subject: [PATCH] Refresh accessToken if request for search tokens fails. --- src/background.js | 7 +++++++ src/config.js | 1 + src/manifest.json | 1 + src/token-pool.js | 3 ++- 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/background.js b/src/background.js index c9ac546..9168140 100644 --- a/src/background.js +++ b/src/background.js @@ -19,6 +19,13 @@ class AccessToken { console.warn("ACCESS_TOKEN removed") AccessToken.TOKEN = null; } + + static async refresh() { + await fetch(`${AUTH_BASE_URL}/refresh_token`, { + method: 'POST', + credentials: 'include', + }); + } } function parseJwt(token) { diff --git a/src/config.js b/src/config.js index c9ccf18..0cf63b2 100644 --- a/src/config.js +++ b/src/config.js @@ -1,3 +1,4 @@ let DEBUG = false; let API_BASE_URL = DEBUG ? 'http://localhost:5000' : 'https://ghosterysearch.com'; let SERP_BASE_URL = DEBUG ? 'http://localhost' : 'https://ghosterysearch.com'; +let AUTH_BASE_URL = 'https://consumerapi.ghostery.com/api/v2'; diff --git a/src/manifest.json b/src/manifest.json index 091e814..0356314 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -22,6 +22,7 @@ "webRequest", "webRequestBlocking", "https://www.ghostery.com/*", + "https://consumerapi.ghostery.com/*", "https://ghosterysearch.com/*", "http://localhost/*" ], diff --git a/src/token-pool.js b/src/token-pool.js index 80a2a68..44faa85 100644 --- a/src/token-pool.js +++ b/src/token-pool.js @@ -84,7 +84,8 @@ class TokenPool { console.warn(`Adding ${res.length} tokens to acquired pool`); this.tokens.push(...res); } else { - console.error("Wrong access token"); + // refresh the access token. This will call generateTokens if the refresh is successful + AccessToken.refresh(); } } } \ No newline at end of file