0

我如何使用javascript(请没有nodejs)为sinch生成身份验证票我正在使用解析进行用户身份验证,然后我想将该会话传递给sinch

将这些加密库添加到您的 html 文件中

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/core-min.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-utf16-min.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js"></script>

    var userTicket = {
                        'identity': {'type': 'username', 'endpoint': user._serverData.username},
                        'expiresIn': 3600,
                    'applicationKey': sinchClient.applicationKey,
                        'created': new Date().toISOString()
                }



                    var userTicketJson = JSON.stringify(userTicket).trim();
                    var userTicketBase64 = btoa(userTicketJson);


   // TicketSignature = Base64 ( HMAC-SHA256 ( ApplicationSecret, UTF8 ( UserTicketJson ) ) )




                var digest = CryptoJS.HmacSHA256(appSecret,userTicketJson);
               var signature =  CryptoJS.enc.Base64.stringify(digest);



    // UserTicket = TicketData + ":" + TicketSignature
              var  signedUserTicket = userTicketBase64.replace('=','') + ':' + signature.replace('=','');




                    sinchClient.start({'userTicket':signedUserTicket})
                        .then(function(data) {
                            console.log(data)
                        })
                        .fail(function(error) {
                            console.log(error)
                        });
4

1 回答 1

5

首选方法是在后端或您控制的环境中生成身份验证票证,而不是在客户端上 - 出于安全原因!为了在 Javascript 中生成票证,有一个可用于节点的 Javascript 参考实现(可手动移植到浏览器或使用 browserify):https ://www.npmjs.com/package/sinch-ticketgen

身份验证票生成器也包含在 Sinch JS SDK 中,用于开发、测试和服务器端目的。在实例化 SinchClient 时,除了“applicationKey”之外,只需提供“applicationSecret”,您现在就可以使用任何用户身份启动 sinchClient。

例子:

sinchClient = new SinchClient({
    applicationKey: 'SOME_APPLICATION_KEY',
    applicationSecret: 'SOME_APPLICATION_SECRET',
});

sinchClient.start({username: 'SOME_USERNAME'});

但是,请记住,这不应该在生产环境的客户端完成,因为您的应用程序机密将是公开的,攻击者利用它是微不足道的。

相反,请确保您在只有您可以访问应用程序机密的安全环境中生成身份验证票证。

于 2015-03-31T11:56:37.823 回答