1
- import { SignedXml } from "xml-crypto" ;
2
1
import { SamlSigningOptions } from "./types" ;
3
- import * as algorithms from "./algorithms " ;
2
+ import { signXml } from "./xml " ;
4
3
5
4
export function assertRequired < T > ( value : T | null | undefined , error ?: string ) : T {
6
5
if ( value === undefined || value === null || ( typeof value === "string" && value . length === 0 ) ) {
@@ -10,37 +9,14 @@ export function assertRequired<T>(value: T | null | undefined, error?: string):
10
9
}
11
10
}
12
11
13
- export function signXml ( samlMessage : string , xpath : string , options : SamlSigningOptions ) : string {
14
- const defaultTransforms = [
15
- "http://www.w3.org/2000/09/xmldsig#enveloped-signature" ,
16
- "http://www.w3.org/2001/10/xml-exc-c14n#" ,
17
- ] ;
18
-
19
- if ( ! samlMessage ) throw new Error ( "samlMessage is required" ) ;
20
- if ( ! xpath ) throw new Error ( "xpath is required" ) ;
21
- if ( ! options ) {
22
- options = { } as SamlSigningOptions ;
23
- }
24
-
25
- if ( ! options . privateKey ) throw new Error ( "options.privateKey is required" ) ;
26
-
27
- const transforms = options . xmlSignatureTransforms || defaultTransforms ;
28
- const sig = new SignedXml ( ) ;
29
- if ( options . signatureAlgorithm ) {
30
- sig . signatureAlgorithm = algorithms . getSigningAlgorithm ( options . signatureAlgorithm ) ;
31
- }
32
- sig . addReference ( xpath , transforms , algorithms . getDigestAlgorithm ( options . digestAlgorithm ) ) ;
33
- sig . signingKey = options . privateKey ;
34
- sig . computeSignature ( samlMessage , {
35
- location : { reference : xpath , action : "append" } ,
36
- } ) ;
37
-
38
- return sig . getSignedXml ( ) ;
39
- }
40
-
41
12
export function signXmlResponse ( samlMessage : string , options : SamlSigningOptions ) : string {
42
13
const responseXpath =
43
14
'//*[local-name(.)="Response" and namespace-uri(.)="urn:oasis:names:tc:SAML:2.0:protocol"]' ;
44
15
45
- return signXml ( samlMessage , responseXpath , options ) ;
16
+ return signXml (
17
+ samlMessage ,
18
+ responseXpath ,
19
+ { reference : responseXpath , action : "append" } ,
20
+ options
21
+ ) ;
46
22
}
0 commit comments