使用REST API身份验证为WebRTC设置TURN服务器

时间:2015-06-10 00:33:20

标签: javascript rest authentication webrtc turn

我正在尝试从here为webRTC设置rfc5766-turn-server TURN服务器。 我能够使用turnuserdb.conf文件通过此TURN服务器成功转发我的视频,其中我有我的用户名和密码(my_user_name:my_password)。 在网络客户端,我用过:

"iceServers":{[
    "url": "turn:my_user_name,@turn_server_ip",
    "credential":"my_password"
}]

我正在尝试使用TURN服务器附带的REST API功能,以避免通过网络发送密码或将其存储在客户端。 我在Rest API下跟踪了this specthis explanation

但不幸的是,我得到了 401 ,我无法进行身份验证。

这就是我所做的:

  1. 我创建了一个秘密“my_secret”,我像这样运行转弯服务器:

    turnserver -v --syslog -a -L xx.xxx.xx.xx -X yy.yyy.yyy.yy -E zz.zzz.zz.zzz --max-bps=3000000 -f -m 3 --min-port=32355 --max-port=65535 --use-auth-secret --static-auth-secret=my_secret --realm=north.gov --cert=turn_server_cert.pem --pkey=turn_server_pkey.pem --log-file=stdout -q 100 -Q 300 --cipher-list=ALL
    

    (我刚用xx.xxx.xx.xx yy.yyy.yyy.yy zz.zzz.zz.zzz替换了IP地址

  2. 后来我生成了一个时间戳,现在是+ 1小时,所以我在nodejs上运行:

    Date.now()+1000*60*60;      // output 1433895918506.
    

    我在this website上生成了临时密码, 使用我的秘密,得到一个结果0ca57806bdc696b3129d4cad83746945b00af77b

  3. 我将密码编码为 base64

  4. 现在,我尝试使用临时用户名:1433895918506:my_user_name和密码:MGNhNTc4MDZiZGM2OTZiMzEyOWQ0Y2FkODM3NDY5NDViMDBhZjc3Yg==在Web客户端上与转弯服务器进行通信,现在我在网络客户端使用

    "iceServers":"url":"turn:1433895918506:my_user_name@turn_server_ip","credential":"MGNhNTc4MDZiZGM2OTZiMzEyOWQ0Y2FkODM3NDY5NDViMDBhZjc3Yg=="}]
    
  5. 但它不起作用,我得到:

    401 user <1433895918506:my_user_name>  incoming packet message processed, error 401: Unauthorised.
    

    你能帮我弄清楚出了什么问题吗?

1 个答案:

答案 0 :(得分:6)

when I generated credential with your name and secret, I got 1Dj9XZ5fwvKS6YoQZOoORcFnXaI= not MGNhNTc4MDZiZGM2OTZiMzEyOWQ0Y2FkODM3NDY5NDViMDBhZjc3Yg==, check your algorithm/code for errors.

and the time is in Unix Timestamp, so in seconds and not milliseconds as you did( though this should not affect, but just makes your credentials never expire)

check if your system and the system where the TURN server is running, the clocks are in sync( at least not days apart), and in general, to avoid issue of clocks not being in sync, better to use ttl as 24 hours, so your timestamp:

timestamp=  parseInt(Date.now()/1000) + 24*3600

the code for generating TURN credential:

var crypto = require('crypto');

function getTURNCredentials(name, secret){    

    var unixTimeStamp = parseInt(Date.now()/1000) + 24*3600,
        username = [unixTimeStamp, name].join(':'),
        password,
        hmac = crypto.createHmac('sha1', secret);
    hmac.setEncoding('base64');
    hmac.write(username);
    hmac.end();
    password = hmac.read();
    return {
        username: username,
        password: password
    };
}
相关问题