UNPKG

pkijs

Version:

Public Key Infrastructure (PKI) is the basis of how identity and key management is performed on the web today. PKIjs is a pure JavaScript library implementing the formats that are used in PKI applications. It is built on WebCrypto and aspires to make it p

143 lines (137 loc) 4.34 kB
import * as asn1js from "asn1js"; import { getParametersValue } from "pvutils"; import GeneralName from "./GeneralName.js"; //************************************************************************************** /** * Class from RFC5280 */ export default class AccessDescription { //********************************************************************************** /** * Constructor for AccessDescription class * @param {Object} [parameters={}] * @property {Object} [schema] asn1js parsed value */ constructor(parameters = {}) { //region Internal properties of the object /** * @type {string} * @description accessMethod */ this.accessMethod = getParametersValue(parameters, "accessMethod", AccessDescription.defaultValues("accessMethod")); /** * @type {GeneralName} * @description accessLocation */ this.accessLocation = getParametersValue(parameters, "accessLocation", AccessDescription.defaultValues("accessLocation")); //endregion //region If input argument array contains "schema" for this object if("schema" in parameters) this.fromSchema(parameters.schema); //endregion } //********************************************************************************** /** * Return default values for all class members * @param {string} memberName String name for a class member */ static defaultValues(memberName) { switch(memberName) { case "accessMethod": return ""; case "accessLocation": return new GeneralName(); default: throw new Error(`Invalid member name for AccessDescription class: ${memberName}`); } } //********************************************************************************** /** * Return value of asn1js schema for current class * @param {Object} parameters Input parameters for the schema * @returns {Object} asn1js schema object */ static schema(parameters = {}) { //AccessDescription ::= SEQUENCE { // accessMethod OBJECT IDENTIFIER, // accessLocation GeneralName } /** * @type {Object} * @property {string} [blockName] * @property {string} [accessMethod] * @property {string} [accessLocation] */ const names = getParametersValue(parameters, "names", {}); return (new asn1js.Sequence({ name: (names.blockName || ""), value: [ new asn1js.ObjectIdentifier({ name: (names.accessMethod || "") }), GeneralName.schema(names.accessLocation || {}) ] })); } //********************************************************************************** /** * Convert parsed asn1js object into current class * @param {!Object} schema */ fromSchema(schema) { //region Check the schema is valid const asn1 = asn1js.compareSchema(schema, schema, AccessDescription.schema({ names: { accessMethod: "accessMethod", accessLocation: { names: { blockName: "accessLocation" } } } }) ); if(asn1.verified === false) throw new Error("Object's schema was not verified against input data for AccessDescription"); //endregion //region Get internal properties from parsed schema this.accessMethod = asn1.result.accessMethod.valueBlock.toString(); this.accessLocation = new GeneralName({ schema: asn1.result.accessLocation }); //endregion } //********************************************************************************** /** * Convert current object to asn1js object and set correct values * @returns {Object} asn1js object */ toSchema() { //region Construct and return new ASN.1 schema for this object return (new asn1js.Sequence({ value: [ new asn1js.ObjectIdentifier({ value: this.accessMethod }), this.accessLocation.toSchema() ] })); //endregion } //********************************************************************************** /** * Convertion for the class to JSON object * @returns {Object} */ toJSON() { return { accessMethod: this.accessMethod, accessLocation: this.accessLocation.toJSON() }; } //********************************************************************************** } //**************************************************************************************