-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
318 lines (247 loc) · 11.5 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
var c14n = require("xml-c14n")(),
connect = require("connect"),
randomId = require("proquint-random-id"),
saml2 = require("saml2"),
url = require("url"),
xmldom = require("xmldom"),
xpath = require("xpath"),
zlib = require("zlib");
var User = require("./lib/user");
var connect_saml2 = module.exports = function connect_saml2(options) {
options = options || {};
var urlencoded = connect.urlencoded(),
canonicaliser = c14n.createCanonicaliser("http://www.w3.org/2001/10/xml-exc-c14n#");
if (!options.idp) {
throw Error("idp parameters are required");
}
if (!options.sp) {
throw Error("sp parameters are required");
}
var idp = new saml2.IdentityProvider(options.idp),
sp = new saml2.ServiceProvider(options.sp);
var mountPrefix = options.mountPrefix || "",
ssoConsumerPostPath = options.ssoConsumerPostPath || "/SAML2/AssertionConsumer/POST";
var ensureAuthentication = !!options.ensureAuthentication,
keepSignatures = !!options.keepSignatures;
var saveAssertionXml = options.saveAssertionXml || function saveAssertionXml(assertionXml, req, cb) {
req.session._saml = req.session._saml || {};
req.session._saml.assertionXml = assertionXml;
return cb();
};
var afterAuthentication = options.afterAuthentication || function afterAuthentication(req, cb) {
req.session._saml = req.session._saml || {};
req.session._saml.fresh = true;
return cb();
};
var fetchAssertionXml = options.fetchAssertionXml || function fetchAssertionXml(req, cb) {
return cb(null, (req.session && req.session._saml && req.session._saml.assertionXml) || null);
};
var removeAssertionXml = options.removeAssertionXml || function removeAssertionXml(req, cb) {
if (req.session && req.session._saml && req.session._saml.assertionXml) {
delete req.session._saml.assertionXml;
}
return cb();
};
var saveRelayState = options.saveRelayState || function saveRelayState(req, id, relayState, cb) {
req.session._saml = req.session._saml || {};
req.session._saml.relayState = req.session._saml.relayState || {};
req.session._saml.relayState[id] = relayState;
return cb();
};
var fetchRelayState = options.fetchRelayState || function fetchRelayState(req, id, cb) {
var relayState = null;
if (req.session && req.session._saml && req.session._saml.relayState && req.session._saml.relayState[id]) {
relayState = req.session._saml.relayState[id];
delete req.session._saml.relayState[id];
}
return cb(null, relayState);
};
return function connect_saml2(req, res, next) {
req.removeAssertion = function removeAssertion(done) {
req.samlAssertion = null;
return removeAssertionXml(req, done);
};
req.initiateAuthentication = function initiateAuthentication() {
var authnRequest = sp.createAuthnRequest();
return canonicaliser.canonicalise(authnRequest.toDocument(), function(err, authnRequestXml) {
if (err) {
return next(err);
}
return zlib.deflateRaw(authnRequestXml, function(err, authnRequestDeflatedXml) {
if (err) {
return next(err);
}
var relayState = {
initiationTime: new Date().toISOString(),
previousUrl: req.url,
};
var relayStateId = Date.now() + "-" + randomId();
var parameters = {
SAMLEncoding: "urn:oasis:names:tc:SAML:2.0:bindings:URL-Encoding:DEFLATE",
SAMLRequest: authnRequestDeflatedXml.toString("base64"),
RelayState: relayStateId,
};
var uri = url.parse(idp.singleSignOnService, true);
for (var k in parameters) {
uri.query[k] = parameters[k];
}
return saveRelayState(req, relayStateId, relayState, function(err) {
if (err) {
return next(err);
}
res.writeHead(302, {
location: url.format(uri),
});
return res.end();
});
});
});
};
fetchAssertionXml(req, function(err, storedAssertionXml) {
if (err) {
return next(err);
}
if (storedAssertionXml) {
try {
var storedAssertionDocument = (new xmldom.DOMParser()).parseFromString(storedAssertionXml);
var storedAssertion = saml2.Protocol.fromDocument(storedAssertionDocument);
} catch (e) {
return next(e);
}
var authnStatementElement = xpath.select1("./*[namespace-uri()='urn:oasis:names:tc:SAML:2.0:assertion' and local-name()='Assertion']/*[namespace-uri()='urn:oasis:names:tc:SAML:2.0:assertion' and local-name()='AuthnStatement']", storedAssertionDocument);
var sessionNotOnOrAfter = null;
if (authnStatementElement) {
sessionNotOnOrAfter = authnStatementElement.getAttribute("SessionNotOnOrAfter");
}
req.samlAssertionXml = storedAssertionXml;
req.samlAssertionDocument = storedAssertionDocument;
req.samlAssertion = storedAssertion;
req.user = new User({
expiresAt: sessionNotOnOrAfter ? new Date(sessionNotOnOrAfter) : null,
id: xpath.select1("./*[namespace-uri()='urn:oasis:names:tc:SAML:2.0:assertion' and local-name()='Assertion']/*[namespace-uri()='urn:oasis:names:tc:SAML:2.0:assertion' and local-name()='Subject']/*[namespace-uri()='urn:oasis:names:tc:SAML:2.0:assertion' and local-name()='NameID']/text()", storedAssertionDocument) + "",
attributes: xpath.select("./*[namespace-uri()='urn:oasis:names:tc:SAML:2.0:assertion' and local-name()='Assertion']/*[namespace-uri()='urn:oasis:names:tc:SAML:2.0:assertion' and local-name()='AttributeStatement']/*[namespace-uri()='urn:oasis:names:tc:SAML:2.0:assertion' and local-name()='Attribute']", storedAssertionDocument).map(function(attributeElement) {
var attribute = saml2.Protocol.fromDocument(attributeElement);
return [
attribute.value.name,
attribute.value.attributeValue.map(function(e) {
return e.value;
}),
];
}).reduce(function(i, v) {
i[v[0]] = i[v[0]] || [];
i[v[0]] = i[v[0]].concat(v[1]);
return i;
}, {}),
});
if (sessionNotOnOrAfter && new Date(sessionNotOnOrAfter).valueOf() <= Date.now()) {
delete req.samlAssertionXml;
delete req.samlAssertionDocument;
delete req.samlAssertion;
delete req.user;
}
}
if (req.url === ssoConsumerPostPath && req.method === "POST") {
return urlencoded(req, res, function onParsedBody(err) {
if (err) {
return next(err);
}
if (!req.body.SAMLResponse) {
return next(Error("couldn't find SAML response field"));
}
var samlResponseXml = Buffer(req.body.SAMLResponse, "base64").toString();
var parser = new xmldom.DOMParser();
try {
var samlResponseDocument = parser.parseFromString(samlResponseXml);
} catch (e) {
return next(e);
}
if (samlResponseDocument.documentElement.namespaceURI !== "urn:oasis:names:tc:SAML:2.0:protocol" && samlResponseDocument.documentElement.localName !== "Response") {
return next(Error("expected a {urn:oasis:names:tc:SAML:2.0:protocol}Response but got a {" + samlResponseDocument.documentElement.namespaceURI + "}" + samlResponseDocument.documentElement.localName));
}
return idp.verify(samlResponseDocument, function onVerification(err, signatureInfo) {
if (err) {
return next(err);
}
var statusElement = xpath.select1("./*[namespace-uri()='urn:oasis:names:tc:SAML:2.0:protocol' and local-name()='Response']/*[namespace-uri()='urn:oasis:names:tc:SAML:2.0:protocol' and local-name()='Status']/*[namespace-uri()='urn:oasis:names:tc:SAML:2.0:protocol' and local-name()='StatusCode']", samlResponseDocument);
if (!statusElement) {
return next(Error("couldn't find status element in saml response"));
}
try {
var status = saml2.Protocol.fromDocument(statusElement);
} catch (e) {
return next(e);
}
if (status.value !== "urn:oasis:names:tc:SAML:2.0:status:Success") {
return next("saml response did not indicate a success status");
}
var assertionElement = xpath.select1("./*[namespace-uri()='urn:oasis:names:tc:SAML:2.0:protocol' and local-name()='Response']/*[namespace-uri()='urn:oasis:names:tc:SAML:2.0:assertion' and local-name()='Assertion']", samlResponseDocument);
if (!assertionElement) {
return next(Error("couldn't find assertion in saml response"));
}
var conditions = xpath.select1("./*[namespace-uri()='urn:oasis:names:tc:SAML:2.0:assertion' and local-name()='Conditions']", assertionElement);
if (conditions) {
// we don't use the protocol stuff here because it kills our dates
var notBefore = conditions.getAttribute("NotBefore"),
notOnOrAfter = conditions.getAttribute("NotOnOrAfter");
if (notBefore && new Date(notBefore).valueOf() > Date.now()) {
return next(Error("NotBefore condition not satisfied"));
}
if (notOnOrAfter && new Date(notOnOrAfter).valueOf() <= Date.now()) {
return next(Error("NotOnOrAfter condition not satisfied"));
}
}
if (!keepSignatures) {
// remove signatures from saved assertion unless specified not to
var signatures = xpath.select("//*[namespace-uri()='http://www.w3.org/2000/09/xmldsig#' and local-name()='Signature']", assertionElement);
for (var i=0;i<signatures.length;++i) {
if (signatures[i].parentNode) {
signatures[i].parentNode.removeChild(signatures[i]);
}
}
}
return canonicaliser.canonicalise(assertionElement, function(err, assertionXml) {
if (err) {
return next(err);
}
return saveAssertionXml(assertionXml, req, function(err) {
if (err) {
return next(err);
}
return afterAuthentication(req, function(err) {
if (err) {
return next(err);
}
if (!req.body.RelayState) {
res.writeHead(302, {
location: "/",
});
return res.end();
}
return fetchRelayState(req, req.body.RelayState, function(err, relayState) {
if (err) {
return next(err);
}
if (typeof relayState !== "object" || relayState === null || !relayState.previousUrl) {
res.writeHead(302, {
location: "/",
});
return res.end();
}
res.writeHead(302, {
location: relayState.previousUrl,
});
return res.end();
});
});
});
});
});
});
}
if (ensureAuthentication && !req.user) {
return req.initiateAuthentication();
}
return next();
});
};
};