Skip to content

Commit

Permalink
fix(Basic Authentication): Add custom http headers for handling basic…
Browse files Browse the repository at this point in the history
… authentication
  • Loading branch information
stevebritton committed Feb 12, 2018
1 parent ff78b7c commit 504113c
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions lib/screenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ var renderUrlsToFile = function(ph, options, urls, callbackPerUrl, callbackFinal

jQuery('script').map(function() {

if( jQuery(this).attr('src') ){
if (jQuery(this).attr('src')) {
data.scripts.push({
src: jQuery(this).attr('src'),
text: jQuery(this).html()
Expand Down Expand Up @@ -416,12 +416,17 @@ var renderUrlsToFile = function(ph, options, urls, callbackPerUrl, callbackFinal

retrievePage = function() {

var folder, url, viewportIndex;
var folder, httpHeaders, url, viewportIndex;

if (urls.length > 0) {

utils.log.verbose('Function: retrievePage');


httpHeaders = {
'Connection':'keep-alive'
};

viewportIndex = options.viewports.length - 1;

url = urls.shift();
Expand All @@ -435,14 +440,15 @@ var renderUrlsToFile = function(ph, options, urls, callbackPerUrl, callbackFinal
utils.log.verbose('PhantomJS - createPage');

// do we have stored credentials?
if (options.domain.uri.auth && isLoggedIn === false) {
if ( options.domain.uri.auth ) {

utils.log.verbose('Setting Authentication');
httpHeaders.Authorization = 'Basic ' + Buffer.from(options.domain.uri.username + ':'+ options.domain.uri.password, 'binary').toString('base64');

page.setting('userName', options.domain.uri.username);
page.setting('password', options.domain.uri.password);
utils.log.verbose('Setting Authorization header:', httpHeaders.Authorization);
}

page.property('customHeaders', httpHeaders);

page.setting('userAgent', getUserAgent(options.viewports[viewportIndex].viewport));

page.property('viewportSize', {
Expand All @@ -454,7 +460,7 @@ var renderUrlsToFile = function(ph, options, urls, callbackPerUrl, callbackFinal
if (utils.log.isDebug()) {

// handles console messages from within an page.evaluate function or inject.js file
page.property('onConsoleMessage', function(msg) {
page.on('onConsoleMessage', function(msg) {
console.log(msg);
});

Expand Down Expand Up @@ -495,15 +501,14 @@ var renderUrlsToFile = function(ph, options, urls, callbackPerUrl, callbackFinal
renderViewPort(options.viewports.length, page, pageStatus, url);
});

// check for Microsoft Online parts in URL
} else if ( options.domain.uri.password && options.domain.uri.auth && (url.indexOf('.sharepoint.com') > -1 || url.indexOf('.azurewebsites') >-1) && !isLoggedIn ) {
// check for Microsoft Online parts in URL
} else if (options.domain.uri.password && options.domain.uri.auth && (url.indexOf('.sharepoint.com') > -1 || url.indexOf('.azurewebsites') > -1) && !isLoggedIn) {

authMicrosoftOnline(page, function() {
renderViewPort(options.viewports.length, page, pageStatus, url);
});

} else if (pageStatus === 'success') {

renderViewPort(options.viewports.length, page, pageStatus, url);

} else {
Expand Down

0 comments on commit 504113c

Please sign in to comment.