verify.js 898 B

123456789101112131415161718192021
  1. import { flattenedVerify } from '../flattened/verify.js';
  2. import { JWSInvalid } from '../../util/errors.js';
  3. import { decoder } from '../../lib/buffer_utils.js';
  4. export async function compactVerify(jws, key, options) {
  5. if (jws instanceof Uint8Array) {
  6. jws = decoder.decode(jws);
  7. }
  8. if (typeof jws !== 'string') {
  9. throw new JWSInvalid('Compact JWS must be a string or Uint8Array');
  10. }
  11. const { 0: protectedHeader, 1: payload, 2: signature, length } = jws.split('.');
  12. if (length !== 3) {
  13. throw new JWSInvalid('Invalid Compact JWS');
  14. }
  15. const verified = await flattenedVerify({ payload, protected: protectedHeader, signature }, key, options);
  16. const result = { payload: verified.payload, protectedHeader: verified.protectedHeader };
  17. if (typeof key === 'function') {
  18. return { ...result, key: verified.key };
  19. }
  20. return result;
  21. }