-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
58 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters