Skip to content

Commit

Permalink
use template literals and escape-html to do XML construction
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Armstrong authored and chris-armstrong committed Jan 16, 2020
1 parent bf19c00 commit a61394d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 17 deletions.
10 changes: 6 additions & 4 deletions lib/templates/encrypted-key.tpl.xml.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module.exports = `
var escapehtml = require('escape-html');

module.exports = ({ contentEncryptionMethod, keyInfo, encryptedContent }) => `
<xenc:EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<xenc:EncryptionMethod Algorithm="<%= contentEncryptionMethod %>" />
<%- keyInfo %>
<xenc:EncryptionMethod Algorithm="${escapehtml(contentEncryptionMethod)}" />
${keyInfo}
<xenc:CipherData>
<xenc:CipherValue><%= encryptedContent %></xenc:CipherValue>
<xenc:CipherValue>${escapehtml(encryptedContent)}</xenc:CipherValue>
</xenc:CipherData>
</xenc:EncryptedData>
`;
Expand Down
10 changes: 6 additions & 4 deletions lib/templates/keyinfo.tpl.xml.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
module.exports = `
var escapehtml = require('escape-html');

module.exports = ({ encryptionPublicCert, encryptedKey, keyEncryptionMethod }) => `
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<e:EncryptedKey xmlns:e="http://www.w3.org/2001/04/xmlenc#">
<e:EncryptionMethod Algorithm="<%= keyEncryptionMethod %>">
<e:EncryptionMethod Algorithm="${escapehtml(keyEncryptionMethod)}">
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
</e:EncryptionMethod>
<KeyInfo>
<%- encryptionPublicCert %>
${encryptionPublicCert}
</KeyInfo>
<e:CipherData>
<e:CipherValue><%= encryptedKey %></e:CipherValue>
<e:CipherValue>${escapehtml(encryptedKey)}</e:CipherValue>
</e:CipherData>
</e:EncryptedKey>
</KeyInfo>
Expand Down
5 changes: 2 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var ejs = require('ejs'),
path = require('path'),
var path = require('path'),
fs = require('fs');

var templates = {
Expand All @@ -8,7 +7,7 @@ var templates = {
};

function renderTemplate (file, data) {
return ejs.render(templates[file], data);
return templates[file](data);
}

function pemToCert(pem) {
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"license": "MIT",
"dependencies": {
"ejs": "^2.5.6",
"escape-html": "^1.0.3",
"node-forge": "^0.7.0",
"xmldom": "~0.1.15",
"xpath": "0.0.27"
Expand Down

0 comments on commit a61394d

Please sign in to comment.