Hi, I’m using a webhook in N8N for receive message of Teams.
The Webhooks is ok, but I need make a verification, how can I do?
I’m trying user ChatGPT but without success.
node code:
// Token secreto gerado pelo Microsoft Teams
const secretToken = ‘<SEU_TOKEN_DO_TEAMS>’; // Substitua pelo token gerado
// Obtenha os cabeçalhos e o corpo da requisição
const headers = $json.headers; // Cabeçalhos enviados na requisição
const requestBody = JSON.stringify($json.body); // Corpo da requisição como string
// Assinatura enviada pelo Teams no cabeçalho Authorization
const receivedHMAC = headers.authorization.split(’ ')[1]; // Remove “HMAC”
// Função para gerar HMAC usando SHA-256
function generateHMAC(secret, body) {
const crypto = require(‘crypto’);
return crypto
.createHmac(‘sha256’, Buffer.from(secret, ‘base64’)) // Chave em Base64
.update(body, ‘utf8’) // Atualiza com o corpo da mensagem
.digest(‘base64’); // Retorna o HMAC em Base64
}
// Gera o HMAC
const calculatedHMAC = generateHMAC(secretToken, requestBody);
// Valida a assinatura
if (calculatedHMAC !== receivedHMAC) {
throw new Error(‘Assinatura HMAC inválida’);
}
// Retorna os dados da requisição se a validação for bem-sucedida
return $json.body;
Is is possible that there is a bug in the Crypto Node as mentioned here?
I tried several possible solutions e.g. Option keep raw body in the Webhook node as input for the crypto module, but the generated output never matches the Teams authorization HMAC header.
The only working solution I found is an implementation with the code node seen in post to generate the HMAC: