From 07ba44cc2c369770b9e675d0b9ceaa46e809d8d8 Mon Sep 17 00:00:00 2001 From: eight Date: Mon, 31 Aug 2020 16:38:18 +0800 Subject: [PATCH] Change: switch to launchWebAuthFlow polyfill (#1017) * WIP: add webextLaunchWebAuthFlow * Change: switch to webextLaunchWebAuthFlow * Bump dependencies * Fix: use minimized version * Fix: wrong call to promisifyChrome --- background/token-manager.js | 12 +++++----- manifest.json | 1 + package-lock.json | 5 +++++ package.json | 3 ++- tools/build-vendor.js | 3 +++ vendor/webext-launch-web-auth-flow/LICENSE | 22 +++++++++++++++++++ vendor/webext-launch-web-auth-flow/README.md | 5 +++++ .../webext-launch-web-auth-flow.min.js | 2 ++ 8 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 vendor/webext-launch-web-auth-flow/LICENSE create mode 100644 vendor/webext-launch-web-auth-flow/README.md create mode 100644 vendor/webext-launch-web-auth-flow/webext-launch-web-auth-flow.min.js diff --git a/background/token-manager.js b/background/token-manager.js index 8be21874..63f69652 100644 --- a/background/token-manager.js +++ b/background/token-manager.js @@ -1,10 +1,11 @@ -/* global chromeLocal promisifyChrome FIREFOX */ +/* global chromeLocal promisifyChrome webextLaunchWebAuthFlow FIREFOX */ /* exported tokenManager */ 'use strict'; const tokenManager = (() => { promisifyChrome({ - identity: ['launchWebAuthFlow'], + 'windows': ['create', 'update', 'remove'], + 'tabs': ['create', 'update', 'remove'] }); const AUTH = { dropbox: { @@ -160,9 +161,10 @@ const tokenManager = (() => { Object.assign(query, provider.authQuery); } const url = `${provider.authURL}?${stringifyQuery(query)}`; - return browser.identity.launchWebAuthFlow({ + return webextLaunchWebAuthFlow({ url, - interactive + interactive, + redirect_uri: query.redirect_uri }) .then(url => { const params = new URLSearchParams( @@ -185,7 +187,7 @@ const tokenManager = (() => { code, grant_type: 'authorization_code', client_id: provider.clientId, - redirect_uri: provider.redirect_uri || chrome.identity.getRedirectURL() + redirect_uri: query.redirect_uri }; if (provider.clientSecret) { body.client_secret = provider.clientSecret; diff --git a/manifest.json b/manifest.json index 89a62be7..9d59db44 100644 --- a/manifest.json +++ b/manifest.json @@ -38,6 +38,7 @@ "vendor/semver-bundle/semver.js", "vendor/db-to-cloud/db-to-cloud.min.js", "vendor/uuid/uuid.min.js", + "vendor/webext-launch-web-auth-flow/webext-launch-web-auth-flow.min.js", "background/token-manager.js", "background/sync.js", "background/content-scripts.js", diff --git a/package-lock.json b/package-lock.json index 4872bfa8..8ecf3451 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7934,6 +7934,11 @@ } } }, + "webext-launch-web-auth-flow": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/webext-launch-web-auth-flow/-/webext-launch-web-auth-flow-0.1.0.tgz", + "integrity": "sha512-3W8ANT9/6uL6NX5SiaKQee439dfiS1NT8wSc+vmjly/2MmH7FBqGFBXLfBFw296w8OOqHNPnEdNcBkDGJQkDgg==" + }, "webext-tx-fix": { "version": "0.3.3", "resolved": "https://registry.npmjs.org/webext-tx-fix/-/webext-tx-fix-0.3.3.tgz", diff --git a/package.json b/package.json index 49d423d4..09ef363a 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "stylelint-bundle": "^8.0.0", "stylus-lang-bundle": "^0.54.5", "usercss-meta": "^0.9.0", - "uuid": "^8.1.0" + "uuid": "^8.1.0", + "webext-launch-web-auth-flow": "^0.1.0" }, "devDependencies": { "archiver": "^4.0.1", diff --git a/tools/build-vendor.js b/tools/build-vendor.js index 1e3d5c24..639a74ab 100644 --- a/tools/build-vendor.js +++ b/tools/build-vendor.js @@ -62,6 +62,9 @@ const files = { ], 'uuid': [ 'dist/umd/uuidv4.min.js → uuid.min.js' + ], + 'webext-launch-web-auth-flow': [ + 'dist/webext-launch-web-auth-flow.min.js → webext-launch-web-auth-flow.min.js' ] }; diff --git a/vendor/webext-launch-web-auth-flow/LICENSE b/vendor/webext-launch-web-auth-flow/LICENSE new file mode 100644 index 00000000..792e1731 --- /dev/null +++ b/vendor/webext-launch-web-auth-flow/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2020 eight + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/vendor/webext-launch-web-auth-flow/README.md b/vendor/webext-launch-web-auth-flow/README.md new file mode 100644 index 00000000..821ceade --- /dev/null +++ b/vendor/webext-launch-web-auth-flow/README.md @@ -0,0 +1,5 @@ +## webext-launch-web-auth-flow v0.1.0 + +Following files are copied from npm (node_modules): + +* webext-launch-web-auth-flow.min.js: dist\webext-launch-web-auth-flow.min.js diff --git a/vendor/webext-launch-web-auth-flow/webext-launch-web-auth-flow.min.js b/vendor/webext-launch-web-auth-flow/webext-launch-web-auth-flow.min.js new file mode 100644 index 00000000..7e8f7587 --- /dev/null +++ b/vendor/webext-launch-web-auth-flow/webext-launch-web-auth-flow.min.js @@ -0,0 +1,2 @@ +var webextLaunchWebAuthFlow=function(){"use strict";function e(e,r,t,n,o,i,s){try{var u=e[i](s),a=u.value}catch(e){return void t(e)}u.done?r(a):Promise.resolve(a).then(n,o)}function r(r){return function(){var t=this,n=arguments;return new Promise((function(o,i){var s=r.apply(t,n);function u(r){e(s,o,i,u,a,"next",r)}function a(r){e(s,o,i,u,a,"throw",r)}u(void 0)}))}}function t(e){return n.apply(this,arguments)}function n(){return(n=r((function*(e){if(browser.windows)return yield browser.windows.create(e);const r={active:"minimized"!==e.state,url:e.url};return{tabs:[yield browser.tabs.create(r)]}}))).apply(this,arguments)}function o(e,r,t){return i.apply(this,arguments)}function i(){return(i=r((function*(e,r,t){return e?yield browser.windows.update(e,t):yield browser.tabs.update(r,{active:t.focused})}))).apply(this,arguments)}function s(e,r){return u.apply(this,arguments)}function u(){return(u=r((function*(e,r){return e?yield browser.windows.remove(e):yield browser.tabs.remove(r)}))).apply(this,arguments)}function a(){const e={};return e.promise=new Promise((r,t)=>{e.resolve=r,e.reject=t}),e}function c(){return(c=r((function*({url:e,redirect_uri:r,interactive:n=!1}){const i=yield t({type:"popup",url:e,state:"minimized"}),u=i.id,c=i.tabs[0].id,d=a(),l=d.promise,w=d.resolve,b=d.reject;browser.webRequest.onBeforeRequest.addListener(f,{urls:["*://*/*"],tabId:c,types:["main_frame"]},["blocking"]),browser.webNavigation.onDOMContentLoaded.addListener(p),browser.tabs.onRemoved.addListener(v);try{return yield l}finally{browser.webRequest.onBeforeRequest.removeListener(f),browser.webNavigation.onDOMContentLoaded.removeListener(p),browser.tabs.onRemoved.removeListener(v),s(u,c).catch(e=>console.error(e))}function f(e){if(!e.frameId&&e.tabId===c&&e.url.startsWith(r))return w(e.url),{cancel:!0}}function p(e){e.frameId||e.tabId!==c||(n?o(u,c,{focused:!0,state:"normal"}).catch(e=>console.error(e)):b(new Error("User interaction required")),browser.webNavigation.onDOMContentLoaded.removeListener(p))}function v(e){e===c&&b(new Error("Canceled by user"))}}))).apply(this,arguments)}return function(e){return c.apply(this,arguments)}}(); +//# sourceMappingURL=webext-launch-web-auth-flow.min.js.map