base.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.Signed = exports.isMetadataKind = exports.MetadataKind = void 0;
  7. const util_1 = __importDefault(require("util"));
  8. const error_1 = require("./error");
  9. const utils_1 = require("./utils");
  10. const SPECIFICATION_VERSION = ['1', '0', '31'];
  11. var MetadataKind;
  12. (function (MetadataKind) {
  13. MetadataKind["Root"] = "root";
  14. MetadataKind["Timestamp"] = "timestamp";
  15. MetadataKind["Snapshot"] = "snapshot";
  16. MetadataKind["Targets"] = "targets";
  17. })(MetadataKind = exports.MetadataKind || (exports.MetadataKind = {}));
  18. function isMetadataKind(value) {
  19. return (typeof value === 'string' &&
  20. Object.values(MetadataKind).includes(value));
  21. }
  22. exports.isMetadataKind = isMetadataKind;
  23. /***
  24. * A base class for the signed part of TUF metadata.
  25. *
  26. * Objects with base class Signed are usually included in a ``Metadata`` object
  27. * on the signed attribute. This class provides attributes and methods that
  28. * are common for all TUF metadata types (roles).
  29. */
  30. class Signed {
  31. constructor(options) {
  32. this.specVersion = options.specVersion || SPECIFICATION_VERSION.join('.');
  33. const specList = this.specVersion.split('.');
  34. if (!(specList.length === 2 || specList.length === 3) ||
  35. !specList.every((item) => isNumeric(item))) {
  36. throw new error_1.ValueError('Failed to parse specVersion');
  37. }
  38. // major version must match
  39. if (specList[0] != SPECIFICATION_VERSION[0]) {
  40. throw new error_1.ValueError('Unsupported specVersion');
  41. }
  42. this.expires = options.expires || new Date().toISOString();
  43. this.version = options.version || 1;
  44. this.unrecognizedFields = options.unrecognizedFields || {};
  45. }
  46. equals(other) {
  47. if (!(other instanceof Signed)) {
  48. return false;
  49. }
  50. return (this.specVersion === other.specVersion &&
  51. this.expires === other.expires &&
  52. this.version === other.version &&
  53. util_1.default.isDeepStrictEqual(this.unrecognizedFields, other.unrecognizedFields));
  54. }
  55. isExpired(referenceTime) {
  56. if (!referenceTime) {
  57. referenceTime = new Date();
  58. }
  59. return referenceTime >= new Date(this.expires);
  60. }
  61. static commonFieldsFromJSON(data) {
  62. const { spec_version, expires, version, ...rest } = data;
  63. if (utils_1.guard.isDefined(spec_version) && !(typeof spec_version === 'string')) {
  64. throw new TypeError('spec_version must be a string');
  65. }
  66. if (utils_1.guard.isDefined(expires) && !(typeof expires === 'string')) {
  67. throw new TypeError('expires must be a string');
  68. }
  69. if (utils_1.guard.isDefined(version) && !(typeof version === 'number')) {
  70. throw new TypeError('version must be a number');
  71. }
  72. return {
  73. specVersion: spec_version,
  74. expires,
  75. version,
  76. unrecognizedFields: rest,
  77. };
  78. }
  79. }
  80. exports.Signed = Signed;
  81. function isNumeric(str) {
  82. return !isNaN(Number(str));
  83. }