Automatic Proxy Authentication

Talk about add-ons and extension development.
Post Reply
davchr
Posts: 1
Joined: May 26th, 2008, 12:54 am
Contact:

Automatic Proxy Authentication

Post by davchr »

Hy

I've tried to implement an automatic proxy authentication, which supresses the login promp and reads the username and password out of the prefs.js. Therefore I've used nsIHttpAuthenticator and implemented generateCredentials and challengeReceived. But now I have the problem, that the Proxy (Squid) logs the requests two times: one time using an empty username/password and one time using the username/password I've read out of the prefs.js (looks like FF tries to use an empty username/password pair at firest and then tries to use generateCredentails?!). So there are two log entries for every page requested: one with an empty username and one with the correct username.

The code looks like the following:

Code: Select all

//constants
const COMPONENT_CID = Components.ID("{7F3C3443-9B73-4250-B9D8-130E895A23C3}");
const COMPONENT_PRETTY_NAME = "Proxy Authentication";
const COMPONENT_CONTRACTID = "@mozilla.org/network/http-authenticator;1?scheme=basic";

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
  //encoding ....
} //encode64

var OciAuthComponent = {
  REQUEST_BASED: 1,
  CONNECTION_BASED: 2,
  REUSABLE_CREDENTIALS: 4,
  REUSABLE_CHALLENGE: 8,
  IDENTITY_IGNORED: 1024,
  IDENTITY_INCLUDES_DOMAIN: 2048,

  get authFlag() {
    return this.REUSABLE_CREDENTIALS;
  } //authFlag
}; //OciAuthComponent

OciAuthComponent.challengeReceived = function (aChannel, aChallenge, aProxyAuth, aSessionState, aContinuationState, aInvalidatesIdentity) {
  aInvalidatesIdentity.value = false;
} //challengeReceived

OciAuthComponent.generateCredentials = function (aChannel, aChallenge, aProxyAuth, aDomain, aUser, aPassword, aSessionState, aContinuationState) {

  //================possibility: out of user.js
  var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
  //get the branch for "extensions.ociauth"
  prefs = prefs.getBranch("extensions.ociauth.");

  //get the value
  var username = prefs.getCharPref("user");

  //combine username and some password (password is not necessary for this kind of proxy-auth)
  var userAndPass = username + ":" + username;

  return "Basic " + encode64(userAndPass);

}; //generateCredentials

//nsISupports
OciAuthComponent.QueryInterface = function (aIID) {
  if(aIID.equals(Components.interfaces.nsISupports) ||
     aIID.equals(Components.interfaces.nsIHttpAuthenticator) ||
    aIID.equals(Components.interfaces.nsIFactory)) {
   return this;
  }
  throw Components.results.NS_ERROR_NO_INTERFACE;
  return null;
}; //QueryInterface

//nsIFactory
OciAuthComponent.createInstance = function (aOuter, aIID) {
  if(aOuter != null)
    throw Components.results.NS_ERROR_NO_AGGREGATION;

  return this.QueryInterface(aIID);
}; //createInstance

OciAuthComponent.lockFactory = function (aLock) {};

var OciAuthModule = {
  //nsISupports
  QueryInterface: function mod_QI(aIID) {
    if(aIID.equals(Components.interfaces.nsIModule) ||
      aIID.equals(Components.interfaces.nsISupports))
     return this;

   throw Components.results.NS_ERROR_NO_INTERFACE;
  }, //QueryInterface

  //nsIModule
  getClassObject: function mod_getco(compMgr, cid, iid) {
    if(cid.equals(COMPONENT_CID))
     return OciAuthComponent.QueryInterface(iid);

   throw Components.results.NS_ERROR_NO_INTERFACE;
  }, //getClassObject

  registerSelf: function mod_req(aCompMgr, aFileSpec, aLocation, aType) {
    var compReg = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
   compReg.registerFactoryLocation(COMPONENT_CID, COMPONENT_PRETTY_NAME, COMPONENT_CONTRACTID, aFileSpec, aLocation, aType);
  }, //reqisterSelf

  unregisterSelf: function mod_unreg(aCompMgr, aLocation, aType) {
    var compReg = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
   compReg.unregisterFactoryLocation(COMPONENT_CID, aLocation);
  }, //unregisterSelf

  canUnload: function(compMgr) {
    return true;
  } //canUnload
}; //OciAuthModule

function NSGetModule(compMgr, fileSpec) {
  return OciAuthModule;
} //NSGetModule


Can anyone help me, I'm totally stuck at the moment.

Thank you very much.
davchr
Post Reply