Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move XML functions to utility module #571

Merged
merged 8 commits into from Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 3 additions & 26 deletions src/node-saml/saml-post-signing.ts
@@ -1,42 +1,19 @@
import { SignedXml } from "xml-crypto";
import * as algorithms from "./algorithms";
import { SamlSigningOptions } from "./types";
import { signXml } from "./xml";

const authnRequestXPath =
'/*[local-name(.)="AuthnRequest" and namespace-uri(.)="urn:oasis:names:tc:SAML:2.0:protocol"]';
const issuerXPath =
'/*[local-name(.)="Issuer" and namespace-uri(.)="urn:oasis:names:tc:SAML:2.0:assertion"]';
const defaultTransforms = [
"http://www.w3.org/2000/09/xmldsig#enveloped-signature",
"http://www.w3.org/2001/10/xml-exc-c14n#",
];

export function signSamlPost(
samlMessage: string,
xpath: string,
options: SamlSigningOptions
): string {
if (!samlMessage) throw new Error("samlMessage is required");
if (!xpath) throw new Error("xpath is required");
if (!options) {
options = {} as SamlSigningOptions;
}

if (options.privateKey == null) 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.privateKey;
sig.computeSignature(samlMessage, {
location: { reference: xpath + issuerXPath, action: "after" },
});
return sig.getSignedXml();
return signXml(samlMessage, xpath, { reference: xpath + issuerXPath, action: "after" }, options);
}

export function signAuthnRequestPost(authnRequest: string, options: SamlSigningOptions) {
export function signAuthnRequestPost(authnRequest: string, options: SamlSigningOptions): string {
return signSamlPost(authnRequest, authnRequestXPath, options);
}