Created
October 26, 2013 11:19
-
-
Save ralt/7168284 to your computer and use it in GitHub Desktop.
SO Login using node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'), | |
https = require('https'), | |
jsdom = require('jsdom'), | |
url = require('url'), | |
req; | |
req = http.request({ | |
method: 'POST', | |
host: 'stackoverflow.com', | |
path: '/users/signin', | |
port: 80, | |
headers: { | |
Accept: '*/*', | |
Cookie: 'gauthed=1' | |
} | |
}, function(res) { | |
var chunk = ''; | |
res.on('data', function(c) { | |
chunk += c; | |
}); | |
res.on('end', function() { | |
callForm(chunk); | |
}); | |
}); | |
req.on('error', function(e) { | |
console.log(e.message); | |
}); | |
req.end(); | |
// Get the form | |
function callForm(formUrl) { | |
var req = https.get(formUrl, function(res) { | |
var chunk = ''; | |
res.on('data', function(c) { | |
chunk += c; | |
}); | |
res.on('end', function() { | |
jsdom.env(chunk, function(err, window) { | |
var fkey = window.document.getElementById('fkey').value; | |
login(res.headers['set-cookie'], fkey); | |
}); | |
}); | |
}); | |
req.end(); | |
} | |
// Submit the form | |
function login(cookies, fkey) { | |
var e = encodeURIComponent; | |
var data = 'email=' + e('florian+2@margaine.com') + '&password=' + | |
e('somepass') + '&affId=4&fkey=' + fkey; | |
var opts = { | |
method: 'POST', | |
host: 'openid.stackexchange.com', | |
path: '/affiliate/form/login/submit', | |
headers: { | |
'Content-Length': data.length, | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Cookie': 'gauthed=1;' | |
} | |
}; | |
cookies.forEach(function(cookie) { | |
opts.headers.Cookie += cookie + ';'; | |
}); | |
var req = https.request(opts, function(res) { | |
var chunk = ''; | |
res.on('data', function(c) { | |
chunk += c; | |
}); | |
res.on('end', function() { | |
jsdom.env(chunk, function(err, w) { | |
var anchor = w.document.querySelector('noscript a'), | |
link = anchor.href; | |
authenticate(res.headers['set-cookie'], link); | |
}); | |
}); | |
}); | |
req.on('error', function(e) { | |
console.log(e.message); | |
}); | |
req.write(data); | |
req.end(); | |
} | |
// Follow the redirection links till the end and keep the cookies | |
function authenticate(cookies, link) { | |
link = url.parse(link); | |
var opts = { | |
method: 'GET', | |
host: link.host, | |
path: link.path, | |
headers: { Cookie: '' } | |
}; | |
cookies.forEach(function(cookie) { | |
opts.headers.Cookie += cookie + ';'; | |
}); | |
if (!/gauth/.test(opts.headers.Cookie)) { | |
opts.headers.Cookie += 'gauthed=1'; | |
} | |
var scheme = link.protocol === 'http:' ? http : https; | |
var req = scheme.request(opts, function(res) { | |
var loc; | |
if (res.statusCode === 302 || res.statusCode === 301) { | |
loc = res.headers.Location || res.headers.location; | |
authenticate(cookies, loc); | |
return; | |
} | |
var chunk = ''; | |
res.on('data', function(c) { | |
chunk += c; | |
}); | |
res.on('end', function() { | |
console.log(cookies); | |
}); | |
}); | |
req.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment