IAmHomes
IAmHomes

Reputation: 523

Basic Authentication with CasperJS

Casperjs has some issue as it's not authenticating page when loaded initially, below is my code.

var casper = require('casper').create({
  verbose: false,
  logLevel: 'debug',
  pageSettings: {
    loadImages: false, // The WebPage instance used by Casper will
    loadPlugins: false, // use these settings
  }
});

// print out all the messages in the headless browser context
casper.on('remote.message', function(msg) {
  this.echo('remote message caught: ' + msg);
});

// print out all the messages in the headless browser context
casper.on("page.error", function(msg, trace) {
  this.echo("Page Error: " + msg, "ERROR");
});

//casper.options.viewportSize = {width: 1366, height: 667};

casper.start();
casper.options.pageSettings = {
  customHeaders:{
    'Authorization':'Basic '+btoa('username:password')
  }
}

casper.thenOpen('https://www.dmr.nd.gov/oilgas/basic/getwellprod.asp', function(status) {
  if (status !== 'success') {
    console.log('Unable to access network');
  } else {
    console.log("Getting Authenticated");
    this.echo(this.getTitle());
  }
});

casper.run();

I also tried using setHttpAuth with no luck. Here the Phantom version and resource.error message - Phantom version is 1.9.2 and ErrorCode is 6 and description is SSL handshake failed

Upvotes: 0

Views: 1402

Answers (1)

Artjom B.
Artjom B.

Reputation: 61952

POODLE

The error message from the resource.error suggests that it is a POODLE bug. PhantomJS < 1.9.8 uses SSLv3 by default, but because of POODLE many web servers disabled SSLv3 support. So you need to tell PhantomJS/CasperJS that TLS should be used:

--ssl-protocol=tlsv1

You can also throw --ignore-ssl-errors=true in for good measure.

More information here: CasperJS/PhantomJS doesn't load https page

Basic Auth

There is no customHeaders option for pageSettings. You probably meant to use the PhantomJS' customHeaders option:

casper.page.customHeaders:{
    'Authorization': 'Basic '+btoa('username:password')
};

A better way would be to use the appropriate settings for this:

casper.options.pageSettings = {
    userName: username,
    password: password
};

You can also define this during creation.

If this doesn't solve your problem, check with the resource.requested and resource.received events whether the headers are set.

Callback arguments

Another issue with your code is that there is no status for the callback of thenOpen and other step functions. The last successfully loaded resource object is passed into the callback and not a string. So you cannot detect whether the page was loaded successfully using that.

Upvotes: 2

Related Questions