Skip to content

Instantly share code, notes, and snippets.

@twolfson
Last active August 29, 2015 13:57
Show Gist options
  • Save twolfson/9543183 to your computer and use it in GitHub Desktop.
Save twolfson/9543183 to your computer and use it in GitHub Desktop.
clippercard.com playground
node_modules/
// Load in dependencies
var assert = require('assert');
var async = require('async');
var request = require('request');
var jsdom = require('jsdom');
jsdom.defaultDocumentFeatures = {
FetchExternalResources: false,
ProcessExternalResources: false,
SkipExternalResources: true
};
function main() {
// Assert that we have a username and password from env
var username = process.env.CLIPPER_USERNAME;
var password = process.env.CLIPPER_PASSWORD;
assert(username, '`CLIPPER_USERNAME` was not found as an environment variable. Please provide it.');
assert(password, '`CLIPPER_PASSWORD` was not found as an environment variable. Please provide it.');
// Attempt to log in to the form
var jar = request.jar();
// var cookie = request.cookie('bbbbbbbbbbbbbbb=BCOMLEABAAAAAAAAFCIMIPAAAAAAAAAAEADANLCDENCDAAAADAAAKKCFCKCFAAAA');
// jar.setCookie(cookie, 'www.clippercard.com');
// var cookie = request.cookie('TS6e5b59=948e412bf557eff1c522ed0706884b18b14b625295bc7a395322abf879b49ee6b5c1e21d');
// jar.setCookie(cookie, 'www.clippercard.com');
async.waterfall([
function collectCookies (cb) {
request({
jar: jar,
url: 'https://www.clippercard.com/ClipperCard/needLogin.jsf',
// url: 'https://www.clippercard.com/ClipperWeb/index.do'
}, cb);
},
function getFormData (res, body, cb) {
// Process the HTML into a DOM
jsdom.env(body, [], function (err, window) {
if (err) {
return cb(err);
}
// Grab the view state
var viewState = window.document.getElementById('javax.faces.ViewState').value;
cb(null, viewState);
});
},
function login (viewState, cb) {
request({
method: 'POST',
url: 'https://www.clippercard.com/ClipperCard/needLogin.jsf',
// jar contains JSESSIONID which we need
jar: jar,
headers: {
'Faces-Request': 'partial/ajax',
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:25.0) Gecko/20100101 Firefox/25.0'
},
form: {
mainForm: 'mainForm', // Not required
'mainForm:oUserName': username,
'mainForm:oPassword': password,
'javax.faces.ViewState': viewState,
'javax.faces.source': decodeURIComponent('mainForm%3AorderCardLogin'),
'javax.faces.partial.event': decodeURIComponent('click'),
'javax.faces.partial.execute': decodeURIComponent('mainForm%3AorderCardLogin%20mainForm%3AloginComp'),
'javax.faces.partial.render': decodeURIComponent('mainForm%3AloginComp'),
'javax.faces.behavior.event': decodeURIComponent('action'),
'javax.faces.partial.ajax': decodeURIComponent('true')
},
followRedirect: false
}, function handleLogin (err, res, body) {
// If there was an error, callback with it
if (err) {
return cb(err);
}
// Verify the content matches as expected
if (res.statusCode !== 200) {
return cb(new Error('Expected: 200, Received: ' + res.statusCode + ' from "https://www.clippercard.com/ClipperCard/needLogin.jsf". Body was: ' + body));
} else if (body !== '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<partial-response><redirect url="dashboard.jsf"></redirect></partial-response>') {
return cb(new Error('Expected: Content to contain <redirect url="dashboard.jsf"> from "https://www.clippercard.com/ClipperCard/needLogin.jsf". Body was: ' + body));
}
// Continue to the next page
cb();
});
},
function collectInfo (cb) {
request({
url: 'https://www.clippercard.com/ClipperCard/dashboard.jsf',
jar: jar
}, function handleInfo (err, res, body) {
// If there was an error, callback with it
if (err) {
return cb(err);
}
// Log the headers and info
console.log(res.statusCode, res.headers);
console.log(body.slice(0, 200));
});
}
], function handleError (err) {
// If there was an error, throw it
if (err) {
throw err;
}
});
}
main();
{
"name": "gist-clippercard-playground",
"version": "0.1.0",
"description": "clippercard.com playground",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://gist.github.com/9543183.git"
},
"author": "Todd Wolfson <todd@twolfson.com> (http://twolfson.com/)",
"license": "UNLICENSE",
"bugs": {
"url": "https://gist.github.com/9543183"
},
"homepage": "https://gist.github.com/9543183",
"private": true,
"dependencies": {
"async": "~0.2.10",
"request": "~2.34.0",
"jsdom": "~0.10.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment