1"use strict"; 2var __importDefault = (this && this.__importDefault) || function (mod) { 3 return (mod && mod.__esModule) ? mod : { "default": mod }; 4}; 5Object.defineProperty(exports, "__esModule", { value: true }); 6exports.Delegations = void 0; 7const util_1 = __importDefault(require("util")); 8const error_1 = require("./error"); 9const key_1 = require("./key"); 10const role_1 = require("./role"); 11const utils_1 = require("./utils"); 12/** 13 * A container object storing information about all delegations. 14 * 15 * Targets roles that are trusted to provide signed metadata files 16 * describing targets with designated pathnames and/or further delegations. 17 */ 18class Delegations { 19 constructor(options) { 20 this.keys = options.keys; 21 this.unrecognizedFields = options.unrecognizedFields || {}; 22 if (options.roles) { 23 if (Object.keys(options.roles).some((roleName) => role_1.TOP_LEVEL_ROLE_NAMES.includes(roleName))) { 24 throw new error_1.ValueError('Delegated role name conflicts with top-level role name'); 25 } 26 } 27 this.succinctRoles = options.succinctRoles; 28 this.roles = options.roles; 29 } 30 equals(other) { 31 if (!(other instanceof Delegations)) { 32 return false; 33 } 34 return (util_1.default.isDeepStrictEqual(this.keys, other.keys) && 35 util_1.default.isDeepStrictEqual(this.roles, other.roles) && 36 util_1.default.isDeepStrictEqual(this.unrecognizedFields, other.unrecognizedFields) && 37 util_1.default.isDeepStrictEqual(this.succinctRoles, other.succinctRoles)); 38 } 39 *rolesForTarget(targetPath) { 40 if (this.roles) { 41 for (const role of Object.values(this.roles)) { 42 if (role.isDelegatedPath(targetPath)) { 43 yield { role: role.name, terminating: role.terminating }; 44 } 45 } 46 } 47 else if (this.succinctRoles) { 48 yield { 49 role: this.succinctRoles.getRoleForTarget(targetPath), 50 terminating: true, 51 }; 52 } 53 } 54 toJSON() { 55 const json = { 56 keys: keysToJSON(this.keys), 57 ...this.unrecognizedFields, 58 }; 59 if (this.roles) { 60 json.roles = rolesToJSON(this.roles); 61 } 62 else if (this.succinctRoles) { 63 json.succinct_roles = this.succinctRoles.toJSON(); 64 } 65 return json; 66 } 67 static fromJSON(data) { 68 const { keys, roles, succinct_roles, ...unrecognizedFields } = data; 69 let succinctRoles; 70 if (utils_1.guard.isObject(succinct_roles)) { 71 succinctRoles = role_1.SuccinctRoles.fromJSON(succinct_roles); 72 } 73 return new Delegations({ 74 keys: keysFromJSON(keys), 75 roles: rolesFromJSON(roles), 76 unrecognizedFields, 77 succinctRoles, 78 }); 79 } 80} 81exports.Delegations = Delegations; 82function keysToJSON(keys) { 83 return Object.entries(keys).reduce((acc, [keyId, key]) => ({ 84 ...acc, 85 [keyId]: key.toJSON(), 86 }), {}); 87} 88function rolesToJSON(roles) { 89 return Object.values(roles).map((role) => role.toJSON()); 90} 91function keysFromJSON(data) { 92 if (!utils_1.guard.isObjectRecord(data)) { 93 throw new TypeError('keys is malformed'); 94 } 95 return Object.entries(data).reduce((acc, [keyID, keyData]) => ({ 96 ...acc, 97 [keyID]: key_1.Key.fromJSON(keyID, keyData), 98 }), {}); 99} 100function rolesFromJSON(data) { 101 let roleMap; 102 if (utils_1.guard.isDefined(data)) { 103 if (!utils_1.guard.isObjectArray(data)) { 104 throw new TypeError('roles is malformed'); 105 } 106 roleMap = data.reduce((acc, role) => { 107 const delegatedRole = role_1.DelegatedRole.fromJSON(role); 108 return { 109 ...acc, 110 [delegatedRole.name]: delegatedRole, 111 }; 112 }, {}); 113 } 114 return roleMap; 115} 116