mirror of
https://github.com/danbulant/jose
synced 2026-05-25 04:51:47 +00:00
21 lines
1.1 KiB
JavaScript
21 lines
1.1 KiB
JavaScript
import decrypt from '../jwe/compact/decrypt.js';
|
|
import jwtPayload from '../lib/jwt_claims_set.js';
|
|
import { JWTClaimValidationFailed } from '../util/errors.js';
|
|
async function jwtDecrypt(jwt, key, options) {
|
|
const decrypted = await decrypt(jwt, key, options);
|
|
const payload = jwtPayload(decrypted.protectedHeader, decrypted.plaintext, options);
|
|
const { protectedHeader } = decrypted;
|
|
if (protectedHeader.iss !== undefined && protectedHeader.iss !== payload.iss) {
|
|
throw new JWTClaimValidationFailed('replicated "iss" claim header parameter mismatch', 'iss', 'mismatch');
|
|
}
|
|
if (protectedHeader.sub !== undefined && protectedHeader.sub !== payload.sub) {
|
|
throw new JWTClaimValidationFailed('replicated "sub" claim header parameter mismatch', 'sub', 'mismatch');
|
|
}
|
|
if (protectedHeader.aud !== undefined &&
|
|
JSON.stringify(protectedHeader.aud) !== JSON.stringify(payload.aud)) {
|
|
throw new JWTClaimValidationFailed('replicated "aud" claim header parameter mismatch', 'aud', 'mismatch');
|
|
}
|
|
return { payload, protectedHeader };
|
|
}
|
|
export { jwtDecrypt };
|
|
export default jwtDecrypt;
|