verify.js 1.1 KB

12345678910111213141516171819202122232425
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.compactVerify = void 0;
  4. const verify_js_1 = require("../flattened/verify.js");
  5. const errors_js_1 = require("../../util/errors.js");
  6. const buffer_utils_js_1 = require("../../lib/buffer_utils.js");
  7. async function compactVerify(jws, key, options) {
  8. if (jws instanceof Uint8Array) {
  9. jws = buffer_utils_js_1.decoder.decode(jws);
  10. }
  11. if (typeof jws !== 'string') {
  12. throw new errors_js_1.JWSInvalid('Compact JWS must be a string or Uint8Array');
  13. }
  14. const { 0: protectedHeader, 1: payload, 2: signature, length } = jws.split('.');
  15. if (length !== 3) {
  16. throw new errors_js_1.JWSInvalid('Invalid Compact JWS');
  17. }
  18. const verified = await (0, verify_js_1.flattenedVerify)({ payload, protected: protectedHeader, signature }, key, options);
  19. const result = { payload: verified.payload, protectedHeader: verified.protectedHeader };
  20. if (typeof key === 'function') {
  21. return { ...result, key: verified.key };
  22. }
  23. return result;
  24. }
  25. exports.compactVerify = compactVerify;