forked from Mindflash/samljs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
152 lines (133 loc) · 5.01 KB
/
util.js
File metadata and controls
152 lines (133 loc) · 5.01 KB
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
var zlib = require('zlib');
var crypto = require('crypto');
var xmldom = require('xmldom');
var xmlCrypto = require('xml-crypto');
var xpath = require('xpath');
var _ = require('lodash');
function generateUniqueId() {
var chars = "abcdef0123456789";
var uniqueID = "";
for (var i = 0; i < 20; i++) {
uniqueID += chars.substr(Math.floor((Math.random()*15)), 1);
}
return "_" + uniqueID;
}
module.exports.generateUniqueId = generateUniqueId;
module.exports.cleanXML = function(xml) {
return xml.replace(/\>\s*\</g, "><");
};
module.exports.deflateAndEncode = function(xml, cb) {
zlib.deflateRaw(xml, function(err, deflatedXML) {
if(err) return cb(err);
deflatedXML = deflatedXML.toString("base64");
cb(null, deflatedXML);
});
};
module.exports.inflateAndDecode = function(base64, cb) {
var decoded = new Buffer(base64, 'base64');
zlib.inflateRaw(decoded, function(err, inflatedXML) {
if(err) return cb(err);
cb(null, inflatedXML.toString());
});
};
module.exports.decode = function(content, sourceEncoding, targetEncoding) {
return Buffer(content, sourceEncoding).toString(targetEncoding);
};
function certToPEM(cert) {
cert = cert.match(/.{1,64}/g).join('\n');
cert = "-----BEGIN CERTIFICATE-----\n" + cert;
cert = cert + "\n-----END CERTIFICATE-----\n";
return cert;
}
function PEMToCert(pem) {
var cert = [];
_.each(pem.split("\n"), function(line) {
if(line && line.indexOf("----") != 0) cert.push(line);
});
return cert.join("\n");
}
module.exports.certToPEM = certToPEM;
module.exports.PEMToCert = PEMToCert;
module.exports.signRequest = function(request, cert) {
var signer = crypto.createSign('RSA-SHA1');
signer.update(request);
return signer.sign(cert, 'base64');
};
module.exports.verifyResponse = function(doc, xml, cert) {
if(!doc) return false;
var signature = xmlCrypto.xpath(doc, "//*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']")[0];
if(!signature) return false;
var sig = new xmlCrypto.SignedXml();
sig.keyInfoProvider = {
getKeyInfo: function (key) {
return "<X509Data></X509Data>";
},
getKey: function (keyInfo) {
return certToPEM(cert);
}
};
sig.loadSignature(signature.toString());
return sig.checkSignature(xml);
};
module.exports.signAuthenticationResponse = function (xml, privateCertPem, cert) {
if(!xml) return false;
var sig = new xmlCrypto.SignedXml();
sig.addReference("//*[local-name(.)='Assertion']", [
"http://www.w3.org/2000/09/xmldsig#enveloped-signature",
"http://www.w3.org/2001/10/xml-exc-c14n#"
]);
sig.signingKey = new Buffer(privateCertPem).toString('ascii');
sig.keyInfoProvider = {
getKeyInfo: function (key) {
return "<X509Data><X509Certificate>" + PEMToCert(cert) + "</X509Certificate></X509Data>";
}
};
sig.computeSignature(xml);
// need to insert the signature in the assertion
var dom = new xmldom.DOMParser();
var signedXmlDom = dom.parseFromString(sig.getOriginalXmlWithIds());
var signatureXmlDom = dom.parseFromString(sig.getSignatureXml());
var subjectElement = signedXmlDom.getElementsByTagName("saml:Subject")[0];
signedXmlDom.documentElement.insertBefore(signatureXmlDom.documentElement, subjectElement);
return '<?xml version="1.0" encoding="UTF-8"?>' + "\n" + signedXmlDom.toString();
};
module.exports.generateAuthRequestParams = function(settings, overrides) {
overrides = overrides || {};
// these are the parameters to the ejs template
return _.defaults(overrides, {
"uniqueId" : generateUniqueId(),
"issueInstant" : new Date().toISOString(),
"version" : "2.0",
"protocolBinding": "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST",
"assertionConsumerServiceUrl" : settings.sp.assertionConsumerServiceUrl,
"spInitiatedRedirectUrl" : settings.idp.spInitiatedRedirectUrl,
"issuer" : settings.sp.issuer,
"nameIdFormat" : settings.sp.nameIdFormat || "urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified",
"nameIdAllowCreate" : settings.sp.nameIdAllowCreate || "true",
"relayState" : null,
"requestedAuthenticationContext" : {
"comparison" : "exact",
"authenticationContextClassRef" : "urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport"
}
});
};
module.exports.generateAuthResponseParams = function(settings, overrides) {
overrides = overrides || {};
var now = new Date();
// these are the parameters to the ejs template
return _.defaults(overrides, {
responseId: generateUniqueId(),
assertionId: generateUniqueId(),
spAssertionId: settings.sp.authRequestId,
timestamp: now.toISOString(),
notOnOrAfter: settings.conditions.notOnOrAfter,
acsUrl: settings.sp.acsUrl,
issuer: settings.idp.issuer,
nameQualifier: settings.idp.nameQualifier,
email: settings.subject.email,
sessIndex: settings.subject.sessIdEncrypted,
attributes: settings.subject.attributes,
conditions: settings.idp.excludeConditions ? null : settings.conditions,
excludeInResponseTo: settings.idp.excludeInResponseTo || false
});
};