-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from enpam/main
Fix attributes SAML Request for backwards compatib
- Loading branch information
Showing
2 changed files
with
57 additions
and
2 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
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { SignedXml } from 'xml-crypto'; | ||
|
||
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"]'; | ||
|
||
export const signAuthRequest = ( | ||
xml: string, | ||
options: { | ||
privateKey: string | Buffer; | ||
signatureAlgorithm: string; | ||
nodeName: string; | ||
certificate?: string; | ||
action?: 'prepend' | 'append' | 'after'; | ||
}, | ||
) => { | ||
const { privateKey, signatureAlgorithm, nodeName, certificate, action } = | ||
options; | ||
const sig = new SignedXml(); | ||
sig.signingKey = privateKey; | ||
if (certificate) | ||
sig.keyInfoProvider = { | ||
file: '', | ||
getKey: () => Buffer.from(privateKey), | ||
getKeyInfo: () => | ||
`<X509Data><X509Certificate>${certificate}</X509Certificate></X509Data>`, | ||
}; | ||
sig.signatureAlgorithm = `http://www.w3.org/2001/04/xmldsig-more#rsa-${signatureAlgorithm}`; | ||
sig.addReference( | ||
`//*[local-name(.)='${nodeName}']`, | ||
[ | ||
'http://www.w3.org/2000/09/xmldsig#enveloped-signature', | ||
'http://www.w3.org/2001/10/xml-exc-c14n#', | ||
], | ||
`http://www.w3.org/2001/04/xmlenc#${signatureAlgorithm}`, | ||
); | ||
sig.computeSignature(xml, { | ||
location: { reference: authnRequestXPath + issuerXPath, action: 'after' }, | ||
}); | ||
return sig.getSignedXml(); | ||
}; |