Evgenii
Evgenii

Reputation: 409

Changing credentials to a proxy server on the fly

I'm developing an extension for chrome. The extension allows to pick any proxy server from a list, each proxy is required authorization. There is an issue when a user would like to connect to the same proxy server twice but with different credentials for example if a user was successfully logged in the first time the chrome remembers it and when the user would try to connect with other credentials the chrome would use credentials that were inputted in the first login.

var authCredentials = {
  username: 'Jack',
  password: 'PassForJack'
}

var auth = function () {
    return {
       authCredentials
    };
};


chrome.webRequest.onAuthRequired.addListener(auth, {
   urls: ["<all_urls>"]
 }, ["blocking"]);

// set a new proxy server for the first login
chrome.proxy.settings.set({
  value: {
      mode: 'fixed_servers',
      rules: {
        singleProxy: {
            host: 'some-proxy-server.com',
            port: 8000
        }
    }
  },
  scope: 'regular'
});


// change credentails 
authCredentials = {
  username: 'Bob',
  password: 'PassForBob'
};

// remove proxy configuration
chrome.proxy.settings.set({
  value: {
    mode: 'direct'
  },
  scope: 'regular'
});

// remove onAuthListener
chrome.webRequest.onAuthRequired.removeListener(auth)
chrome.webRequest.onAuthRequired.hasListener(auth) // returns false

chrome.webRequest.onAuthRequired.addListener(auth, {
   urls: ["<all_urls>"]
 }, ["blocking"]);

// lets re connect 
chrome.proxy.settings.set({
  value: {
    mode: 'fixed_servers',
    rules: {
        singleProxy: {
            host: 'some-proxy-server.com',
            port: 8000
        }
    }
  },
  scope: 'regular'
});

// that doesn't help the user would be logged as "Jack" but has to be as "Bob"

Upvotes: 3

Views: 1522

Answers (1)

magnump0
magnump0

Reputation: 2566

There are multiple possibilities here since the question is not very clear. I would suggest to walk through documentation for the chrome.webRequest first. But My question would be why don't you use interceptor method to check for server-credential pair ? There's a good article about adding interceptor in the background.js script of your extension which suggests to use the beforeRequest hook.

Upvotes: 2

Related Questions