Skip to content

Commit

Permalink
Format code (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbarth authored Jun 1, 2023
1 parent cdf28fa commit ca56346
Show file tree
Hide file tree
Showing 22 changed files with 3,353 additions and 2,718 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"canonicalized",
"codecov",
"feide",
"HMAC",
"reserialization",
"wsfederation",
"wssecurity"
Expand Down
542 changes: 272 additions & 270 deletions README.md

Large diffs are not rendered by default.

70 changes: 32 additions & 38 deletions example/example.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,43 @@
var select = require('xml-crypto').xpath
, dom = require('@xmldom/xmldom').DOMParser
, SignedXml = require('xml-crypto').SignedXml
, FileKeyInfo = require('xml-crypto').FileKeyInfo
, fs = require('fs')

function signXml(xml, xpath, key, dest)
{
var sig = new SignedXml()
sig.signingKey = fs.readFileSync(key)
sig.addReference(xpath)
sig.computeSignature(xml)
fs.writeFileSync(dest, sig.getSignedXml())
/* eslint-disable no-console */

var select = require("xml-crypto").xpath,
dom = require("@xmldom/xmldom").DOMParser,
SignedXml = require("xml-crypto").SignedXml,
FileKeyInfo = require("xml-crypto").FileKeyInfo,
fs = require("fs");

function signXml(xml, xpath, key, dest) {
var sig = new SignedXml();
sig.signingKey = fs.readFileSync(key);
sig.addReference(xpath);
sig.computeSignature(xml);
fs.writeFileSync(dest, sig.getSignedXml());
}

function validateXml(xml, key)
{
var doc = new dom().parseFromString(xml)
var signature = select("/*/*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']", doc)[0]
var sig = new SignedXml()
sig.keyInfoProvider = new FileKeyInfo(key)
sig.loadSignature(signature.toString())
var res = sig.checkSignature(xml)
if (!res) console.log(sig.validationErrors)
function validateXml(xml, key) {
var doc = new dom().parseFromString(xml);
var signature = select(
"/*/*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']",
doc
)[0];
var sig = new SignedXml();
sig.keyInfoProvider = new FileKeyInfo(key);
sig.loadSignature(signature.toString());
var res = sig.checkSignature(xml);
if (!res) console.log(sig.validationErrors);
return res;
}

var xml = "<library>" +
"<book>" +
"<name>Harry Potter</name>" +
"</book>" +
"</library>"
var xml = "<library>" + "<book>" + "<name>Harry Potter</name>" + "</book>" + "</library>";

//sign an xml document
signXml(xml,
"//*[local-name(.)='book']",
"client.pem",
"result.xml")
signXml(xml, "//*[local-name(.)='book']", "client.pem", "result.xml");

console.log("xml signed succesfully")
console.log("xml signed successfully");

var signedXml = fs.readFileSync("result.xml").toString()
console.log("validating signature...")
var signedXml = fs.readFileSync("result.xml").toString();
console.log("validating signature...");

//validate an xml document
if (validateXml(signedXml, "client_public.pem"))
console.log("signature is valid")
else
console.log("signature not valid")
if (validateXml(signedXml, "client_public.pem")) console.log("signature is valid");
else console.log("signature not valid");
141 changes: 73 additions & 68 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,97 +5,102 @@

/// <reference types="node" />

import { SelectedValue } from 'xpath';
import { SelectedValue } from "xpath";

export class HashAlgorithm {
getAlgorithmName(): string;
getHash(xml: string): string;
getAlgorithmName(): string;
getHash(xml: string): string;
}

export interface Reference {
xpath: string;
transforms?: ReadonlyArray<string> | undefined;
digestAlgorithm?: string | undefined;
uri?: string | undefined;
digestValue?: string | undefined;
inclusiveNamespacesPrefixList?: string | undefined;
isEmptyUri?: boolean | undefined;
xpath: string;
transforms?: ReadonlyArray<string> | undefined;
digestAlgorithm?: string | undefined;
uri?: string | undefined;
digestValue?: string | undefined;
inclusiveNamespacesPrefixList?: string | undefined;
isEmptyUri?: boolean | undefined;
}

export class SignatureAlgorithm {
getAlgorithmName(): string;
getSignature(signedInfo: Node, signingKey: Buffer): string;
getAlgorithmName(): string;
getSignature(signedInfo: Node, signingKey: Buffer): string;
}

export class TransformationAlgorithm {
getAlgorithmName(): string;
process(node: Node): string;
getAlgorithmName(): string;
process(node: Node): string;
}

export class SignedXml {
static CanonicalizationAlgorithms: {[uri: string]: new () => TransformationAlgorithm };
static HashAlgorithms: {[uri: string]: new () => HashAlgorithm};
static SignatureAlgorithms: {[uri: string]: new () => SignatureAlgorithm};
canonicalizationAlgorithm: string;
inclusiveNamespacesPrefixList: string;
keyInfoProvider: KeyInfo;
references: Reference[];
signatureAlgorithm: string;
signingKey: Buffer | string;
validationErrors: string[];
constructor(idMode?: string | null, options?: {
canonicalizationAlgorithm?: string | undefined
inclusiveNamespacesPrefixList?: string | undefined
idAttribute?: string | undefined
implicitTransforms?: ReadonlyArray<string> | undefined
signatureAlgorithm?: string | undefined
})
addReference(
xpath: string,
transforms?: ReadonlyArray<string>,
digestAlgorithm?: string,
uri?: string,
digestValue?: string,
inclusiveNamespacesPrefixList?: string,
isEmptyUri?: boolean
): void;
checkSignature(xml: string): boolean;
computeSignature(
xml: string,
opts?: {
prefix?: string | undefined,
attrs?: {[key: string]: any} | undefined,
location?: {
reference: string,
action: 'append' | 'prepend' | 'before' | 'after'
} | undefined,
existingPrefixes?: {[prefix: string]: string} | undefined
}
): void;
getOriginalXmlWithIds(): string;
getSignatureXml(): string;
getSignedXml(): string;
loadSignature(signatureNode: string | Node): void;
static CanonicalizationAlgorithms: { [uri: string]: new () => TransformationAlgorithm };
static HashAlgorithms: { [uri: string]: new () => HashAlgorithm };
static SignatureAlgorithms: { [uri: string]: new () => SignatureAlgorithm };
canonicalizationAlgorithm: string;
inclusiveNamespacesPrefixList: string;
keyInfoProvider: KeyInfo;
references: Reference[];
signatureAlgorithm: string;
signingKey: Buffer | string;
validationErrors: string[];
constructor(
idMode?: string | null,
options?: {
canonicalizationAlgorithm?: string | undefined;
inclusiveNamespacesPrefixList?: string | undefined;
idAttribute?: string | undefined;
implicitTransforms?: ReadonlyArray<string> | undefined;
signatureAlgorithm?: string | undefined;
}
);
addReference(
xpath: string,
transforms?: ReadonlyArray<string>,
digestAlgorithm?: string,
uri?: string,
digestValue?: string,
inclusiveNamespacesPrefixList?: string,
isEmptyUri?: boolean
): void;
checkSignature(xml: string): boolean;
computeSignature(
xml: string,
opts?: {
prefix?: string | undefined;
attrs?: { [key: string]: any } | undefined;
location?:
| {
reference: string;
action: "append" | "prepend" | "before" | "after";
}
| undefined;
existingPrefixes?: { [prefix: string]: string } | undefined;
}
): void;
getOriginalXmlWithIds(): string;
getSignatureXml(): string;
getSignedXml(): string;
loadSignature(signatureNode: string | Node): void;
}

export interface KeyInfo {
getKey(keyInfo?: Node[] | null): Buffer;
getKeyInfo(key?: string, prefix?: string): string;
attrs?: {[key: string]: any} | undefined;
getKey(keyInfo?: Node[] | null): Buffer;
getKeyInfo(key?: string, prefix?: string): string;
attrs?: { [key: string]: any } | undefined;
}

export class FileKeyInfo implements KeyInfo {
file: string;
constructor(file?: string);
getKey(keyInfo?: Node[] | null): Buffer;
getKeyInfo(key?: string, prefix?: string): string;
file: string;
constructor(file?: string);
getKey(keyInfo?: Node[] | null): Buffer;
getKeyInfo(key?: string, prefix?: string): string;
}

export class StringKeyInfo implements KeyInfo {
key: string;
constructor(key?: string);
getKey(keyInfo?: Node[] | null): Buffer;
getKeyInfo(key?: string, prefix?: string): string;
key: string;
constructor(key?: string);
getKey(keyInfo?: Node[] | null): Buffer;
getKeyInfo(key?: string, prefix?: string): string;
}

export function xpath(node: Node, xpath: string): SelectedValue[];
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var select = require('xpath').select
var select = require("xpath").select;

module.exports = require('./lib/signed-xml')
module.exports.xpath = function(node, xpath) {
return select(xpath, node)
}
module.exports = require("./lib/signed-xml");
module.exports.xpath = function (node, xpath) {
return select(xpath, node);
};
Loading

0 comments on commit ca56346

Please sign in to comment.