UNPKG

adonisjs-livewire

Version:

A front-end framework for AdonisJS

25 lines (24 loc) 792 B
import CryptoJS from 'crypto-js'; class CorruptComponentPayloadException extends Error { constructor() { super(`Livewire encountered corrupt data when trying to hydrate a component. Ensure that the [name, id, data] of the Livewire component wasn't tampered with between requests.`); this.name = 'CorruptComponentPayloadException'; } } export class Checksum { key; constructor(key) { this.key = key; } verify(snapshot) { let checksum = snapshot['checksum']; delete snapshot['checksum']; if (checksum !== this.generate(snapshot)) { throw new CorruptComponentPayloadException(); } } generate(snapshot) { return CryptoJS.HmacSHA256(JSON.stringify(snapshot), this.key).toString(); } }