Time.ts (4024B)
1 import * as asn1js from "asn1js"; 2 import * as pvutils from "pvutils"; 3 import { EMPTY_STRING } from "./constants"; 4 import { AsnError } from "./errors"; 5 import { PkiObject, PkiObjectParameters } from "./PkiObject"; 6 import * as Schema from "./Schema"; 7 8 const TYPE = "type"; 9 const VALUE = "value"; 10 const UTC_TIME_NAME = "utcTimeName"; 11 const GENERAL_TIME_NAME = "generalTimeName"; 12 const CLEAR_PROPS = [UTC_TIME_NAME, GENERAL_TIME_NAME]; 13 14 export enum TimeType { 15 UTCTime, 16 GeneralizedTime, 17 empty, 18 } 19 20 export interface ITime { 21 /** 22 * 0 - UTCTime; 1 - GeneralizedTime; 2 - empty value 23 */ 24 type: TimeType; 25 /** 26 * Value of the TIME class 27 */ 28 value: Date; 29 } 30 31 export type TimeParameters = PkiObjectParameters & Partial<ITime>; 32 33 export type TimeSchema = Schema.SchemaParameters<{ 34 utcTimeName?: string; 35 generalTimeName?: string; 36 }>; 37 38 export interface TimeJson { 39 type: TimeType; 40 value: Date; 41 } 42 43 /** 44 * Represents the Time structure described in [RFC5280](https://datatracker.ietf.org/doc/html/rfc5280) 45 */ 46 export class Time extends PkiObject implements ITime { 47 48 public static override CLASS_NAME = "Time"; 49 50 public type!: TimeType; 51 public value!: Date; 52 53 /** 54 * Initializes a new instance of the {@link Time} class 55 * @param parameters Initialization parameters 56 */ 57 constructor(parameters: TimeParameters = {}) { 58 super(); 59 60 this.type = pvutils.getParametersValue(parameters, TYPE, Time.defaultValues(TYPE)); 61 this.value = pvutils.getParametersValue(parameters, VALUE, Time.defaultValues(VALUE)); 62 63 if (parameters.schema) { 64 this.fromSchema(parameters.schema); 65 } 66 } 67 68 /** 69 * Returns default values for all class members 70 * @param memberName String name for a class member 71 * @returns Default value 72 */ 73 public static override defaultValues(memberName: typeof TYPE): TimeType; 74 public static override defaultValues(memberName: typeof VALUE): Date; 75 public static override defaultValues(memberName: string): any { 76 switch (memberName) { 77 case TYPE: 78 return 0; 79 case VALUE: 80 return new Date(0, 0, 0); 81 default: 82 return super.defaultValues(memberName); 83 } 84 } 85 86 /** 87 * @inheritdoc 88 * @asn ASN.1 schema 89 * ```asn 90 * Time ::= CHOICE { 91 * utcTime UTCTime, 92 * generalTime GeneralizedTime } 93 * ``` 94 * 95 * @param parameters Input parameters for the schema 96 * @param optional Flag that current schema should be optional 97 * @returns ASN.1 schema object 98 */ 99 static override schema(parameters: TimeSchema = {}, optional = false): Schema.SchemaType { 100 const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {}); 101 102 return (new asn1js.Choice({ 103 optional, 104 value: [ 105 new asn1js.UTCTime({ name: (names.utcTimeName || EMPTY_STRING) }), 106 new asn1js.GeneralizedTime({ name: (names.generalTimeName || EMPTY_STRING) }) 107 ] 108 })); 109 } 110 111 public fromSchema(schema: Schema.SchemaType): void { 112 // Clear input data first 113 pvutils.clearProps(schema, CLEAR_PROPS); 114 115 // Check the schema is valid 116 const asn1 = asn1js.compareSchema(schema, schema, Time.schema({ 117 names: { 118 utcTimeName: UTC_TIME_NAME, 119 generalTimeName: GENERAL_TIME_NAME 120 } 121 })); 122 AsnError.assertSchema(asn1, this.className); 123 124 // Get internal properties from parsed schema 125 if (UTC_TIME_NAME in asn1.result) { 126 this.type = 0; 127 this.value = asn1.result.utcTimeName.toDate(); 128 } 129 if (GENERAL_TIME_NAME in asn1.result) { 130 this.type = 1; 131 this.value = asn1.result.generalTimeName.toDate(); 132 } 133 } 134 135 public toSchema(): asn1js.UTCTime | asn1js.GeneralizedTime { 136 if (this.type === 0) { 137 return new asn1js.UTCTime({ valueDate: this.value }); 138 } else if (this.type === 1) { 139 return new asn1js.GeneralizedTime({ valueDate: this.value }); 140 } 141 142 return {} as any; 143 } 144 145 public toJSON(): TimeJson { 146 return { 147 type: this.type, 148 value: this.value 149 }; 150 } 151 152 }