sign.js 884 B

123456789101112131415161718192021
  1. import { CompactSign } from '../jws/compact/sign.js';
  2. import { JWTInvalid } from '../util/errors.js';
  3. import { encoder } from '../lib/buffer_utils.js';
  4. import { ProduceJWT } from './produce.js';
  5. export class SignJWT extends ProduceJWT {
  6. setProtectedHeader(protectedHeader) {
  7. this._protectedHeader = protectedHeader;
  8. return this;
  9. }
  10. async sign(key, options) {
  11. var _a;
  12. const sig = new CompactSign(encoder.encode(JSON.stringify(this._payload)));
  13. sig.setProtectedHeader(this._protectedHeader);
  14. if (Array.isArray((_a = this._protectedHeader) === null || _a === void 0 ? void 0 : _a.crit) &&
  15. this._protectedHeader.crit.includes('b64') &&
  16. this._protectedHeader.b64 === false) {
  17. throw new JWTInvalid('JWTs MUST NOT use unencoded payload');
  18. }
  19. return sig.sign(key, options);
  20. }
  21. }