Skip to content

Commit

Permalink
Add deprecation notice for privateCert; fix bug (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbarth authored Nov 3, 2020
1 parent be111f3 commit c2f32c6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
16 changes: 14 additions & 2 deletions src/passport-saml/saml-post-signing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,27 @@ const defaultTransforms = [ 'http://www.w3.org/2000/09/xmldsig#enveloped-signatu
export function signSamlPost(samlMessage: string, xpath: string, options: SAMLOptions) {
if (!samlMessage) throw new Error('samlMessage is required');
if (!xpath) throw new Error('xpath is required');
if (!options || (!options.privateCert && !options.privateKey)) throw new Error('options.privateCert or options.privateKey is required');
if (!options) {
options = {} as SAMLOptions;
}

if (options.privateCert) {
console.warn("options.privateCert has been deprecated; use options.privateKey instead.");

if (!options.privateKey) {
options.privateKey = options.privateCert;
}
}

if (!options.privateKey) throw new Error('options.privateKey is required');

const transforms = options.xmlSignatureTransforms || defaultTransforms;
const sig = new SignedXml();
if (options.signatureAlgorithm) {
sig.signatureAlgorithm = algorithms.getSigningAlgorithm(options.signatureAlgorithm);
}
sig.addReference(xpath, transforms, algorithms.getDigestAlgorithm(options.digestAlgorithm));
sig.signingKey = options.privateCert || options.privateKey;
sig.signingKey = options.privateKey;
sig.computeSignature(samlMessage, { location: { reference: xpath + issuerXPath, action: 'after' }});
return sig.getSignedXml();
}
Expand Down
25 changes: 15 additions & 10 deletions src/passport-saml/saml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export interface SAMLOptions {
callbackUrl: string;
signatureAlgorithm: string;
path: string;
privateCert: string;
privateCert?: string;
privateKey: string;
logoutUrl: string;
entryPoint: string;
Expand Down Expand Up @@ -153,6 +153,14 @@ class SAML {
options = {};
}

if (options.privateCert) {
console.warn("options.privateCert has been deprecated; use options.privateKey instead.");

if (!options.privateKey) {
options.privateKey = options.privateCert;
}
}

if (Object.prototype.hasOwnProperty.call(options, 'cert') && !options.cert) {
throw new Error('Invalid property: cert must not be empty');
}
Expand Down Expand Up @@ -267,7 +275,7 @@ class SAML {
samlMessageToSign.SigAlg = samlMessage.SigAlg;
}
signer.update(querystring.stringify(samlMessageToSign));
samlMessage.Signature = signer.sign(this.keyToPEM(this.options.privateCert) || this.options.privateKey, 'base64');
samlMessage.Signature = signer.sign(this.keyToPEM(this.options.privateKey), 'base64');
}

generateAuthorizeRequest(req: Request, isPassive: boolean, isHttpPostBinding: boolean, callback: (err: Error | null, request?: string) => void) {
Expand Down Expand Up @@ -392,8 +400,7 @@ class SAML {
}

let stringRequest = xmlbuilder.create(request as unknown as Record<string, any>).end();
const privateKey = this.options.privateCert || this.options.privateKey;
if (isHttpPostBinding && privateKey) {
if (isHttpPostBinding && this.options.privateKey) {
stringRequest = signAuthnRequestPost(stringRequest, this.options);
}
callback(null, stringRequest);
Expand Down Expand Up @@ -500,8 +507,7 @@ class SAML {
Object.keys(additionalParameters).forEach(k => {
samlMessage[k] = additionalParameters[k];
});
const privateKey = this.options.privateCert || this.options.privateKey;
if (privateKey) {
if (this.options.privateKey) {
try {
if (!this.options.entryPoint) {
throw new Error('"entryPoint" config parameter is required for signed messages');
Expand Down Expand Up @@ -1340,17 +1346,16 @@ class SAML {
"Missing decryptionCert while generating metadata for decrypting service provider");
}
}
const privateKey = this.options.privateCert || this.options.privateKey;
if(privateKey){
if(this.options.privateKey){
if(!signingCert){
throw new Error(
"Missing signingCert while generating metadata for signing service provider messages");
}
}

if(this.options.decryptionPvk || privateKey){
if(this.options.decryptionPvk || this.options.privateKey){
metadata.EntityDescriptor.SPSSODescriptor.KeyDescriptor=[];
if (privateKey) {
if (this.options.privateKey) {

signingCert = signingCert!.replace( /-+BEGIN CERTIFICATE-+\r?\n?/, '' );
signingCert = signingCert.replace( /-+END CERTIFICATE-+\r?\n?/, '' );
Expand Down

0 comments on commit c2f32c6

Please sign in to comment.