我正试图为一个使用Coturn的项目设置一个TURN服务器,但我发现文档充其量是粗略的…
我意识到有一个turnadmin
工具可以为您做到这一点,但我更喜欢直接在我的数据库上运行查询。这是一个具有潜在许多用户的应用程序,他们的共享密钥(hmackey
在turnusers_lt
)可能会发生变化(为了不与应用程序共享密码,应用程序使用"假"密码,这是某些不稳定的用户参数的哈希值)。
我可以从少量文档中收集到hmackey
是使用领域,用户名和密码计算的:
$ turnadmin -k -u myusername -r my.realm.org -p my-password
> e.g. 0x7a69b0e2b747a4560045f79d171b78c0
假设我的代码知道这三个参数,我如何构建hmac哈希?例如,PHP中有
string hash_hmac ( string $algo , string $data , string $key [, bool $raw_output = false ] )
$algo
这里应该是SHA1,但什么值会进入$data
(例如concat的用户/通行证)和$key
(例如境界)?
还有一个turn_secret
表,列出了一个领域的"值",我猜这应该用作上面示例中的$key
,但是添加和修改键仍然在我调用turnadmin时给出相同的结果。
本质上,我想做的是(伪代码):
// user registers
// pseudo-code, this is of course computed using php's password_hash function
$hashed_pw = hash($pw);
$db->query('insert into usertable (name, pass) values ($name, $hashed_pw)');
// this is implemented somewhere...
$coturn_pw = get_secret_hash($name);
// this needs implementing...
$HAMC = calc_hmac($name, $coturn_pw, 'my.realm.com');
$turndb->query('insert into turnusers_lt values (...)');
// on update, delete also update turnusers_lt
…然后在客户端,我现在应该能够使用$name
和$coturn_pw
作为my.realm.com
的凭据连接到TURN服务器。
如何构建HMAC密钥在RFC 5389中有描述:
key = MD5(username ":"领域":";SASLprep(密码))
其中MD5在RFC 1321中定义,SASLprep()在RFC 4013中定义
唯一需要更新的表是turnusers_lt
。turn_secret
表和SHA1算法用于生成有时间限制的凭证。
INSERT INTO turnusers_lt (realm, name, hmackey) VALUES (:realm, :username, :key);
当然,使用准备好的语句,而不是手动构建SQL字符串。
OrangeDog的答案是正确的
With node.js:
const crypto= require("crypto");
const username= "foo";
const realm= "here";
const password= "secret";
const hmac = crypto
.createHash("md5")
.update(`${username}:${realm}:${password}`)
.digest("hex")
;