Skip to content

Commit

Permalink
Update types (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbarth authored Jun 23, 2023
1 parent e11a9d8 commit 4bd9577
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 21 deletions.
6 changes: 2 additions & 4 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
{
"env": {
"browser": false,
"node": true,
"mocha": true,
"es6": false
"es2020": true
},
"root": true,
"parserOptions": {
"ecmaVersion": 2020
},
"extends": ["eslint:recommended", "prettier"],
"rules": {
"no-console": "error",
Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ HMAC-SHA1 is also available but it is disabled by default

- HMAC-SHA1 http://www.w3.org/2000/09/xmldsig#hmac-sha1

to enable HMAC-SHA1, do:

```javascript
require("xml-crypto").SignedXml.enableHMAC();
```
to enable HMAC-SHA1, call `enableHMAC()` on your instance of `SignedXml`.

This will enable HMAC and disable digital signature algorithms. Due to key
confusion issues, it is risky to have both HMAC-based and public key digital
Expand Down
28 changes: 24 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ type SignedXmlOptions = {
signatureAlgorithm?: SignatureAlgorithmType;
};

type CanonicalizationOrTransformationAlgorithmProcessOptions = {
defaultNs?: string;
defaultForPrefix?: {};
ancestorNamespaces?: [];
signatureNode: Node;
};

/**
* Options for the computeSignature method.
*/
Expand Down Expand Up @@ -87,6 +94,13 @@ export interface Reference {
isEmptyUri?: boolean;
}

/** Implement this to create a new CanonicalizationAlgorithm */
export class CanonicalizationOrTransformationAlgorithm {
process(node: Node, options: CanonicalizationOrTransformationAlgorithmProcessOptions): string;

getAlgorithmName(): CanonicalizationAlgorithmType;
}

/** Implement this to create a new HashAlgorithm */
export class HashAlgorithm {
getAlgorithmName(): HashAlgorithmType;
Expand All @@ -96,15 +110,18 @@ export class HashAlgorithm {

/** Implement this to create a new SignatureAlgorithm */
export class SignatureAlgorithm {
getAlgorithmName(): SignatureAlgorithmType;

/**
* Sign the given string using the given key
*/
getSignature(
signedInfo: crypto.BinaryLike,
privateKey: crypto.KeyLike,
callback?: (err: Error, signedInfo: string) => never
): string;

/**
* Verify the given signature of the given string using key
*
* @param key a public cert, public key, or private key can be passed here
*/
verifySignature(
Expand All @@ -113,6 +130,8 @@ export class SignatureAlgorithm {
signatureValue: string,
callback?: (err: Error, verified: boolean) => never
): boolean;

getAlgorithmName(): SignatureAlgorithmType;
}

/** Implement this to create a new TransformAlgorithm */
Expand Down Expand Up @@ -174,9 +193,10 @@ export class SignedXml {
// One of the supported signature algorithms. See {@link SignatureAlgorithmType}
signatureAlgorithm: SignatureAlgorithmType;
// A {@link Buffer} or pem encoded {@link String} containing your private key
privateKey: Buffer | string;
privateKey: crypto.KeyLike;
// Contains validation errors (if any) after {@link checkSignature} method is called
validationErrors: string[];
publicCert: crypto.KeyLike;

/**
* The SignedXml constructor provides an abstraction for sign and verify xml documents. The object is constructed using
Expand Down Expand Up @@ -324,7 +344,7 @@ export class SignedXml {
getCertFromKeyInfo(keyInfo: string): string | null;
}

export interface Utils {
export class Utils {
/**
* @param pem The PEM-encoded base64 certificate to strip headers from
*/
Expand Down
10 changes: 8 additions & 2 deletions lib/c14n-canonicalization.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* jshint laxcomma: true */
const utils = require("./utils");

/**
* @type { import("../index.d.ts").CanonicalizationOrTransformationAlgorithm}
*/
class C14nCanonicalization {
constructor() {
this.includeComments = false;
Expand Down Expand Up @@ -276,7 +278,11 @@ class C14nCanonicalization {
}
}

// Add c14n#WithComments here (very simple subclass)
/**
* Add c14n#WithComments here (very simple subclass)
*
* @type { import("../index.d.ts").CanonicalizationOrTransformationAlgorithm}
*/
class C14nCanonicalizationWithComments extends C14nCanonicalization {
constructor() {
super();
Expand Down
3 changes: 3 additions & 0 deletions lib/enveloped-signature.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const xpath = require("xpath");
const utils = require("./utils");

/**
* @type { import("../index.d.ts").CanonicalizationOrTransformationAlgorithm}
*/
class EnvelopedSignature {
process(node, options) {
if (null == options.signatureNode) {
Expand Down
10 changes: 7 additions & 3 deletions lib/exclusive-canonicalization.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* jshint laxcomma: true */
const utils = require("./utils");

function isPrefixInScope(prefixesInScope, prefix, namespaceURI) {
Expand All @@ -12,6 +11,9 @@ function isPrefixInScope(prefixesInScope, prefix, namespaceURI) {
return ret;
}

/**
* @type { import("../index.d.ts").CanonicalizationOrTransformationAlgorithm}
*/
class ExclusiveCanonicalization {
constructor() {
this.includeComments = false;
Expand Down Expand Up @@ -329,8 +331,10 @@ class ExclusiveCanonicalization {
}
}

// Add c14n#WithComments here (very simple subclass)

/**
* Add c14n#WithComments here (very simple subclass)
* @type { import("../index.d.ts").CanonicalizationOrTransformationAlgorithm}
*/
class ExclusiveCanonicalizationWithComments extends ExclusiveCanonicalization {
constructor() {
super();
Expand Down
4 changes: 1 addition & 3 deletions lib/signed-xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ const hashAlgorithms = require("./hash-algorithms");
const signatureAlgorithms = require("./signature-algorithms");

/**
/**
* @typedef { import("../index.d.ts").SignedXml}
* @type {import ("../index.d.ts").SignedXml}
*/
class SignedXml {
constructor(idMode, options = {}) {
Expand Down

0 comments on commit 4bd9577

Please sign in to comment.