PolicyMappings.ts (3216B)
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 { PolicyMapping, PolicyMappingJson } from "./PolicyMapping"; 7 import * as Schema from "./Schema"; 8 9 const MAPPINGS = "mappings"; 10 const CLEAR_PROPS = [ 11 MAPPINGS, 12 ]; 13 14 export interface IPolicyMappings { 15 mappings: PolicyMapping[]; 16 } 17 18 export interface PolicyMappingsJson { 19 mappings: PolicyMappingJson[]; 20 } 21 22 export type PolicyMappingsParameters = PkiObjectParameters & Partial<IPolicyMappings>; 23 24 /** 25 * Represents the PolicyMappings structure described in [RFC5280](https://datatracker.ietf.org/doc/html/rfc5280) 26 */ 27 export class PolicyMappings extends PkiObject implements IPolicyMappings { 28 29 public static override CLASS_NAME = "PolicyMappings"; 30 31 public mappings!: PolicyMapping[]; 32 33 /** 34 * Initializes a new instance of the {@link PolicyMappings} class 35 * @param parameters Initialization parameters 36 */ 37 constructor(parameters: PolicyMappingsParameters = {}) { 38 super(); 39 40 this.mappings = pvutils.getParametersValue(parameters, MAPPINGS, PolicyMappings.defaultValues(MAPPINGS)); 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): PolicyMapping[]; 53 public static override defaultValues(memberName: string): any { 54 switch (memberName) { 55 case MAPPINGS: 56 return []; 57 default: 58 return super.defaultValues(memberName); 59 } 60 } 61 62 /** 63 * @inheritdoc 64 * @asn ASN.1 schema 65 * ```asn 66 * PolicyMappings ::= SEQUENCE SIZE (1..MAX) OF PolicyMapping 67 *``` 68 */ 69 public static override schema(parameters: Schema.SchemaParameters<{ 70 mappings?: string; 71 }> = {}): Schema.SchemaType { 72 const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {}); 73 74 return (new asn1js.Sequence({ 75 name: (names.blockName || EMPTY_STRING), 76 value: [ 77 new asn1js.Repeated({ 78 name: (names.mappings || EMPTY_STRING), 79 value: PolicyMapping.schema() 80 }) 81 ] 82 })); 83 } 84 85 public fromSchema(schema: Schema.SchemaType): void { 86 // Clear input data first 87 pvutils.clearProps(schema, CLEAR_PROPS); 88 89 // Check the schema is valid 90 const asn1 = asn1js.compareSchema(schema, 91 schema, 92 PolicyMappings.schema({ 93 names: { 94 mappings: MAPPINGS 95 } 96 }) 97 ); 98 AsnError.assertSchema(asn1, this.className); 99 100 // Get internal properties from parsed schema 101 this.mappings = Array.from(asn1.result.mappings, element => new PolicyMapping({ schema: element })); 102 } 103 104 public toSchema(): asn1js.Sequence { 105 // Construct and return new ASN.1 schema for this object 106 return (new asn1js.Sequence({ 107 value: Array.from(this.mappings, o => o.toSchema()) 108 })); 109 } 110 111 public toJSON(): PolicyMappingsJson { 112 return { 113 mappings: Array.from(this.mappings, o => o.toJSON()) 114 }; 115 } 116 117 }