-
Notifications
You must be signed in to change notification settings - Fork 3
/
sharepoint.js
143 lines (123 loc) · 3.88 KB
/
sharepoint.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var request = require('./request').request,
async = require('async'),
fs = require('fs'),
parser = require('xml2json'),
path = require('path'),
_ = require('lodash'),
saml = fs.readFileSync(__dirname + '/config/saml.xml').toString();
var extractCookies = function(headers) {
var cookies = {};
_.each(headers['set-cookie'], function(value, key) {
var parsedCookies = value.split(/\=(.+)?/);
parsedCookies[1] = parsedCookies[1].substr(0, parsedCookies[1].indexOf(';'));
cookies[parsedCookies[0]] = parsedCookies[1];
});
return cookies;
};
function getCustomerDomain(host) {
var hostParts = host.split('://');
var hostname = hostParts[1].split('/')[0].split('.')[0];
return hostname;
}
module.exports = function(options, callback) {
var asyncTasks = [];
var customerDomain = getCustomerDomain(options.host).trim();
//Replace username, pwd and URL into SAML.xml
asyncTasks.push(function(callback) {
var samlBody = saml;
samlBody = samlBody.replace('{username}', options.auth.username);
samlBody = samlBody.replace('{password}', options.auth.password);
samlBody = samlBody.replace('{url}', options.host);
callback(null, samlBody);
});
// Get the Security Token
asyncTasks.push(function(samlBody, cb) {
var options = {
method: 'POST',
url: 'https://login.microsoftonline.com/extSTS.srf',
body: samlBody,
strictSSL: false,
followRedirect: true
};
request(options)
.then(function(resp) {
try {
var body = parser.toJson(resp.body, {object: true});
}
catch(err) {
return cb({message: 'Error parsing XML: ' + err});
}
var responseBody = body['S:Envelope']['S:Body'];
var samlError = responseBody['S:Fault'];
if (samlError) {
return cb({statusCode: 401, message: 'Error logging in - SAML fault detected.'});
}
var token = responseBody['wst:RequestSecurityTokenResponse']['wst:RequestedSecurityToken']['wsse:BinarySecurityToken'].$t;
if (!token) {
return cb({message: 'No token found in response body'});
}
cb(null, token);
})
.error(function(err) {
cb(err);
})
.catch(function(exception) {
cb(exception);
});
});
// Get the Cookies
asyncTasks.push(function(token, callback) {
var options = {
url: 'https://' + customerDomain + '.sharepoint.com/_forms/default.aspx?wa=wsignin1.0',
method: 'POST',
body: token,
followAllRedirects: true,
jar: true
};
request(options)
.then(function(resp) {
var cookies = extractCookies(resp.headers);
callback(null, cookies);
})
.error(function(err) {
callback(err);
})
.catch(function(exception) {
callback(exception);
});
});
// Get the request digest
asyncTasks.push(function(cookies, cb) {
var options = {
method: 'POST',
url: 'https://' + customerDomain + '.sharepoint.com/_api/contextinfo',
followAllRedirects: true,
headers: {
'Cookie': 'FedAuth='+ cookies.FedAuth + ';' + 'rtFa=' + cookies.rtFa,
'Content-Type': 'application/json; odata=verbose',
'Accept': 'application/json; odata=verbose'
}
};
request(options)
.then(function(resp) {
var data = JSON.parse(resp.body),
requestDigest = data.d.GetContextWebInformation.FormDigestValue,
requestDigestTimeoutSeconds = data.d.GetContextWebInformation.FormDigestTimeoutSeconds;
callback(null, {
requestDigest: requestDigest,
requestDigestTimeoutSeconds: requestDigestTimeoutSeconds,
cookies: {
FedAuth: cookies.FedAuth,
rtFa: cookies.rtFa
}
});
})
.error(function(err) {
callback(err);
})
.catch(function(exception) {
callback('Exception getting request digest '+ exception);
});
});
return async.waterfall(asyncTasks, callback);
};