SubjectDirectoryAttributes.ts (3392B)
1 import * as asn1js from "asn1js"; 2 import * as pvutils from "pvutils"; 3 import { Attribute, AttributeJson } from "./Attribute"; 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 ATTRIBUTES = "attributes"; 10 const CLEAR_PROPS = [ 11 ATTRIBUTES 12 ]; 13 14 export interface ISubjectDirectoryAttributes { 15 attributes: Attribute[]; 16 } 17 18 export interface SubjectDirectoryAttributesJson { 19 attributes: AttributeJson[]; 20 } 21 22 export type SubjectDirectoryAttributesParameters = PkiObjectParameters & Partial<ISubjectDirectoryAttributes>; 23 24 /** 25 * Represents the SubjectDirectoryAttributes structure described in [RFC5280](https://datatracker.ietf.org/doc/html/rfc5280) 26 */ 27 export class SubjectDirectoryAttributes extends PkiObject implements ISubjectDirectoryAttributes { 28 29 public static override CLASS_NAME = "SubjectDirectoryAttributes"; 30 31 public attributes!: Attribute[]; 32 33 /** 34 * Initializes a new instance of the {@link SubjectDirectoryAttributes} class 35 * @param parameters Initialization parameters 36 */ 37 constructor(parameters: SubjectDirectoryAttributesParameters = {}) { 38 super(); 39 40 this.attributes = pvutils.getParametersValue(parameters, ATTRIBUTES, SubjectDirectoryAttributes.defaultValues(ATTRIBUTES)); 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: typeof ATTRIBUTES): Attribute[]; 53 public static override defaultValues(memberName: string): any { 54 switch (memberName) { 55 case ATTRIBUTES: 56 return []; 57 default: 58 return super.defaultValues(memberName); 59 } 60 } 61 62 /** 63 * @inheritdoc 64 * @asn ASN.1 schema 65 * ```asn 66 * SubjectDirectoryAttributes ::= SEQUENCE SIZE (1..MAX) OF Attribute 67 *``` 68 */ 69 public static override schema(parameters: Schema.SchemaParameters<{ 70 attributes?: 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.attributes || EMPTY_STRING), 79 value: Attribute.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 SubjectDirectoryAttributes.schema({ 93 names: { 94 attributes: ATTRIBUTES 95 } 96 }) 97 ); 98 AsnError.assertSchema(asn1, this.className); 99 100 // Get internal properties from parsed schema 101 this.attributes = Array.from(asn1.result.attributes, element => new Attribute({ 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.attributes, o => o.toSchema()) 108 })); 109 } 110 111 public toJSON(): SubjectDirectoryAttributesJson { 112 return { 113 attributes: Array.from(this.attributes, o => o.toJSON()) 114 }; 115 } 116 117 }