decrypt.js 1.2 KB

1234567891011121314151617181920212223
  1. import { compactDecrypt } from '../jwe/compact/decrypt.js';
  2. import jwtPayload from '../lib/jwt_claims_set.js';
  3. import { JWTClaimValidationFailed } from '../util/errors.js';
  4. export async function jwtDecrypt(jwt, key, options) {
  5. const decrypted = await compactDecrypt(jwt, key, options);
  6. const payload = jwtPayload(decrypted.protectedHeader, decrypted.plaintext, options);
  7. const { protectedHeader } = decrypted;
  8. if (protectedHeader.iss !== undefined && protectedHeader.iss !== payload.iss) {
  9. throw new JWTClaimValidationFailed('replicated "iss" claim header parameter mismatch', 'iss', 'mismatch');
  10. }
  11. if (protectedHeader.sub !== undefined && protectedHeader.sub !== payload.sub) {
  12. throw new JWTClaimValidationFailed('replicated "sub" claim header parameter mismatch', 'sub', 'mismatch');
  13. }
  14. if (protectedHeader.aud !== undefined &&
  15. JSON.stringify(protectedHeader.aud) !== JSON.stringify(payload.aud)) {
  16. throw new JWTClaimValidationFailed('replicated "aud" claim header parameter mismatch', 'aud', 'mismatch');
  17. }
  18. const result = { payload, protectedHeader };
  19. if (typeof key === 'function') {
  20. return { ...result, key: decrypted.key };
  21. }
  22. return result;
  23. }