Skip to content

Commit 255663b

Browse files
committed
refactor: refactor names, fix review issues
1 parent ec7592e commit 255663b

File tree

9 files changed

+317
-290
lines changed

9 files changed

+317
-290
lines changed

src/c14n-canonicalization.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
} from "./types";
88
import * as utils from "./utils";
99
import * as isDomNode from "@xmldom/is-dom-node";
10-
import { Algorithms } from "./constants";
10+
import { XMLDSIG_URIS } from "./xmldsig-uris";
1111

1212
export class C14nCanonicalization implements CanonicalizationAlgorithm {
1313
protected includeComments = false;
@@ -279,7 +279,7 @@ export class C14nCanonicalization implements CanonicalizationAlgorithm {
279279
}
280280

281281
getAlgorithmName(): CanonicalizationAlgorithmName {
282-
return Algorithms.canonicalization.C14N;
282+
return XMLDSIG_URIS.CANONICALIZATION_ALGORITHMS.C14N;
283283
}
284284
}
285285

@@ -293,6 +293,6 @@ export class C14nCanonicalizationWithComments extends C14nCanonicalization {
293293
}
294294

295295
getAlgorithmName(): CanonicalizationAlgorithmName {
296-
return Algorithms.canonicalization.C14N_WITH_COMMENTS;
296+
return XMLDSIG_URIS.CANONICALIZATION_ALGORITHMS.C14N_WITH_COMMENTS;
297297
}
298298
}

src/exclusive-canonicalization.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
} from "./types";
77
import * as utils from "./utils";
88
import * as isDomNode from "@xmldom/is-dom-node";
9-
import { Algorithms } from "./constants";
9+
import { XMLDSIG_URIS } from "./xmldsig-uris";
1010

1111
function isPrefixInScope(prefixesInScope, prefix, namespaceURI) {
1212
let ret = false;
@@ -322,7 +322,7 @@ export class ExclusiveCanonicalization implements CanonicalizationAlgorithm {
322322
}
323323

324324
getAlgorithmName(): CanonicalizationAlgorithmName {
325-
return Algorithms.canonicalization.EXCLUSIVE_C14N;
325+
return XMLDSIG_URIS.CANONICALIZATION_ALGORITHMS.EXCLUSIVE_C14N;
326326
}
327327
}
328328

@@ -333,6 +333,6 @@ export class ExclusiveCanonicalizationWithComments extends ExclusiveCanonicaliza
333333
}
334334

335335
getAlgorithmName(): CanonicalizationAlgorithmName {
336-
return Algorithms.canonicalization.EXCLUSIVE_C14N_WITH_COMMENTS;
336+
return XMLDSIG_URIS.CANONICALIZATION_ALGORITHMS.EXCLUSIVE_C14N_WITH_COMMENTS;
337337
}
338338
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export {
44
ExclusiveCanonicalizationWithComments,
55
} from "./exclusive-canonicalization";
66
export { SignedXml } from "./signed-xml";
7-
export { XmlDSigValidator } from "./xmldsig-validator";
8-
export { Algorithms } from "./constants";
7+
export { XmlDSigVerifier } from "./xmldsig-verifier";
8+
export { XMLDSIG_URIS } from "./xmldsig-uris";
99
export * from "./types";
1010
export * from "./utils";

src/signed-xml.ts

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import type {
1111
Reference,
1212
SignatureAlgorithmName,
1313
SignedXmlOptions,
14-
HashAlgorithmMap,
14+
DigestAlgorithmMap,
1515
SignatureAlgorithmMap,
1616
CanonicalizationAlgorithmMap,
1717
TransformAlgorithmMap,
18+
VerificationIdAttributeType,
1819
} from "./types";
1920

2021
import * as isDomNode from "@xmldom/is-dom-node";
@@ -28,7 +29,14 @@ import * as execC14n from "./exclusive-canonicalization";
2829
import * as hashAlgorithms from "./hash-algorithms";
2930
import * as signatureAlgorithms from "./signature-algorithms";
3031
import * as utils from "./utils";
31-
import { Algorithms, Namespaces } from "./constants";
32+
import { XMLDSIG_URIS } from "./xmldsig-uris";
33+
const {
34+
CANONICALIZATION_ALGORITHMS,
35+
DIGEST_ALGORITHMS,
36+
SIGNATURE_ALGORITHMS,
37+
TRANSFORM_ALGORITHMS,
38+
NAMESPACES,
39+
} = XMLDSIG_URIS;
3240

3341
export class SignedXml {
3442
/**
@@ -37,11 +45,12 @@ export class SignedXml {
3745
*/
3846
idMode?: "wssecurity";
3947
/**
40-
* Specifies the Id attributes which will be usedd to resolve reference URIs.
41-
* When signing, if no Id attribute is found on the element to be signed, the first one from this list will be added.
48+
* Specifies the Id attributes which will be used to resolve reference URIs.
49+
* When signing, if no Id attribute is found on the element to be signed the first one from this list will be added.
50+
* If idAttribute is also specified, it will be added to the start of this list.
4251
*
43-
* @default ["Id", "ID", "id"]
44-
* @example [{ prefix: "wsu", localName: "Id", namespaceUri: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" }]
52+
* @default {@link SignedXml.getDefaultIdAttributes()}
53+
* @example [{localName: "Id", namespaceUri: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" }]
4554
*/
4655
idAttributes: IdAttributeType[];
4756
/**
@@ -107,7 +116,7 @@ export class SignedXml {
107116
/**
108117
* To add a new hash algorithm create a new class that implements the {@link HashAlgorithm} interface, and register it here. More info: {@link https://github.com/node-saml/xml-crypto#customizing-algorithms|Customizing Algorithms}
109118
*/
110-
HashAlgorithms: HashAlgorithmMap;
119+
HashAlgorithms: DigestAlgorithmMap;
111120

112121
/**
113122
* To add a new signature algorithm create a new class that implements the {@link SignatureAlgorithm} interface, and register it here. More info: {@link https://github.com/node-saml/xml-crypto#customizing-algorithms|Customizing Algorithms}
@@ -120,42 +129,42 @@ export class SignedXml {
120129
TransformAlgorithms: TransformAlgorithmMap | undefined;
121130

122131
static defaultNsForPrefix = {
123-
ds: Namespaces.ds,
132+
ds: NAMESPACES.ds,
124133
};
125134

126135
static noop = () => null;
127136

128-
static readonly getDefaultCanonicalizationAlgorithms = () => ({
129-
[Algorithms.canonicalization.C14N]: c14n.C14nCanonicalization,
130-
[Algorithms.canonicalization.C14N_WITH_COMMENTS]: c14n.C14nCanonicalizationWithComments,
131-
[Algorithms.canonicalization.EXCLUSIVE_C14N]: execC14n.ExclusiveCanonicalization,
132-
[Algorithms.canonicalization.EXCLUSIVE_C14N_WITH_COMMENTS]:
137+
static readonly getDefaultCanonicalizationAlgorithms = (): CanonicalizationAlgorithmMap => ({
138+
[CANONICALIZATION_ALGORITHMS.C14N]: c14n.C14nCanonicalization,
139+
[CANONICALIZATION_ALGORITHMS.C14N_WITH_COMMENTS]: c14n.C14nCanonicalizationWithComments,
140+
[CANONICALIZATION_ALGORITHMS.EXCLUSIVE_C14N]: execC14n.ExclusiveCanonicalization,
141+
[CANONICALIZATION_ALGORITHMS.EXCLUSIVE_C14N_WITH_COMMENTS]:
133142
execC14n.ExclusiveCanonicalizationWithComments,
134143
// TODO: separate TransformAlgorithms from CanonicalizationAlgorithms
135-
[Algorithms.transform.ENVELOPED_SIGNATURE]: envelopedSignatures.EnvelopedSignature,
144+
[TRANSFORM_ALGORITHMS.ENVELOPED_SIGNATURE]: envelopedSignatures.EnvelopedSignature,
136145
});
137146

138-
static readonly getDefaultHashAlgorithms = () => ({
147+
static readonly getDefaultDigestAlgorithms = (): DigestAlgorithmMap => ({
139148
// TODO: In v7.x we may consider removing sha1 from defaults
140-
[Algorithms.hash.SHA1]: hashAlgorithms.Sha1,
141-
[Algorithms.hash.SHA256]: hashAlgorithms.Sha256,
142-
[Algorithms.hash.SHA512]: hashAlgorithms.Sha512,
149+
[DIGEST_ALGORITHMS.SHA1]: hashAlgorithms.Sha1,
150+
[DIGEST_ALGORITHMS.SHA256]: hashAlgorithms.Sha256,
151+
[DIGEST_ALGORITHMS.SHA512]: hashAlgorithms.Sha512,
143152
});
144153

145-
static readonly getDefaultSignatureAlgorithms = () => ({
154+
static readonly getDefaultSignatureAlgorithms = (): SignatureAlgorithmMap => ({
146155
// TODO: In v7.x we may consider removing rsa-sha1 from defaults
147-
[Algorithms.signature.RSA_SHA1]: signatureAlgorithms.RsaSha1,
148-
[Algorithms.signature.RSA_SHA256]: signatureAlgorithms.RsaSha256,
149-
[Algorithms.signature.RSA_SHA256_MGF1]: signatureAlgorithms.RsaSha256Mgf1,
150-
[Algorithms.signature.RSA_SHA512]: signatureAlgorithms.RsaSha512,
156+
[SIGNATURE_ALGORITHMS.RSA_SHA1]: signatureAlgorithms.RsaSha1,
157+
[SIGNATURE_ALGORITHMS.RSA_SHA256]: signatureAlgorithms.RsaSha256,
158+
[SIGNATURE_ALGORITHMS.RSA_SHA256_MGF1]: signatureAlgorithms.RsaSha256Mgf1,
159+
[SIGNATURE_ALGORITHMS.RSA_SHA512]: signatureAlgorithms.RsaSha512,
151160
// Disabled by default due to key confusion concerns.
152161
// 'http://www.w3.org/2000/09/xmldsig#hmac-sha1': SignatureAlgorithms.HmacSha1
153162
});
154163

155-
static readonly getDefaultTransformAlgorithms = () =>
164+
static readonly getDefaultTransformAlgorithms = (): TransformAlgorithmMap =>
156165
SignedXml.getDefaultCanonicalizationAlgorithms();
157166

158-
static readonly getDefaultIdAttributes = () => ["Id", "ID", "id"];
167+
static readonly getDefaultIdAttributes = (): VerificationIdAttributeType[] => ["Id", "ID", "id"];
159168

160169
/**
161170
* The SignedXml constructor provides an abstraction for sign and verify xml documents. The object is constructed using
@@ -178,7 +187,7 @@ export class SignedXml {
178187
getCertFromKeyInfo,
179188
objects,
180189
allowedSignatureAlgorithms,
181-
allowedHashAlgorithms,
190+
allowedDigestAlgorithms,
182191
allowedCanonicalizationAlgorithms,
183192
allowedTransformAlgorithms,
184193
} = options;
@@ -206,7 +215,7 @@ export class SignedXml {
206215
this.objects = objects;
207216
this.CanonicalizationAlgorithms =
208217
allowedCanonicalizationAlgorithms ?? SignedXml.getDefaultCanonicalizationAlgorithms();
209-
this.HashAlgorithms = allowedHashAlgorithms ?? SignedXml.getDefaultHashAlgorithms();
218+
this.HashAlgorithms = allowedDigestAlgorithms ?? SignedXml.getDefaultDigestAlgorithms();
210219
this.SignatureAlgorithms =
211220
allowedSignatureAlgorithms ?? SignedXml.getDefaultSignatureAlgorithms();
212221
// TODO: use default transform algorithms if not provided (breaking change)
@@ -220,7 +229,7 @@ export class SignedXml {
220229
*/
221230
enableHMAC(): void {
222231
this.SignatureAlgorithms = {
223-
[Algorithms.signature.HMAC_SHA1]: signatureAlgorithms.HmacSha1,
232+
[SIGNATURE_ALGORITHMS.HMAC_SHA1]: signatureAlgorithms.HmacSha1,
224233
};
225234
this.getKeyInfoContent = SignedXml.noop;
226235
}
@@ -443,8 +452,8 @@ export class SignedXml {
443452
}
444453

445454
if (
446-
this.canonicalizationAlgorithm === Algorithms.canonicalization.C14N ||
447-
this.canonicalizationAlgorithm === Algorithms.canonicalization.C14N_WITH_COMMENTS
455+
this.canonicalizationAlgorithm === CANONICALIZATION_ALGORITHMS.C14N ||
456+
this.canonicalizationAlgorithm === CANONICALIZATION_ALGORITHMS.C14N_WITH_COMMENTS
448457
) {
449458
if (!doc || typeof doc !== "object") {
450459
throw new Error(
@@ -663,7 +672,7 @@ export class SignedXml {
663672

664673
findSignatures(doc: Node): Node[] {
665674
const nodes = xpath.select(
666-
`//*[local-name(.)='Signature' and namespace-uri(.)='${Namespaces.ds}']`,
675+
`//*[local-name(.)='Signature' and namespace-uri(.)='${NAMESPACES.ds}']`,
667676
doc,
668677
);
669678

@@ -727,10 +736,10 @@ export class SignedXml {
727736
let canonicalizationAlgorithmForSignedInfo = this.canonicalizationAlgorithm;
728737
if (
729738
!canonicalizationAlgorithmForSignedInfo ||
730-
canonicalizationAlgorithmForSignedInfo === Algorithms.canonicalization.C14N ||
731-
canonicalizationAlgorithmForSignedInfo === Algorithms.canonicalization.C14N_WITH_COMMENTS
739+
canonicalizationAlgorithmForSignedInfo === CANONICALIZATION_ALGORITHMS.C14N ||
740+
canonicalizationAlgorithmForSignedInfo === CANONICALIZATION_ALGORITHMS.C14N_WITH_COMMENTS
732741
) {
733-
canonicalizationAlgorithmForSignedInfo = Algorithms.canonicalization.EXCLUSIVE_C14N;
742+
canonicalizationAlgorithmForSignedInfo = CANONICALIZATION_ALGORITHMS.EXCLUSIVE_C14N;
734743
}
735744

736745
const temporaryCanonSignedInfo = this.getCanonXml(
@@ -1403,16 +1412,8 @@ export class SignedXml {
14031412
const id = `_${this.id++}`;
14041413

14051414
if (this.idMode === "wssecurity") {
1406-
node.setAttributeNS(
1407-
Namespaces.xmlns,
1408-
"xmlns:wsu",
1409-
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd",
1410-
);
1411-
node.setAttributeNS(
1412-
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd",
1413-
"wsu:Id",
1414-
id,
1415-
);
1415+
node.setAttributeNS(NAMESPACES.xmlns, "xmlns:wsu", NAMESPACES.wsu);
1416+
node.setAttributeNS(NAMESPACES.wsu, "wsu:Id", id);
14161417
} else {
14171418
// Use the first idAttribute to set the new ID
14181419
const firstIdAttr = this.idAttributes[0];
@@ -1421,7 +1422,7 @@ export class SignedXml {
14211422
} else {
14221423
if ("prefix" in firstIdAttr && firstIdAttr.prefix) {
14231424
node.setAttributeNS(
1424-
Namespaces.xmlns,
1425+
NAMESPACES.xmlns,
14251426
`xmlns:${firstIdAttr.prefix}`,
14261427
firstIdAttr.namespaceUri,
14271428
);
@@ -1488,7 +1489,7 @@ export class SignedXml {
14881489
const signatureValueXml = `<${prefix}SignatureValue>${this.signatureValue}</${prefix}SignatureValue>`;
14891490
//the canonicalization requires to get a valid xml node.
14901491
//we need to wrap the info in a dummy signature since it contains the default namespace.
1491-
const dummySignatureWrapper = `<${prefix}Signature ${xmlNsAttr}="${Namespaces.ds}">${signatureValueXml}</${prefix}Signature>`;
1492+
const dummySignatureWrapper = `<${prefix}Signature ${xmlNsAttr}="${NAMESPACES.ds}">${signatureValueXml}</${prefix}Signature>`;
14921493

14931494
const doc = new xmldom.DOMParser().parseFromString(dummySignatureWrapper);
14941495

src/types.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,23 @@
77
/// <reference types="node" />
88

99
import * as crypto from "crypto";
10-
import { Algorithms } from "./constants";
10+
import { XMLDSIG_URIS } from "./xmldsig-uris";
11+
const {
12+
SIGNATURE_ALGORITHMS,
13+
DIGEST_ALGORITHMS,
14+
TRANSFORM_ALGORITHMS,
15+
CANONICALIZATION_ALGORITHMS,
16+
} = XMLDSIG_URIS;
1117

1218
export type ErrorFirstCallback<T> = (err: Error | null, result?: T) => void;
1319

14-
export type IdAttributeType =
20+
export type SignatureIdAttributeType =
21+
| string
22+
| { prefix: string; localName: string; namespaceUri: string };
23+
export type VerificationIdAttributeType =
1524
| string
16-
| { prefix: string; localName: string; namespaceUri: string }
1725
| { localName: string; namespaceUri: string | undefined };
26+
export type IdAttributeType = SignatureIdAttributeType | VerificationIdAttributeType;
1827

1928
/**
2029
* @param cert the certificate as a string or array of strings (@see https://www.w3.org/TR/2008/REC-xmldsig-core-20080610/#sec-X509Data)
@@ -56,7 +65,7 @@ export interface TransformAlgorithmOptions {
5665
}
5766

5867
export type SignatureAlgorithmName =
59-
| (typeof Algorithms.signature)[keyof typeof Algorithms.signature]
68+
| (typeof SIGNATURE_ALGORITHMS)[keyof typeof SIGNATURE_ALGORITHMS]
6069
| string;
6170

6271
/** Extend this to create a new SignatureAlgorithm */
@@ -87,17 +96,17 @@ export interface SignatureAlgorithm {
8796
}
8897
export type SignatureAlgorithmMap = Record<SignatureAlgorithmName, new () => SignatureAlgorithm>;
8998

90-
export type HashAlgorithmName = (typeof Algorithms.hash)[keyof typeof Algorithms.hash] | string;
99+
export type HashAlgorithmName = (typeof DIGEST_ALGORITHMS)[keyof typeof DIGEST_ALGORITHMS] | string;
91100
/** Implement this to create a new HashAlgorithm */
92101
export interface HashAlgorithm {
93102
getAlgorithmName(): HashAlgorithmName;
94103

95104
getHash(xml: string): string;
96105
}
97-
export type HashAlgorithmMap = Record<HashAlgorithmName, new () => HashAlgorithm>;
106+
export type DigestAlgorithmMap = Record<HashAlgorithmName, new () => HashAlgorithm>;
98107

99108
export type TransformAlgorithmName =
100-
| (typeof Algorithms.transform)[keyof typeof Algorithms.transform]
109+
| (typeof TRANSFORM_ALGORITHMS)[keyof typeof TRANSFORM_ALGORITHMS]
101110
| string;
102111
/** Implement this to create a new TransformAlgorithm */
103112
export interface TransformAlgorithm {
@@ -108,7 +117,7 @@ export interface TransformAlgorithm {
108117
export type TransformAlgorithmMap = Record<TransformAlgorithmName, new () => TransformAlgorithm>;
109118

110119
export type CanonicalizationAlgorithmName =
111-
| (typeof Algorithms.canonicalization)[keyof typeof Algorithms.canonicalization]
120+
| (typeof CANONICALIZATION_ALGORITHMS)[keyof typeof CANONICALIZATION_ALGORITHMS]
112121
| string;
113122
/** Implement this to create a new CanonicalizationAlgorithm */
114123
export interface CanonicalizationAlgorithm extends TransformAlgorithm {
@@ -127,8 +136,8 @@ export type CanonicalizationAlgorithmMap = Record<
127136
*/
128137
export interface SignedXmlOptions {
129138
idMode?: "wssecurity";
130-
idAttribute?: IdAttributeType;
131-
idAttributes?: IdAttributeType[];
139+
idAttribute?: SignatureIdAttributeType;
140+
idAttributes?: VerificationIdAttributeType[];
132141
privateKey?: crypto.KeyLike;
133142
publicCert?: crypto.KeyLike;
134143
signatureAlgorithm?: SignatureAlgorithmName;
@@ -141,7 +150,7 @@ export interface SignedXmlOptions {
141150
getCertFromKeyInfo?: KeySelectorFunction;
142151
objects?: Array<{ content: string; attributes?: ObjectAttributes }>;
143152
allowedSignatureAlgorithms?: SignatureAlgorithmMap;
144-
allowedHashAlgorithms?: HashAlgorithmMap;
153+
allowedDigestAlgorithms?: DigestAlgorithmMap;
145154
allowedCanonicalizationAlgorithms?: CanonicalizationAlgorithmMap;
146155
allowedTransformAlgorithms?: TransformAlgorithmMap;
147156
}

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function attrEqualsImplicitly(attr: Attr, localName: string, namespace?: string,
1717
);
1818
}
1919

20-
export function findAttr(element: Element, localName: string, namespace?: string) {
20+
export function findAttr(element: Element, localName: string, namespace?: string | undefined) {
2121
for (let i = 0; i < element.attributes.length; i++) {
2222
const attr = element.attributes[i];
2323

0 commit comments

Comments
 (0)