CRLDistributionPoints.ts (3672B)
1 import * as asn1js from "asn1js"; 2 import * as pvutils from "pvutils"; 3 import { EMPTY_STRING } from "./constants"; 4 import { DistributionPoint, DistributionPointJson } from "./DistributionPoint"; 5 import { AsnError } from "./errors"; 6 import { PkiObject, PkiObjectParameters } from "./PkiObject"; 7 import * as Schema from "./Schema"; 8 9 const DISTRIBUTION_POINTS = "distributionPoints"; 10 const CLEAR_PROPS = [ 11 DISTRIBUTION_POINTS 12 ]; 13 14 export interface ICRLDistributionPoints { 15 distributionPoints: DistributionPoint[]; 16 } 17 18 export interface CRLDistributionPointsJson { 19 distributionPoints: DistributionPointJson[]; 20 } 21 22 export type CRLDistributionPointsParameters = PkiObjectParameters & Partial<ICRLDistributionPoints>; 23 24 /** 25 * Represents the CRLDistributionPoints structure described in [RFC5280](https://datatracker.ietf.org/doc/html/rfc5280) 26 */ 27 export class CRLDistributionPoints extends PkiObject implements ICRLDistributionPoints { 28 29 public static override CLASS_NAME = "CRLDistributionPoints"; 30 31 public distributionPoints!: DistributionPoint[]; 32 33 /** 34 * Initializes a new instance of the {@link CRLDistributionPoints} class 35 * @param parameters Initialization parameters 36 */ 37 constructor(parameters: CRLDistributionPointsParameters = {}) { 38 super(); 39 40 this.distributionPoints = pvutils.getParametersValue(parameters, DISTRIBUTION_POINTS, CRLDistributionPoints.defaultValues(DISTRIBUTION_POINTS)); 41 42 if (parameters.schema) { 43 this.fromSchema(parameters.schema); 44 } 45 } 46 47 /** 48 * Returns default values for all class members 49 * @param memberName String name for a class member 50 * @returns Default value 51 */ 52 public static override defaultValues(memberName: string): DistributionPoint[]; 53 public static override defaultValues(memberName: string): any { 54 switch (memberName) { 55 case DISTRIBUTION_POINTS: 56 return []; 57 default: 58 return super.defaultValues(memberName); 59 } 60 } 61 62 /** 63 * @inheritdoc 64 * @asn ASN.1 schema 65 * ```asn 66 * CRLDistributionPoints ::= SEQUENCE SIZE (1..MAX) OF DistributionPoint 67 *``` 68 */ 69 public static override schema(parameters: Schema.SchemaParameters<{ 70 distributionPoints?: string; 71 }> = {}): Schema.SchemaType { 72 /** 73 * @type {Object} 74 * @property {string} [blockName] 75 * @property {string} [distributionPoints] 76 */ 77 const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {}); 78 79 return (new asn1js.Sequence({ 80 name: (names.blockName || EMPTY_STRING), 81 value: [ 82 new asn1js.Repeated({ 83 name: (names.distributionPoints || EMPTY_STRING), 84 value: DistributionPoint.schema() 85 }) 86 ] 87 })); 88 } 89 90 public fromSchema(schema: Schema.SchemaType): void { 91 // Clear input data first 92 pvutils.clearProps(schema, CLEAR_PROPS); 93 94 // Check the schema is valid 95 const asn1 = asn1js.compareSchema(schema, 96 schema, 97 CRLDistributionPoints.schema({ 98 names: { 99 distributionPoints: DISTRIBUTION_POINTS 100 } 101 }) 102 ); 103 AsnError.assertSchema(asn1, this.className); 104 105 // Get internal properties from parsed schema 106 this.distributionPoints = Array.from(asn1.result.distributionPoints, element => new DistributionPoint({ schema: element })); 107 } 108 109 public toSchema(): asn1js.Sequence { 110 // Construct and return new ASN.1 schema for this object 111 return (new asn1js.Sequence({ 112 value: Array.from(this.distributionPoints, o => o.toSchema()) 113 })); 114 } 115 116 public toJSON(): CRLDistributionPointsJson { 117 return { 118 distributionPoints: Array.from(this.distributionPoints, o => o.toJSON()) 119 }; 120 } 121 122 }