Skip to content

Commit

Permalink
Allow certificate contents to be defined inline
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveShaffer committed Dec 16, 2016
1 parent 7ab9f20 commit 2094334
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
11 changes: 11 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ ADVANCED USAGE
'PassPhrase': ''
}

- You can also include the certificate contents directly in the options like
this:

var options = {
'CertificateContents': '-----BEGIN CERTIFICATE-----\r\nABCD...'
'CertificateKeyContents': '-----BEGIN RSA PRIVATE KEY-----\r\nABCD...'
}

If you do this, no files will be checked for certificates and the
Certificate and/or CertificateKey options will be ignored.

- When Qlik Sense is redirecting to a custom authentication module it passes
proxyRestUri and targetId as parameters. These are normally handled by the
function automatically, but for scenarios where it might be necessary to
Expand Down
64 changes: 37 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,51 @@ function getFileRealPath(s) {
}
}

function getCertificates(cert, certKey) {
function getCertificates(options) {
var certificate = {};
if (options.CertificateContents) {
certificate.cert = options.CertificateContents;
}
if (options.CertificateKeyContents) {
certificate.key = options.CertificateKeyContents;
}
var _cert = null;
var _certKey = null;

var files = ["client.pem", "client.pfx", "C:\\ProgramData\\Qlik\\Sense\\Repository\\Exported Certificates\\.Local Certificates\\client.pem"];
files.unshift(cert);
files = _.uniq(files);
if (!certificate.cert) {
var files = ["client.pem", "client.pfx", "C:\\ProgramData\\Qlik\\Sense\\Repository\\Exported Certificates\\.Local Certificates\\client.pem"];
files.unshift(options.Certificate);
files = _.uniq(files);

for (var obj in files) {
_cert = getFileRealPath(files[obj]);
if (_cert != null)
break;
for (var obj in files) {
_cert = getFileRealPath(files[obj]);
if (_cert != null)
break;
}
}

if (_cert != null) {
if (certificate.cert || _cert != null) {
try {
if (_cert.toLowerCase().indexOf(".pem") > 0) {
// .pem
var files = ["client_key.pem", "C:\\ProgramData\\Qlik\\Sense\\Repository\\Exported Certificates\\.Local Certificates\\client_key.pem"];
files.unshift(certKey);
files = _.uniq(files);

for (var obj in files) {
_certKey = getFileRealPath(files[obj]);

if (_certKey != null) {
certificate.cert = fs.readFileSync(_cert);
certificate.key = fs.readFileSync(_certKey);
break;
if (!certificate.key) {
if (_cert.toLowerCase().indexOf(".pem") > 0) {
// .pem
var files = ["client_key.pem", "C:\\ProgramData\\Qlik\\Sense\\Repository\\Exported Certificates\\.Local Certificates\\client_key.pem"];
files.unshift(options.CertificateKey);
files = _.uniq(files);

for (var obj in files) {
_certKey = getFileRealPath(files[obj]);

if (_certKey != null) {
certificate.cert = fs.readFileSync(_cert);
certificate.key = fs.readFileSync(_certKey);
break;
}
}
} else {
// .pfx
certificate.pfx = fs.readFileSync(_cert);
}
} else {
// .pfx
certificate.pfx = fs.readFileSync(_cert);
}
} catch (e) {
// nothing to see here...
Expand Down Expand Up @@ -108,7 +118,7 @@ module.exports = {
};

//Locate certificate
var cert = getCertificates(options.Certificate, options.CertificateKey);
var cert = getCertificates(options);
if (cert.cert === undefined || cert.key === undefined) {
if (cert.pfx === undefined) {
res.end('Client certificate or key was not found');
Expand Down Expand Up @@ -216,7 +226,7 @@ module.exports = {
};

//Locate certificate
var cert = getCertificates(options.Certificate, options.CertificateKey);
var cert = getCertificates(options);
if (cert.cert === undefined || cert.key === undefined) {
if (cert.pfx === undefined) {
console.log('Client certificate or key was not found');
Expand Down

0 comments on commit 2094334

Please sign in to comment.