Hi there,
I’m currently testing out a Shopify embedded app development with Sveltekit. I’m constructing my handler file that will run as hooks and in this file, it will run the app installation request validation via the /api/auth/callback route.
However, I’ve noticed that the hmac that I receive in my query parameter is continually different to what I computed.
Here’s a snippet of my code:
case '/api/auth/callback':
// Obtain values to check from the incoming request
const incomingState = searchParams.get('state')
const hmac = searchParams.get('hmac')
const code = searchParams.get('code')
searchParams.delete('hmac')
searchParams.sort()
// Instantiate values to check the incoming request against
const generatedNonce = generateNonce(shop)
const hostNameMatching = /[a-zA-Z0-9][a-zA-Z0-9\-]*\.myshopify\.com[\/]?$/
const generatedHash = crypto.createHmac('sha256', SHOPIFY_API_SECRET).update(`?${searchParams.toString()}`).digest('hex')
if (generatedNonce === incomingState && shop!.match(hostNameMatching) != null && hmac === generatedHash) {
// I can never get here because the hmac is never the same as generatedHash
const tokenRequestBody = {
client_id: SHOPIFY_API_KEY,
client_secret: SHOPIFY_API_SECRET,
code
}
const response = await fetch(`https://${ shop }/admin/oauth/access_token`, {
method: 'POST',
headers: {
'accept': 'application/json'
},
body: JSON.stringify(tokenRequestBody)
})
const { accessToken } = await response.json()
// the code continues
Is anyone facing the same issue?
Thanks
P.S. I’m aware of the best practice of safe comparing the hmac and the generatedHash. This is just hacked together for now with cleanup to be done once I get it wired up and working.