2017-01-28 1 views
1

facebook webhookのセキュリティを実装しようとしています。Facebookのペイロードをsha1値に変換してx-hub-signationと照合して一致させる

以下のコードは、テキストメッセージには問題ありませんが、添付ファイルが送信される瞬間、sha値は一致しません。

エスケープされたUnicode小文字のペイロードで計算しようとしましたが、シンプルテキストでも異なるsha値を持つことになりました。

ご協力いただきますようお願い申し上げます。

var express = require('express'); 
var app = express() 
// Other imports 
app.listen(app.get('port'),()=> 
{console.log('running on port', app.get('port'))}); 

リクエストボディのようにアクセスされた:

byte[] payloadBytes = request.inputStream.bytes 

String hashReceived = xHubSignature.substring(5) 

String hashComputed = HmacUtils.hmacSha1Hex(facebookAppSecret.getBytes(StandardCharsets.UTF_8), payloadBytes) 

log.info('Received {} computed {}', hashReceived, hashComputed) 
+0

これに関する最新情報はありますか? –

答えて

1

は問題はこのようなものは、私がデータにアクセスされた方法にあったが判明

app.post('/webhook/', (req, res)=> 
    { 
    let body = req.body; 
    //By this time the encoded characters were already decoded and hence the hashcheck was failing. 

    //processing the data 
    }); 

ソリューションでしたnatice httpServerを使用してサーバーを作成し、データにアクセスして、ハッシュチェックが生データで行われるようにします。

これはおそらくExpressを使用して行うこともできますが、それは私のためには機能しませんでした。

これは私がやったことです。

const http = require('http'); 
const url = require("url"); 
const crypto = require('crypto'); 

http.createServer((req, res) => { 
    res.setHeader('Content-Type', 'text/html; charset=utf-8') 

    let body = ''; 
    req.on('data', chunk => { 
     body += chunk 
    }); 

    if (urlItems.pathname === '/facebook/' && req.method === 'POST') { 
     req.on('end',() => { 

      let hmac = crypto.createHmac('sha1', appSecret); 
      hmac.update(body, 'utf-8'); 
      let computedSig = `sha1=${hmac.digest('hex')}`; 

      if (req.headers['x-hub-signature'] === computedSig) { 
       console.log(`${computedSig} matched ${req.headers['x-hub-signature']}`); 
      } else { 
       console.log(`Found ${computedSig} instead of ${req.headers['x-hub-signature']}`); 
      } 
      res.end(JSON.stringify({ status: 'ok' })) 
     }) 
    } 
}).listen(process.env.PORT || 3000); 

EDIT 1:下記に起因変化、我々は、したがって、ノードコードをノードに切り替わります。

関連する問題