Question about webRequest.onAuthRequired in MV3

344 views
Skip to first unread message

Lu Xu

unread,
May 30, 2023, 10:00:45 PM5/30/23
to Chromium Extensions
Hi Team,
Maybe this is an old question, but I am trying the webRequest.onAuthRequired API  in MV3 recently. My question is can I achieve both of the two goals
  1. provide a `BlockingResponse` in a promised callback to authenticate the user
  2. take no action in some other cases
for example
```
chrome.webRequest.onAuthRequired.addListener(details => {
  return new Promise(resolve => {
    if(some conditional checking) {
      // provide creds to authenticate
      resolve({authCredentials: myCredentials});
    } else {
      // take no actions
      resolve();
    }
  })
},  {urls: ["<all_urls>"]}, ["asyncBlocking"])
```
But according to my tests
  1. if we want to return `BlockingResponse`, we need to use `asyncBlocking`
  2. if we use `asyncBlocking`, seems we must provide credentials or cancel or redirect, I would like to take no actions, but if I resolve the promise with undefined, the request seems still blocked
Is there any solutions or suggestions?
Thanks a lot!

Lu Xu

unread,
Jun 1, 2023, 10:48:04 PM6/1/23
to Simeon Vincent, Chromium Extensions
Hi Simeon,
You are right, I tested again, we can not return the Promise<BlockingResponse | undefined>, I misunderstood this section 

If the optional opt_extraInfoSpec array contains the string 'asyncBlocking' instead (only allowed for onAuthRequired), the extension can generate the webRequest.BlockingResponse asynchronously.

I think the doc means we can generate the BlockingResponse async, but still need to call asyncCallback(BlockingResponse) like your snippet

My question was more focus on how do we proceed with no action, in your snippet

setTimeout(() => callback(response), 2000);

the BlockingResponse can be empty object, and this means Chrome will not take any actions

Thanks a lot!

On Wed, May 31, 2023 at 11:52 PM Simeon Vincent <sim...@chromium.org> wrote:
The snippet you shared doesn't seem to work in Chrome Canary at all. Was this implemented using the documentation on MDN? If so, Chrome's event handler uses a slightly different API signature: (details: object, asyncCallback?: function) => BlockingResponse | undefined. Note that the return value must either be BlockingResponse or undefined – Promises are not supported.

Here's an example of an asynchronous auth handler that I verified works in Canary. Using this code, try visiting Postman's basic auth test URL both with and without a query parameter.

chrome.webRequest.onAuthRequired.addListener(
(details, callback) => {
const url = new URL(details.url);
const response = {};
if (url.search == "") {
// Request doesn't have query parameters, use default credentials.
response.authCredentials = {
username: 'postman',
password: 'password',
};
} else {
// Request HAS query params, let the user enter their own credentials.
}
// Delay resolving this for a few seconds for demo purposes
setTimeout(() => callback(response), 2000);
},
['asyncBlocking']
);

Simeon - @dotproto


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/5ce1f402-766d-4a0a-ad5e-0a788d13d9abn%40chromium.org.
Reply all
Reply to author
Forward
0 new messages