Created
March 6, 2022 22:12
-
-
Save gayanhewa/320bc86113bc178131fd87a05f786617 to your computer and use it in GitHub Desktop.
Verify app proxy request if it came through Shopify or not
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.get("/app-proxy", async (req, res) => { | |
// Sorted list of query parameters | |
const q = { | |
path_prefix: req.query['path_prefix'], | |
shop: req.query['shop'], | |
timestamp: req.query['timestamp'], | |
}; | |
const originalQuerystring = Object.keys(q).map(key => key + '=' + q[key]).join(''); | |
const crypto = require('crypto') | |
function computeSignature(querystringFromClient, shopifyAppSecret) { | |
const computedSignature = crypto.createHmac('sha256', shopifyAppSecret) | |
.update(querystringFromClient, 'utf-8').digest('hex') | |
return computedSignature | |
} | |
const computedSignature = computeSignature(originalQuerystring, "API_SECRET_KEY") | |
console.log(originalQuerystring) | |
console.log(computedSignature, req.query); | |
if (computedSignature === req.query['signature']) { | |
res.setHeader('Content-Type', 'application/liquid'); | |
res.send("Hello authenticated {{ customer.id }}"); | |
} else { | |
res.send('Error'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment