2016-03-23 3 views
1

私の問題がどこにあるのかがわかりません。私はリアルタイムメッセージングのためにPubNubを使用しており、サンプルプロジェクトはECC(楕円曲線暗号)暗号化メッセージングを使用しています。ECCキーと公開鍵がJsファイルを通過しているかどうかを確認する方法はありますか?

メッセージを送信する以外は、ファイル内のすべてのものが動作します(アニメーション、チャネルの存在など)。 、

(function() { 

    if (typeof(user) === 'undefined') { 
    return; 
    } 

    var output = document.getElementById('output'), 
     input = document.getElementById('input'), 
     picture = document.getElementById('picture'), 
     presence = document.getElementById('presence'), 
     action = document.getElementById('action'), 
     send = document.getElementById('send'); 

    var channel = 'fun'; 

    var keysCache = {}; 

    var pubnub = PUBNUB.init({ 
     subscribe_key: 'sub-c-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 
     publish_key: 'pub-c-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 
     uuid: user.name, 
     auth_key: user.gtoken, 
     ssl: true 
    }); 


    function displayOutput(message) { 
    if(!message) return; 
    if(typeof(message.text) === 'undefined') return; 

    var html = ''; 

    if ('userid' in message && message.userid in keysCache) { 

     var signature = message.signature; 

     delete message.signature; 

     var result = ecc.verify(keysCache[message.userid].publicKey, signature, JSON.stringify(message)); 

     if(result) { 
     html = '<p><img src="'+ keysCache[message.userid].picture +'" class="avatar"><strong>' + keysCache[message.userid].name + '</strong><br><span>' + message.text + '</span></p>'; 
     } else { 
     html = '<p><img src="images/troll.png" class="avatar"><strong></strong><br><em>A troll tried to spoof '+ keysCache[message.userid].name +' (but failed).</em></p>'; 
     } 

     output.innerHTML = html + output.innerHTML; 

    } else { 
     var xhr = new XMLHttpRequest(); 
     xhr.open('GET', '/user/' + message.userid, true); 
     xhr.onreadystatechange = function() { 
     if (xhr.readyState === 4) { 
      var res = JSON.parse(xhr.responseText); 

      keysCache[message.userid] = { 
      'publicKey': res.publicKey, 
      'name': res.name, 
      'artist': res.artist, 
      'picture': res.picture, 
      'id': res.id 
      } 
      displayOutput(message); 
     } 
     }; 
     xhr.send(null); 
    } 
    } 

    function getHistory() { 
    pubnub.history({ 
     channel: channel, 
     count: 30, 
     callback: function(messages) { 
     messages[0].forEach(function(m){ 
      displayOutput(m); 
     }); 
     } 
    }); 
    } 

    pubnub.subscribe({ 
    channel: channel, 
    restore: true, 
    connect: getHistory, 
    disconnect: function(res){ 
     console.log('disconnect called'); 
    }, 
    reconnect: function(res){ 
     console.log('reconnecting to pubnub'); 
    }, 
    callback: function(m) { 
     displayOutput(m); 
    }, 
    presence: function(m){ 
     if(m.occupancy === 1) { 
     presence.textContent = m.occupancy + ' person online'; 
     } else { 
     presence.textContent = m.occupancy + ' people online'; 
     } 
     if((m.action === 'join') || (m.action === 'timeout') || (m.action === 'leave')){ 
     var status = (m.action === 'join') ? 'joined' : 'left'; 
     action.textContent = m.uuid + ' ' + status +' room'; 
     action.classList.add(m.action); 
     action.classList.add('poof'); 
     action.addEventListener('animationend', function(){action.className='';}, false); 
     } 
    } 
    }); 

    function post() { 
    var safeText = input.value.replace(/\&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;'); 
    var message = { text: safeText, userid: user.id }; 

    var signature = ecc.sign(user.eccKey, JSON.stringify(message)); 
    message['signature'] = signature; 

    pubnub.publish({ 
     channel: channel, 
     message: message 
    }); 

    input.value = ''; 
    } 

    input.addEventListener('keyup', function(e) { 
    if(input.value === '') return; 
    (e.keyCode || e.charCode) === 13 && post(); 
    }, false); 

    send.addEventListener('click', function(e) { 
    if(input.value === '') return; 
    post(); 
    }, false); 


})(); 

このエラーは、私のecc.jsから来ているしかし

Uncaught TypeError: Cannot read property 'r' of undefined 

ファイル:ここで

は私のチャットapp.jsファイルがある:私はヒットするたびに、私はこのメッセージを得る送ります私はecc.jsファイルに触れなかったので、エラーがchat-app.jsファイルにあると仮定しています。私はプロジェクトからそれを得た - ECCのhttps://github.com/pubnub/auth-chat-demo

詳細情報はここで見つけることができます:

var signature = ecc.sign(user.eccKey, JSON.stringify(message)); 

https://github.com/jpillora/eccjs

+0

実際の問題は何ですか? – zerkms

+0

@zerkmsなぜ私はエラーが発生しているのか把握しようとしています(Uncaught TypeError:未定義のプロパティ 'r'を読み取れません) –

+0

エラーがecc.jsから来ている場合、そのエラーが発生したときに呼び出され、コード内でその関数が呼び出されている場所が示されます。デバッグを容易にするためにこの情報を提供してください。 – TheGreatContini

答えて

0

あなたの説明によると、それは、この行のように見えますが、あなたにエラーを与えていますだから私はあなたが正しくuser.eccKeyを渡していないと思いますか?

oAuth(またはログイン認証)のすべてのユーザーデータがありますか?またはユーザーのサインアップ時に正しく生成されたeccKeyですか?

さらに、ecc.jsの非圧縮バージョンを含めて、エラーがより簡単にどこから来ているかを追跡できるようにしてください。 https://raw.githubusercontent.com/jpillora/eccjs/gh-pages/dist/0.3/ecc.js

+0

私はgoogle-oauthを使用しています。登録時にノードに保存されたデータベースのデータをすべて見ることができますが、鍵はPubNubの履歴に保存されていると仮定しました。私はeccKeyが正しく処理されていないことに同意します。なぜなら、コンソールで出力しようとしたところで、パスポートのシリアル化の後に "未定義"です。私は掘り続けます。本当にありがとう! –

+0

ええ、あなたはDBにキーを保存する必要がありますので、node-persistを使っても問題ありません! (PubNubの履歴をDBとして使用することは実際のシナリオではあまり効果的ではありません!レポの元のコードはデモです)。 –

+0

ありがとう問題が解決しました!私はちょうどDB擬似チャネルを取り出し、Mongo dbを代わりに使用しました。 –

関連する問題