Skip to content

Instantly share code, notes, and snippets.

@igrek8
Last active August 17, 2022 12:52
Show Gist options
  • Save igrek8/7f8c77a811e5739f581dec2ec78c9b3c to your computer and use it in GitHub Desktop.
Save igrek8/7f8c77a811e5739f581dec2ec78c9b3c to your computer and use it in GitHub Desktop.
generate shopify multipass token
const crypto = require("crypto");
class Multipass {
constructor(multipass_secret) {
const key_material = crypto.createHash("sha256").update(multipass_secret).digest();
this.encryption_key = key_material.subarray(0, 16);
this.signature_key = key_material.subarray(16);
}
generate_token(customer_data_hash) {
customer_data_hash.created_at = new Date().toISOString();
const ciphertext = this.encrypt(JSON.stringify(customer_data_hash));
return Buffer.concat([ciphertext, this.sign(ciphertext)]).toString("base64url");
}
encrypt(plaintext) {
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv("aes-128-cbc", this.encryption_key, iv);
return Buffer.concat([iv, cipher.update(plaintext), cipher.final()]);
}
sign(data) {
return crypto.createHmac("SHA256", this.signature_key).update(data).digest();
}
}
const user = { email: "[email protected]" };
const multipass = new Multipass("secert");
console.log(multipass.generate_token(user));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment