From d7641d9261d0cc5a7c3552ba126cf1c7bdcfe2a7 Mon Sep 17 00:00:00 2001 From: Simeon Vincent Date: Thu, 18 Jul 2024 15:46:43 -0700 Subject: [PATCH 1/5] Update for MV3 and Chrome compatibility --- stored-credentials/auth.js | 18 ++++++++++-------- stored-credentials/manifest.json | 11 ++++++++--- stored-credentials/options/options.js | 3 +++ stored-credentials/storage.js | 3 +++ stored-credentials/sw.js | 2 ++ 5 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 stored-credentials/sw.js diff --git a/stored-credentials/auth.js b/stored-credentials/auth.js index e4ffda1d..a1ab6beb 100644 --- a/stored-credentials/auth.js +++ b/stored-credentials/auth.js @@ -1,3 +1,5 @@ +// Polyfill the "browser" global in Chrome. +globalThis.browser ??= chrome; let target = "https://httpbin.org/basic-auth/*"; @@ -14,27 +16,27 @@ function completed(requestDetails) { } } -function provideCredentialsAsync(requestDetails) { - // If we have seen this request before, - // then assume our credentials were bad, +async function provideCredentialsAsync(requestDetails, asyncCallback) { + // If we have seen this request before, then assume our credentials were bad, // and give up. if (pendingRequests.indexOf(requestDetails.requestId) != -1) { console.log("bad credentials for: " + requestDetails.requestId); return {cancel: true}; - + } else { pendingRequests.push(requestDetails.requestId); console.log("providing credentials for: " + requestDetails.requestId); - // we can return a promise that will be resolved - // with the stored credentials - return browser.storage.local.get(null); + // We can respond asynchronously by calling asyncCallback and providing the + // authentication credentials. + const {authCredentials} = await browser.storage.local.get("authCredentials"); + asyncCallback({authCredentials}); } } browser.webRequest.onAuthRequired.addListener( provideCredentialsAsync, {urls: [target]}, - ["blocking"] + ["asyncBlocking"] ); browser.webRequest.onCompleted.addListener( diff --git a/stored-credentials/manifest.json b/stored-credentials/manifest.json index 65a15c8e..1429284a 100644 --- a/stored-credentials/manifest.json +++ b/stored-credentials/manifest.json @@ -1,6 +1,6 @@ { "description": "Performs basic authentication by supplying stored credentials.", - "manifest_version": 2, + "manifest_version": 3, "name": "stored-credentials", "version": "2.0", "homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/stored-credentials", @@ -15,7 +15,8 @@ }, "background": { - "scripts": ["storage.js", "auth.js"] + "scripts": ["storage.js", "auth.js"], + "service_worker": "sw.js" }, "options_ui": { @@ -25,7 +26,11 @@ "permissions": [ "webRequest", "webRequestBlocking", - "storage", + "webRequestAuthProvider", + "storage" + ], + + "host_permissions": [ "https://httpbin.org/basic-auth/*" ] } diff --git a/stored-credentials/options/options.js b/stored-credentials/options/options.js index bb0477d6..80b7d64f 100644 --- a/stored-credentials/options/options.js +++ b/stored-credentials/options/options.js @@ -1,3 +1,6 @@ +// Polyfill the "browser" global in Chrome. +globalThis.browser ??= chrome; + const usernameInput = document.querySelector("#username"); const passwordInput = document.querySelector("#password"); diff --git a/stored-credentials/storage.js b/stored-credentials/storage.js index 847d1af4..c994332c 100644 --- a/stored-credentials/storage.js +++ b/stored-credentials/storage.js @@ -1,3 +1,6 @@ +// Polyfill the "browser" global in Chrome. +globalThis.browser ??= chrome; + /* Default settings. Initialize storage to these values. */ diff --git a/stored-credentials/sw.js b/stored-credentials/sw.js new file mode 100644 index 00000000..fd40c51b --- /dev/null +++ b/stored-credentials/sw.js @@ -0,0 +1,2 @@ +// Import and synchronously execute other JavaScript files. +importScripts("storage.js", "auth.js"); From 31b0efc5a6fa33d6d61e0d5438d0c3af024de2b2 Mon Sep 17 00:00:00 2001 From: Simeon Vincent Date: Sun, 9 Mar 2025 17:11:30 -0700 Subject: [PATCH 2/5] Rewrap at 80 characters --- stored-credentials/README.md | 37 ++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/stored-credentials/README.md b/stored-credentials/README.md index b9d70f7a..f62758a8 100644 --- a/stored-credentials/README.md +++ b/stored-credentials/README.md @@ -3,30 +3,35 @@ **Although this add-on uses a stored password to authenticate to a web server, it should not be taken as an example of how to store or work securely with passwords. It's only a demonstration of how to use the -[`webRequest.onAuthRequired`](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/webRequest/onAuthRequired) API.** +[`webRequest.onAuthRequired`](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/webRequest/onAuthRequired) +API.** -This add-on uses the [`webRequest.onAuthRequired`](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/webRequest/onAuthRequired) API to log the user into -the demo site at https://httpbin.org/basic-auth/user/passwd using a stored -username and password. +This add-on uses the +[`webRequest.onAuthRequired`](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/webRequest/onAuthRequired) +API to log the user into the demo site at +https://httpbin.org/basic-auth/user/passwd using a stored username and password. -This add-on stores a username and password using the [`storage.local`](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/storage/local) API. -The default value is the correct value -for the demo site: +This add-on stores a username and password using the +[`storage.local`](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/storage/local) +API. The default value is the correct value for the demo site: username: "user" password: "passwd" -You can change the default values in the add-on's [options page](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Options_pages). +You can change the default values in the add-on's [options +page](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Options_pages). The add-on then uses `webRequest.onAuthRequired` to intercept authentication -requests from the demo site. When it gets -such a request, it fetches the stored credentials and supplies them -asynchronously. +requests from the demo site. When it gets such a request, it fetches the stored +credentials and supplies them asynchronously. To try out the add-on: -* Before installing the add-on, visit https://httpbin.org/basic-auth/user/passwd, -and see that it asks for a username and password. -* [Install the add-on](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Temporary_Installation_in_Firefox) in Firefox 54 or later. -* Visit https://httpbin.org/basic-auth/user/passwd again, and see that authentication succeeds automatically. - +* Before installing the add-on, visit + https://httpbin.org/basic-auth/user/passwd, and see that it asks for a + username and password. +* [Install the + add-on](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Temporary_Installation_in_Firefox) + in Firefox 54 or later. +* Visit https://httpbin.org/basic-auth/user/passwd again, and see that + authentication succeeds automatically. From 3d52dce7c8716171d6916fd708a26a682ce348f8 Mon Sep 17 00:00:00 2001 From: Simeon Vincent Date: Sun, 9 Mar 2025 17:14:12 -0700 Subject: [PATCH 3/5] Remove Chrome support @rebloor [noted][1] that the root readme states that "examples are tested in Firefox." This change removes Chrome support in order to accurately reflect the framing established in the README. [1]: https://github.com/mdn/webextensions-examples/pull/565#issuecomment-2241954087 --- stored-credentials/manifest.json | 3 +-- stored-credentials/sw.js | 2 -- 2 files changed, 1 insertion(+), 4 deletions(-) delete mode 100644 stored-credentials/sw.js diff --git a/stored-credentials/manifest.json b/stored-credentials/manifest.json index 1429284a..11961c87 100644 --- a/stored-credentials/manifest.json +++ b/stored-credentials/manifest.json @@ -15,8 +15,7 @@ }, "background": { - "scripts": ["storage.js", "auth.js"], - "service_worker": "sw.js" + "scripts": ["storage.js", "auth.js"] }, "options_ui": { diff --git a/stored-credentials/sw.js b/stored-credentials/sw.js deleted file mode 100644 index fd40c51b..00000000 --- a/stored-credentials/sw.js +++ /dev/null @@ -1,2 +0,0 @@ -// Import and synchronously execute other JavaScript files. -importScripts("storage.js", "auth.js"); From 7bc80b7fae65a7fbbbde9f1495d79d074f161998 Mon Sep 17 00:00:00 2001 From: Simeon Vincent Date: Sun, 9 Mar 2025 17:20:19 -0700 Subject: [PATCH 4/5] Revert to MV2 --- stored-credentials/auth.js | 4 ---- stored-credentials/manifest.json | 7 ++----- stored-credentials/options/options.js | 3 --- stored-credentials/storage.js | 3 --- 4 files changed, 2 insertions(+), 15 deletions(-) diff --git a/stored-credentials/auth.js b/stored-credentials/auth.js index a1ab6beb..226a163a 100644 --- a/stored-credentials/auth.js +++ b/stored-credentials/auth.js @@ -1,8 +1,4 @@ -// Polyfill the "browser" global in Chrome. -globalThis.browser ??= chrome; - let target = "https://httpbin.org/basic-auth/*"; - let pendingRequests = []; /* diff --git a/stored-credentials/manifest.json b/stored-credentials/manifest.json index 11961c87..5650e235 100644 --- a/stored-credentials/manifest.json +++ b/stored-credentials/manifest.json @@ -1,6 +1,6 @@ { "description": "Performs basic authentication by supplying stored credentials.", - "manifest_version": 3, + "manifest_version": 2, "name": "stored-credentials", "version": "2.0", "homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/stored-credentials", @@ -26,10 +26,7 @@ "webRequest", "webRequestBlocking", "webRequestAuthProvider", - "storage" - ], - - "host_permissions": [ + "storage", "https://httpbin.org/basic-auth/*" ] } diff --git a/stored-credentials/options/options.js b/stored-credentials/options/options.js index 80b7d64f..bb0477d6 100644 --- a/stored-credentials/options/options.js +++ b/stored-credentials/options/options.js @@ -1,6 +1,3 @@ -// Polyfill the "browser" global in Chrome. -globalThis.browser ??= chrome; - const usernameInput = document.querySelector("#username"); const passwordInput = document.querySelector("#password"); diff --git a/stored-credentials/storage.js b/stored-credentials/storage.js index c994332c..847d1af4 100644 --- a/stored-credentials/storage.js +++ b/stored-credentials/storage.js @@ -1,6 +1,3 @@ -// Polyfill the "browser" global in Chrome. -globalThis.browser ??= chrome; - /* Default settings. Initialize storage to these values. */ From 9698240b0ceedb08ce00d9a361a21a19144ed117 Mon Sep 17 00:00:00 2001 From: Simeon Vincent Date: Sun, 9 Mar 2025 17:24:42 -0700 Subject: [PATCH 5/5] Remove extra space --- stored-credentials/auth.js | 1 - 1 file changed, 1 deletion(-) diff --git a/stored-credentials/auth.js b/stored-credentials/auth.js index 226a163a..d7168ef3 100644 --- a/stored-credentials/auth.js +++ b/stored-credentials/auth.js @@ -18,7 +18,6 @@ async function provideCredentialsAsync(requestDetails, asyncCallback) { if (pendingRequests.indexOf(requestDetails.requestId) != -1) { console.log("bad credentials for: " + requestDetails.requestId); return {cancel: true}; - } else { pendingRequests.push(requestDetails.requestId); console.log("providing credentials for: " + requestDetails.requestId);