Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions stored-credentials/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
API. The default value is the correct value for the demo site:
API. The default values are the correct ones 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.
17 changes: 7 additions & 10 deletions stored-credentials/auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

let target = "https://httpbin.org/basic-auth/*";

let pendingRequests = [];

/*
Expand All @@ -14,27 +12,26 @@ 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(
Expand Down
1 change: 1 addition & 0 deletions stored-credentials/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"permissions": [
"webRequest",
"webRequestBlocking",
"webRequestAuthProvider",
"storage",
"https://httpbin.org/basic-auth/*"
]
Expand Down
Loading