verify.js 779 B

12345678910111213141516
  1. import { compactVerify } from '../jws/compact/verify.js';
  2. import jwtPayload from '../lib/jwt_claims_set.js';
  3. import { JWTInvalid } from '../util/errors.js';
  4. export async function jwtVerify(jwt, key, options) {
  5. var _a;
  6. const verified = await compactVerify(jwt, key, options);
  7. if (((_a = verified.protectedHeader.crit) === null || _a === void 0 ? void 0 : _a.includes('b64')) && verified.protectedHeader.b64 === false) {
  8. throw new JWTInvalid('JWTs MUST NOT use unencoded payload');
  9. }
  10. const payload = jwtPayload(verified.protectedHeader, verified.payload, options);
  11. const result = { payload, protectedHeader: verified.protectedHeader };
  12. if (typeof key === 'function') {
  13. return { ...result, key: verified.key };
  14. }
  15. return result;
  16. }