From 28a70605e5fcbe3b74ca4f3f5b52493a11c5914e Mon Sep 17 00:00:00 2001 From: eight04 Date: Wed, 8 Dec 2021 20:18:42 +0800 Subject: [PATCH] WIP: don't revoke google token, add TokenError --- background/token-manager.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/background/token-manager.js b/background/token-manager.js index 605da94d..c06926d2 100644 --- a/background/token-manager.js +++ b/background/token-manager.js @@ -32,10 +32,11 @@ const tokenMan = (() => { }, tokenURL: 'https://oauth2.googleapis.com/token', scopes: ['https://www.googleapis.com/auth/drive.appdata'], - revoke: token => { - const params = {token}; - return postQuery(`https://accounts.google.com/o/oauth2/revoke?${new URLSearchParams(params)}`); - }, + // FIXME: https://github.com/openstyles/stylus/issues/1248 + // revoke: token => { + // const params = {token}; + // return postQuery(`https://accounts.google.com/o/oauth2/revoke?${new URLSearchParams(params)}`); + // }, }, onedrive: { flow: 'code', @@ -91,7 +92,7 @@ const tokenMan = (() => { } } if (!interactive) { - throw new Error(`Invalid token: ${name}`); + throw new TokenError(name, 'Token is missing'); } return authUser(k, name, interactive, hooks); }, @@ -111,9 +112,20 @@ const tokenMan = (() => { }, }; + class TokenError extends Error { + constructor(provider, message) { + super(`[${provider}] ${message}`); + this.name = 'TokenError'; + this.provider = provider; + if (Error.captureStackTrace) { + Error.captureStackTrace(this, TokenError); + } + } + } + async function refreshToken(name, k, obj) { if (!obj[k.REFRESH]) { - throw new Error('No refresh token'); + throw new TokenError(name, 'No refresh token'); } const provider = AUTH[name]; const body = { @@ -179,7 +191,7 @@ const tokenMan = (() => { new URL(finalUrl).search.slice(1) ); if (params.get('state') !== state) { - throw new Error(`Unexpected state: ${params.get('state')}, expected: ${state}`); + throw new TokenError(name, `Unexpected state: ${params.get('state')}, expected: ${state}`); } let result; if (provider.flow === 'token') {