verify.js 898 B

123456789101112131415161718192021222324
  1. import { flattenedVerify } from '../flattened/verify.js';
  2. import { JWSInvalid, JWSSignatureVerificationFailed } from '../../util/errors.js';
  3. import isObject from '../../lib/is_object.js';
  4. export async function generalVerify(jws, key, options) {
  5. if (!isObject(jws)) {
  6. throw new JWSInvalid('General JWS must be an object');
  7. }
  8. if (!Array.isArray(jws.signatures) || !jws.signatures.every(isObject)) {
  9. throw new JWSInvalid('JWS Signatures missing or incorrect type');
  10. }
  11. for (const signature of jws.signatures) {
  12. try {
  13. return await flattenedVerify({
  14. header: signature.header,
  15. payload: jws.payload,
  16. protected: signature.protected,
  17. signature: signature.signature,
  18. }, key, options);
  19. }
  20. catch {
  21. }
  22. }
  23. throw new JWSSignatureVerificationFailed();
  24. }