Skip to content

Commit

Permalink
Add SigningTime options
Browse files Browse the repository at this point in the history
  • Loading branch information
microshine committed Apr 26, 2019
1 parent 60509bf commit 3c1e05d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -143,6 +143,7 @@ interface OptionsXAdES {
// Signed signature properties

signingCertificate?: string;
signingTime?: OptionsSigningTime;
policy?: OptionsPolicyId;
productionPlace?: OptionsProductionPlace;
signerRole?: OptionsSignerRole;
Expand All @@ -166,6 +167,11 @@ interface OptionsSignReference {

type OptionsSignTransform = "enveloped" | "c14n" | "exc-c14n" | "c14n-com" | "exc-c14n-com" | "base64";

interface OptionsSigningTime {
value?: Date;
format?: string;
}

interface OptionsSignerRole {
claimed?: string[];
certified?: string[];
Expand Down
16 changes: 16 additions & 0 deletions index.d.ts
Expand Up @@ -45,6 +45,18 @@ declare namespace XAdES {
hash: AlgorithmIdentifier;
qualifiers?: Array<OptionsPolicyUserNotice | string>;
}

export interface OptionsSigningTime {
/**
* Signing time value. Default value if now
*/
value?: Date;
/**
* Format of the signing time. Default format is ISO
*/
format?: string;
}

export interface OptionsXAdES extends XmlDSigJs.OptionsSign {
/**
* Sets a certificate of signer for signature. Optional
Expand All @@ -53,6 +65,10 @@ declare namespace XAdES {
* @memberOf OptionsXAdES
*/
signingCertificate?: string;
/**
* Sets signing time options
*/
signingTime?: OptionsSigningTime;
policy?: OptionsPolicyId;
productionPlace?: OptionsProductionPlace;
signerRole?: OptionsSignerRole;
Expand Down
28 changes: 27 additions & 1 deletion src/signed_xml.ts
Expand Up @@ -41,6 +41,17 @@ export interface OptionsPolicyId {
qualifiers?: Array<OptionsPolicyUserNotice | string>;
}

export interface OptionsSigningTime {
/**
* Signing time value. Default value if now
*/
value?: Date;
/**
* Format of the signing time. Default format is ISO
*/
format?: string;
}

export interface OptionsXAdES extends XmlDSigJs.OptionsSign {

/**
Expand All @@ -51,6 +62,11 @@ export interface OptionsXAdES extends XmlDSigJs.OptionsSign {
*/
signingCertificate?: string;

/**
* Sets signing time options
*/
signingTime?: OptionsSigningTime;

policy?: OptionsPolicyId | boolean;
productionPlace?: OptionsProductionPlace;
signerRole?: OptionsSignerRole;
Expand Down Expand Up @@ -134,9 +150,19 @@ export class SignedXml extends XmlDSigJs.SignedXml {
protected async ApplySignOptions(signature: XmlDSigJs.Signature, algorithm: Algorithm, key: CryptoKey, options: OptionsXAdES) {
await super.ApplySignOptions(signature, algorithm, key, options);
if (this.Properties) {
// Add SigningTime
const sigProps = this.Properties.SignedProperties.SignedSignatureProperties;

//#region Add SigningTime
sigProps.SigningTime.Value = new Date();
if (options.signingTime) {
if (options.signingTime.value) {
sigProps.SigningTime.Value = options.signingTime.value;
}
if (options.signingTime.format) {
sigProps.SigningTime.Format = options.signingTime.format;
}
}
//#endregion

// Add reference for SignedProperties
const signingAlg = XmlCore.assign({}, algorithm, key.algorithm);
Expand Down
10 changes: 10 additions & 0 deletions test/xml/date_time.ts
Expand Up @@ -28,6 +28,16 @@ context("xml", () => {
assert.equal(/\<xades\:XadesDateTime xmlns\:xades\=\"http\:\/\/uri\.etsi\.org\/01903\/v1\.3\.2\#"\>\d+\:\d+\:\d+\<\/xades\:XadesDateTime\>/.test(test), true);
});

it("Format yyyy-mm-dd'T'HH:MM:sso", () => {
const dt = new XAdES.xml.XadesDateTime();
dt.Value = DATE;
dt.Format = "isoDateTime";

const xml = dt.GetXml();
const test = new XMLSerializer().serializeToString(xml);
assert.equal(/\<xades\:XadesDateTime xmlns\:xades\=\"http\:\/\/uri\.etsi\.org\/01903\/v1\.3\.2\#"\>\d+\-\d+\-\d+T\d{2}\:\d{2}\:\d{2}[\+\-]\d{4}\<\/xades\:XadesDateTime\>/.test(test), true);
});

});

});
Expand Down

0 comments on commit 3c1e05d

Please sign in to comment.