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