2021-06-29 09:36:59 +00:00
|
|
|
/* global FIREFOX getActiveTab waitForTabUrl URLS */// toolbox.js
|
2021-01-01 14:27:58 +00:00
|
|
|
/* global chromeLocal */// storage-util.js
|
2019-11-05 19:30:45 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/* exported tokenMan */
|
|
|
|
const tokenMan = (() => {
|
2019-11-05 19:30:45 +00:00
|
|
|
const AUTH = {
|
|
|
|
dropbox: {
|
|
|
|
flow: 'token',
|
|
|
|
clientId: 'zg52vphuapvpng9',
|
|
|
|
authURL: 'https://www.dropbox.com/oauth2/authorize',
|
|
|
|
tokenURL: 'https://api.dropboxapi.com/oauth2/token',
|
|
|
|
revoke: token =>
|
|
|
|
fetch('https://api.dropboxapi.com/2/auth/token/revoke', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
2020-11-18 11:17:15 +00:00
|
|
|
'Authorization': `Bearer ${token}`,
|
|
|
|
},
|
|
|
|
}),
|
2019-11-05 19:30:45 +00:00
|
|
|
},
|
|
|
|
google: {
|
|
|
|
flow: 'code',
|
|
|
|
clientId: '283762574871-d4u58s4arra5jdan2gr00heasjlttt1e.apps.googleusercontent.com',
|
|
|
|
clientSecret: 'J0nc5TlR_0V_ex9-sZk-5faf',
|
|
|
|
authURL: 'https://accounts.google.com/o/oauth2/v2/auth',
|
|
|
|
authQuery: {
|
|
|
|
// NOTE: Google needs 'prompt' parameter to deliver multiple refresh
|
|
|
|
// tokens for multiple machines.
|
|
|
|
// https://stackoverflow.com/q/18519185
|
|
|
|
access_type: 'offline',
|
2020-11-18 11:17:15 +00:00
|
|
|
prompt: 'consent',
|
2019-11-05 19:30:45 +00:00
|
|
|
},
|
|
|
|
tokenURL: 'https://oauth2.googleapis.com/token',
|
|
|
|
scopes: ['https://www.googleapis.com/auth/drive.appdata'],
|
2021-12-09 04:00:38 +00:00
|
|
|
// 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)}`);
|
|
|
|
// },
|
2019-11-05 19:30:45 +00:00
|
|
|
},
|
|
|
|
onedrive: {
|
|
|
|
flow: 'code',
|
|
|
|
clientId: '3864ce03-867c-4ad8-9856-371a097d47b1',
|
|
|
|
clientSecret: '9Pj=TpsrStq8K@1BiwB9PIWLppM:@s=w',
|
|
|
|
authURL: 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
|
|
|
|
tokenURL: 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
|
2020-11-18 11:17:15 +00:00
|
|
|
scopes: ['Files.ReadWrite.AppFolder', 'offline_access'],
|
|
|
|
},
|
2021-06-29 09:36:59 +00:00
|
|
|
userstylesworld: {
|
|
|
|
flow: 'code',
|
|
|
|
clientId: 'zeDmKhJIfJqULtcrGMsWaxRtWHEimKgS',
|
|
|
|
clientSecret: 'wqHsvTuThQmXmDiVvOpZxPwSIbyycNFImpAOTxjaIRqDbsXcTOqrymMJKsOMuibFaij' +
|
|
|
|
'ZZAkVYTDbLkQuYFKqgpMsMlFlgwQOYHvHFbgxQHDTwwdOroYhOwFuekCwXUlk',
|
2021-07-02 00:12:24 +00:00
|
|
|
authURL: URLS.usw + 'api/oauth/style/link',
|
|
|
|
tokenURL: URLS.usw + 'api/oauth/token',
|
2021-06-29 09:36:59 +00:00
|
|
|
redirect_uri: 'https://gusted.xyz/callback_helper/',
|
|
|
|
},
|
2019-11-05 19:30:45 +00:00
|
|
|
};
|
|
|
|
const NETWORK_LATENCY = 30; // seconds
|
2022-02-03 10:06:29 +00:00
|
|
|
const DEFAULT_REDIRECT_URI = 'https://clngdbkpkpeebahjckkjfobafhncgmne.chromiumapp.org/';
|
2019-11-05 19:30:45 +00:00
|
|
|
|
2021-02-22 12:12:19 +00:00
|
|
|
let alwaysUseTab = FIREFOX ? false : null;
|
|
|
|
|
2021-12-09 04:00:38 +00:00
|
|
|
class TokenError extends Error {
|
|
|
|
constructor(provider, message) {
|
|
|
|
super(`[${provider}] ${message}`);
|
|
|
|
this.name = 'TokenError';
|
|
|
|
this.provider = provider;
|
|
|
|
if (Error.captureStackTrace) {
|
|
|
|
Error.captureStackTrace(this, TokenError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
return {
|
2019-11-05 19:30:45 +00:00
|
|
|
|
2021-07-30 12:44:06 +00:00
|
|
|
buildKeys(name, hooks) {
|
|
|
|
const prefix = `secure/token/${hooks ? hooks.keyName(name) : name}/`;
|
2021-01-01 14:27:58 +00:00
|
|
|
const k = {
|
2021-07-30 12:44:06 +00:00
|
|
|
TOKEN: `${prefix}token`,
|
|
|
|
EXPIRE: `${prefix}expire`,
|
|
|
|
REFRESH: `${prefix}refresh`,
|
2021-01-01 14:27:58 +00:00
|
|
|
};
|
|
|
|
k.LIST = Object.values(k);
|
|
|
|
return k;
|
|
|
|
},
|
2019-11-05 19:30:45 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
getClientId(name) {
|
|
|
|
return AUTH[name].clientId;
|
|
|
|
},
|
2019-11-05 19:30:45 +00:00
|
|
|
|
2021-07-30 12:44:06 +00:00
|
|
|
async getToken(name, interactive, hooks) {
|
|
|
|
const k = tokenMan.buildKeys(name, hooks);
|
2021-01-01 14:27:58 +00:00
|
|
|
const obj = await chromeLocal.get(k.LIST);
|
|
|
|
if (obj[k.TOKEN]) {
|
2019-11-05 19:30:45 +00:00
|
|
|
if (!obj[k.EXPIRE] || Date.now() < obj[k.EXPIRE]) {
|
|
|
|
return obj[k.TOKEN];
|
|
|
|
}
|
|
|
|
if (obj[k.REFRESH]) {
|
2021-02-14 15:24:49 +00:00
|
|
|
return refreshToken(name, k, obj);
|
2019-11-05 19:30:45 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
2021-02-14 15:24:49 +00:00
|
|
|
if (!interactive) {
|
2021-12-09 04:00:38 +00:00
|
|
|
throw new TokenError(name, 'Token is missing');
|
2021-02-14 15:24:49 +00:00
|
|
|
}
|
2021-07-30 12:44:06 +00:00
|
|
|
return authUser(k, name, interactive, hooks);
|
2021-01-01 14:27:58 +00:00
|
|
|
},
|
2019-11-05 19:30:45 +00:00
|
|
|
|
2021-07-30 12:44:06 +00:00
|
|
|
async revokeToken(name, hooks) {
|
2021-01-01 14:27:58 +00:00
|
|
|
const provider = AUTH[name];
|
2021-07-30 12:44:06 +00:00
|
|
|
const k = tokenMan.buildKeys(name, hooks);
|
2021-01-01 14:27:58 +00:00
|
|
|
if (provider.revoke) {
|
|
|
|
try {
|
|
|
|
const token = await chromeLocal.getValue(k.TOKEN);
|
|
|
|
if (token) await provider.revoke(token);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
2020-11-11 11:53:40 +00:00
|
|
|
}
|
2019-11-05 19:30:45 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
await chromeLocal.remove(k.LIST);
|
|
|
|
},
|
|
|
|
};
|
2019-11-05 19:30:45 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
async function refreshToken(name, k, obj) {
|
2019-11-05 19:30:45 +00:00
|
|
|
if (!obj[k.REFRESH]) {
|
2021-12-09 04:00:38 +00:00
|
|
|
throw new TokenError(name, 'No refresh token');
|
2019-11-05 19:30:45 +00:00
|
|
|
}
|
|
|
|
const provider = AUTH[name];
|
|
|
|
const body = {
|
|
|
|
client_id: provider.clientId,
|
|
|
|
refresh_token: obj[k.REFRESH],
|
|
|
|
grant_type: 'refresh_token',
|
2020-11-18 11:17:15 +00:00
|
|
|
scope: provider.scopes.join(' '),
|
2019-11-05 19:30:45 +00:00
|
|
|
};
|
|
|
|
if (provider.clientSecret) {
|
|
|
|
body.client_secret = provider.clientSecret;
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
const result = await postQuery(provider.tokenURL, body);
|
|
|
|
if (!result.refresh_token) {
|
|
|
|
// reuse old refresh token
|
|
|
|
result.refresh_token = obj[k.REFRESH];
|
|
|
|
}
|
|
|
|
return handleTokenResult(result, k);
|
2019-11-05 19:30:45 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 12:44:06 +00:00
|
|
|
async function authUser(keys, name, interactive = false, hooks = null) {
|
2022-03-31 10:56:51 +00:00
|
|
|
await require(['/vendor/webext-launch-web-auth-flow/webext-launch-web-auth-flow']);
|
2021-01-01 14:27:58 +00:00
|
|
|
/* global webextLaunchWebAuthFlow */
|
2019-11-05 19:30:45 +00:00
|
|
|
const provider = AUTH[name];
|
|
|
|
const state = Math.random().toFixed(8).slice(2);
|
2021-07-30 12:44:06 +00:00
|
|
|
const query = {
|
2019-11-05 19:30:45 +00:00
|
|
|
response_type: provider.flow,
|
|
|
|
client_id: provider.clientId,
|
2022-02-03 10:06:29 +00:00
|
|
|
redirect_uri: provider.redirect_uri || DEFAULT_REDIRECT_URI,
|
2020-11-18 11:17:15 +00:00
|
|
|
state,
|
2021-07-30 12:44:06 +00:00
|
|
|
};
|
2019-11-05 19:30:45 +00:00
|
|
|
if (provider.scopes) {
|
|
|
|
query.scope = provider.scopes.join(' ');
|
|
|
|
}
|
|
|
|
if (provider.authQuery) {
|
|
|
|
Object.assign(query, provider.authQuery);
|
|
|
|
}
|
2021-02-22 12:12:19 +00:00
|
|
|
if (alwaysUseTab == null) {
|
|
|
|
alwaysUseTab = await detectVivaldiWebRequestBug();
|
|
|
|
}
|
2021-07-30 12:44:06 +00:00
|
|
|
if (hooks) hooks.query(query);
|
2020-10-13 18:14:54 +00:00
|
|
|
const url = `${provider.authURL}?${new URLSearchParams(query)}`;
|
2021-07-30 12:44:06 +00:00
|
|
|
const width = Math.min(screen.availWidth - 100, 800);
|
|
|
|
const height = Math.min(screen.availHeight - 100, 800);
|
|
|
|
const wnd = await browser.windows.getLastFocused();
|
2021-01-01 14:27:58 +00:00
|
|
|
const finalUrl = await webextLaunchWebAuthFlow({
|
2019-11-05 19:30:45 +00:00
|
|
|
url,
|
2021-02-22 12:12:19 +00:00
|
|
|
alwaysUseTab,
|
2020-08-31 08:38:18 +00:00
|
|
|
interactive,
|
2020-11-18 11:17:15 +00:00
|
|
|
redirect_uri: query.redirect_uri,
|
2021-07-30 12:44:06 +00:00
|
|
|
windowOptions: Object.assign({
|
2021-02-22 12:12:19 +00:00
|
|
|
state: 'normal',
|
2021-07-30 12:44:06 +00:00
|
|
|
width,
|
|
|
|
height,
|
|
|
|
}, wnd.state !== 'minimized' && {
|
|
|
|
// Center the popup to the current window
|
|
|
|
top: Math.ceil(wnd.top + (wnd.height - width) / 2),
|
|
|
|
left: Math.ceil(wnd.left + (wnd.width - width) / 2),
|
|
|
|
}),
|
2021-01-01 14:27:58 +00:00
|
|
|
});
|
|
|
|
const params = new URLSearchParams(
|
|
|
|
provider.flow === 'token' ?
|
|
|
|
new URL(finalUrl).hash.slice(1) :
|
|
|
|
new URL(finalUrl).search.slice(1)
|
|
|
|
);
|
|
|
|
if (params.get('state') !== state) {
|
2021-12-09 04:00:38 +00:00
|
|
|
throw new TokenError(name, `Unexpected state: ${params.get('state')}, expected: ${state}`);
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
|
|
|
let result;
|
|
|
|
if (provider.flow === 'token') {
|
|
|
|
const obj = {};
|
|
|
|
for (const [key, value] of params) {
|
|
|
|
obj[key] = value;
|
|
|
|
}
|
|
|
|
result = obj;
|
|
|
|
} else {
|
|
|
|
const code = params.get('code');
|
|
|
|
const body = {
|
|
|
|
code,
|
|
|
|
grant_type: 'authorization_code',
|
|
|
|
client_id: provider.clientId,
|
|
|
|
redirect_uri: query.redirect_uri,
|
2021-06-29 09:36:59 +00:00
|
|
|
state,
|
2021-01-01 14:27:58 +00:00
|
|
|
};
|
|
|
|
if (provider.clientSecret) {
|
|
|
|
body.client_secret = provider.clientSecret;
|
|
|
|
}
|
|
|
|
result = await postQuery(provider.tokenURL, body);
|
|
|
|
}
|
2021-07-30 12:44:06 +00:00
|
|
|
return handleTokenResult(result, keys);
|
2019-11-05 19:30:45 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
async function handleTokenResult(result, k) {
|
|
|
|
await chromeLocal.set({
|
2019-11-05 19:30:45 +00:00
|
|
|
[k.TOKEN]: result.access_token,
|
2021-01-01 14:27:58 +00:00
|
|
|
[k.EXPIRE]: result.expires_in
|
|
|
|
? Date.now() + (result.expires_in - NETWORK_LATENCY) * 1000
|
|
|
|
: undefined,
|
2020-11-18 11:17:15 +00:00
|
|
|
[k.REFRESH]: result.refresh_token,
|
2021-01-01 14:27:58 +00:00
|
|
|
});
|
|
|
|
return result.access_token;
|
2019-11-05 19:30:45 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
async function postQuery(url, body) {
|
2019-11-05 19:30:45 +00:00
|
|
|
const options = {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
2020-11-18 11:17:15 +00:00
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
2020-10-13 18:14:54 +00:00
|
|
|
},
|
|
|
|
body: body ? new URLSearchParams(body) : null,
|
2019-11-05 19:30:45 +00:00
|
|
|
};
|
2021-01-01 14:27:58 +00:00
|
|
|
const r = await fetch(url, options);
|
|
|
|
if (r.ok) {
|
|
|
|
return r.json();
|
|
|
|
}
|
|
|
|
const text = await r.text();
|
|
|
|
const err = new Error(`Failed to fetch (${r.status}): ${text}`);
|
|
|
|
err.code = r.status;
|
|
|
|
throw err;
|
2019-11-05 19:30:45 +00:00
|
|
|
}
|
2021-02-22 12:12:19 +00:00
|
|
|
|
|
|
|
async function detectVivaldiWebRequestBug() {
|
|
|
|
// Workaround for https://github.com/openstyles/stylus/issues/1182
|
|
|
|
// Note that modern Vivaldi isn't exposed in `navigator.userAgent` but it adds `extData` to tabs
|
|
|
|
const anyTab = await getActiveTab() || (await browser.tabs.query({}))[0];
|
2022-06-04 16:20:20 +00:00
|
|
|
if (anyTab && !(anyTab.extData || anyTab.vivExtData)) {
|
2021-02-22 12:12:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let bugged = true;
|
|
|
|
const TEST_URL = chrome.runtime.getURL('manifest.json');
|
|
|
|
const check = ({url}) => {
|
|
|
|
bugged = url !== TEST_URL;
|
|
|
|
};
|
|
|
|
chrome.webRequest.onBeforeRequest.addListener(check, {urls: [TEST_URL], types: ['main_frame']});
|
|
|
|
const {tabs: [tab]} = await browser.windows.create({
|
|
|
|
type: 'popup',
|
|
|
|
state: 'minimized',
|
|
|
|
url: TEST_URL,
|
|
|
|
});
|
|
|
|
await waitForTabUrl(tab);
|
|
|
|
chrome.windows.remove(tab.windowId);
|
|
|
|
chrome.webRequest.onBeforeRequest.removeListener(check);
|
|
|
|
return bugged;
|
|
|
|
}
|
2019-11-05 19:30:45 +00:00
|
|
|
})();
|