forked from Nicklason/node-steam-openid-login
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (66 loc) · 2.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const request = require('@nicklason/request-retry');
const cheerio = require('cheerio');
/**
* Signs in to a website through Steam
* @param {String} url The URL you would go to, to sign in through Steam
* @param {Array<String>|Object} cookies An array of cookies as strings or a cookie jar that contains cookies from a session which is logged in to steamcommunity.com
* @param {Function} callback
*/
module.exports = function (url, cookies, callback) {
// TODO: Custom request options (proxy, headers...)
let jar;
if (Array.isArray(cookies)) {
jar = request.jar();
cookies.forEach(function (cookieStr) {
jar.setCookie(request.cookie(cookieStr), 'https://steamcommunity.com');
});
} else {
jar = cookies;
}
// Go to path for signing in through Steam and follow redirects
request({
method: 'GET',
url: url,
jar: jar,
followAllRedirects: true
}, function (err, response, body) {
if (err) {
return callback(err);
}
if (response.request.uri.host !== 'steamcommunity.com') {
return callback(new Error('Was not redirected to steam, make sure the url is correct'));
}
const $ = cheerio.load(body);
// If we are given a login form, then we are not signed in to steam
if ($('#loginForm').length !== 0) {
return callback(new Error('You are not signed in to Steam'));
}
const form = $('#openidForm');
if (form.length !== 1) {
return callback(new Error('Could not find OpenID login form'));
}
const inputs = form.find('input');
const formData = {};
// Get form data
inputs.each(function (index, element) {
const attribs = element.attribs;
if (attribs.name) {
formData[attribs.name] = attribs.value;
}
});
// Send form to steam and follow redirects back to the website we are signing in to
request({
method: 'POST',
url: 'https://steamcommunity.com/openid/login',
form: formData,
jar: jar,
followAllRedirects: true
}, function (err, response, body) {
if (err) {
return callback(err);
}
// Return cookie jar
callback(null, jar);
});
});
};