-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (47 loc) · 1.81 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
const request = require('request');
const cheerio = require('cheerio');
const qs = require('querystring');
function parseBreadcrumb(dom) {
const $ = cheerio.load(dom);
return JSON.parse($('div #props_node')[0]['attribs']['data-props'])['crumbValue'];
}
function jsObjectKeyValueExtract(key, dom) {
// I don't like doing this, but couldn't think of a nicer way to parse
// JS key values from the DOM
let rx = '"' + key + '":"([^"]+?)"';
let rxArray = dom.match(rx);
return rxArray[1];
}
function getSessionCredentials(slackWorkspaceName, slackEmail, slackPassword, callback) {
let j = request.jar()
let r = request.defaults({ jar: j })
let slackWorkspaceUrl = 'https://' + slackWorkspaceName + '.slack.com';
r.get(slackWorkspaceUrl, function (error, response, body) {
let crumb = parseBreadcrumb(body);
let loginForm = {
'signin': 1,
'has_remember': true,
'remember': 'off',
'crumb': crumb,
'email': slackEmail,
'password': slackPassword
}
r.post({
'uri': slackWorkspaceUrl,
'form': loginForm,
'followAllRedirects': true
}, function (error, response, body) {
let teamID = jsObjectKeyValueExtract("team_id", body);
let authQuery = {
'app': 'client', 'lc': null,
'return_to': 'client/' + teamID, 'teams': '', 'iframe': 1
}
r.get('https://app.slack.com/auth?' + qs.stringify(authQuery), function (error, response, body) {
let token = jsObjectKeyValueExtract("token", body);
let cookie = j.getCookieString(slackWorkspaceUrl);
callback(cookie, token);
});
})
});
}
module.exports = getSessionCredentials;