When I wanna set the proxy in my extension, I use chrome.proxy.settings.set() Then I use the
chrome.webRequest.onAuthRequired.addListener(callbackFn, {urls: ['<all_urls>']}, ['blocking']);
const callbackFn = (details: any) => {
const username = 'someUser';
const password = 'somePass';
return {authCredentials: {username, password}};
}
But after 5mins I want to use another user creds. When I set proxy.settings.clear({}) - that's clear proxy and I have my default ip. After that I set proxy, set new onAuthRequired listener, but chrome saved somewhere my first creds, and I can't change it by onAuthRequired because chrome set my first creds to headers for proxy server. How can I delete from chrome my creds that I have set before?
I think that chrome save connection with server. Because the proxy ask for creds only after chrome reopen. How to close connection with proxy server (by chrome API)?
So I have found a solution of the problem. onAuthRequired after server 407 response, get the creds and save it to cookie. Then for every request browser add Authentication header with your creds from cookie.
This part of code remove creds cookies, and after new proxy connection the server will ask for a new creds.
let options = {};
const rootDomain = 'your.proxy.host'; // for example domain.com
options.origins = [];
options.origins.push("http://"+ rootDomain);
options.origins.push("https://"+ rootDomain);
let types = {"cookies": true};
chrome.browsingData.remove(options, types, function(){
// some code for callback function
});
For example if you have authentication in extension, you can add this code to logout function like in my application.