Skip to content

Commit f4ee4a4

Browse files
committed
refactor(saml20): generalised and pulled EncryptXml out into ./xml/encrypt
1 parent 5eed625 commit f4ee4a4

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed

lib/saml20.js

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
var fs = require('fs');
22
var path = require('path');
3+
var async = require('async');
34
var Parser = require('xmldom').DOMParser;
4-
var xmlenc = require('xml-encryption');
55
var moment = require('moment');
66
var xmlNameValidator = require('xml-name-validator');
77
var is_uri = require('valid-url').is_uri;
88

9+
var EncryptXml = require('./xml/encrypt');
910
var SignXml = require('./xml/sign');
1011
var utils = require('./utils');
1112

@@ -69,37 +70,6 @@ function extractSaml20Options(opts) {
6970
};
7071
}
7172

72-
var EncryptXml = Object.freeze({
73-
fromEncryptXmlOptions: function (options) {
74-
if (!options.encryptionCert) {
75-
return this.unencrypted;
76-
} else {
77-
return this.encrypted({
78-
rsa_pub: options.encryptionPublicKey,
79-
pem: options.encryptionCert,
80-
encryptionAlgorithm: options.encryptionAlgorithm || 'http://www.w3.org/2001/04/xmlenc#aes256-cbc',
81-
keyEncryptionAlgorighm: options.keyEncryptionAlgorighm || 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p'
82-
})
83-
}
84-
},
85-
unencrypted: function(xml, callback) {
86-
if (callback) {
87-
return setImmediate(callback, null, xml);
88-
} else {
89-
return xml;
90-
}
91-
},
92-
encrypted: function (encryptOptions) {
93-
return function encrypt(xml, callback) {
94-
xmlenc.encrypt(xml, encryptOptions, function(err, encrypted) {
95-
if (err) return callback(err);
96-
encrypted = '<saml:EncryptedAssertion xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">' + encrypted + '</saml:EncryptedAssertion>';
97-
callback(null, utils.removeWhitespace(encrypted));
98-
});
99-
};
100-
}
101-
});
102-
10373
/**
10474
* Creates a signed SAML 2.0 assertion from the given options.
10575
*
@@ -297,5 +267,17 @@ function createAssertion(options, strategies, callback) {
297267
return utils.reportError(err, callback);
298268
}
299269

300-
return strategies.encryptXml(signed, callback);
270+
if (strategies.encryptXml === EncryptXml.unencrypted) {
271+
return strategies.encryptXml(signed, callback);
272+
}
273+
274+
async.waterfall([
275+
function (cb) {
276+
strategies.encryptXml(signed, cb)
277+
},
278+
function (encrypted, cb) {
279+
var assertion = '<saml:EncryptedAssertion xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">' + encrypted + '</saml:EncryptedAssertion>';
280+
cb(null, utils.removeWhitespace(assertion));
281+
},
282+
], callback);
301283
}

lib/xml/encrypt.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var xmlenc = require('xml-encryption');
2+
3+
var utils = require('../utils');
4+
5+
exports.fromEncryptXmlOptions = function (options) {
6+
if (!options.encryptionCert) {
7+
return this.unencrypted;
8+
} else {
9+
return this.encrypted({
10+
rsa_pub: options.encryptionPublicKey,
11+
pem: options.encryptionCert,
12+
encryptionAlgorithm: options.encryptionAlgorithm || 'http://www.w3.org/2001/04/xmlenc#aes256-cbc',
13+
keyEncryptionAlgorighm: options.keyEncryptionAlgorighm || 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p'
14+
});
15+
}
16+
};
17+
18+
exports.unencrypted = function (xml, callback) {
19+
if (callback) {
20+
return setImmediate(callback, null, xml);
21+
} else {
22+
return xml;
23+
}
24+
};
25+
26+
exports.encrypted = function (encryptOptions) {
27+
return function encrypt(xml, callback) {
28+
xmlenc.encrypt(xml, encryptOptions, function (err, encrypted) {
29+
if (err) return callback(err);
30+
callback(null, utils.removeWhitespace(encrypted));
31+
});
32+
};
33+
};

0 commit comments

Comments
 (0)