XMPP握手(DIGEST-MD5 SASL)

时间:2010-09-08 22:47:26

标签: php xmpp sasl digest-authentication

我已经将ejabberd作为我的xmpp服务器,这是我的PHP代码:

$stripped = strip_tags($returnTwo); // remove the xml tags from the response stanza
$decoded = base64_decode($stripped); // decode the jibberish
$regex = "([0-9]{8,})"; // create regex to extract the nonce
preg_match($regex, $decoded, $noncearr); // extracts nonce
$nonce = $noncearr[0]; // finally, we can put the nonce into a variable to continue...

//   1. Create a string of the form "username:realm:password". Call this string X.
$x = "username:server.dyndns.org:password";
//   2. Compute the 16 octet MD5 hash of X. Call the result Y.
$y = md5($x);
//   3. Create a string of the form "Y:nonce:cnonce:authzid". Call this string A1.
$a = "$y:$nonce:$cnonce:username@server.dyndns.org/webchat";
//   4. Create a string of the form "AUTHENTICATE:digest-uri". Call this string A2.
$a2 = "AUTHENTICATE:xmpp/server.dyndns.org";
//   5. Compute the 32 hex digit MD5 hash of A1. Call the result HA1.
$ha1 = md5($a1);
//   6. Compute the 32 hex digit MD5 hash of A2. Call the result HA2.
$ha2 = md5($a2);
//   7. Create a string of the form "HA1:nonce:nc:cnonce:qop:HA2". Call this string KD.
$kd = "$ha1:$nonce:00000001:$cnonce:auth:$ha2";
//   8. Compute the 32 hex digit MD5 hash of KD. Call the result Z.
$z = md5($kd);
$b64z = base64_encode($z);
$respond = "<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>$b64z</response>";

// initialize curl again
$sendThree = curl_init("http://localhost:5280/http-bind");
curl_setopt($sendThree, CURLOPT_POST, 1);
curl_setopt($sendThree, CURLOPT_POSTFIELDS, $respond);
$returnThree = curl_exec($sendThree);
curl_close($sendThree); // close the curl connection

我的问题是服务器返回“1”。多数民众赞成,没有接受,没有错误,只有一个数字1.这之前的步骤都返回了预期,但这部分我遇到了麻烦。我是php的新手(这只是我用它创建的第二页)所以我想知道我是否正确遵循SASL步骤或者是否是ejabberd的问题?

4 个答案:

答案 0 :(得分:0)

curl_exec返回一个布尔值,具体取决于它是否成功。所以你的函数返回true,它被转换为数字1。

要获得实际结果,您必须添加以下行:

curl_setopt($sendThree, CURLOPT_RETURNTRANSFER, true);

答案 1 :(得分:0)

如果您仍然遇到DIGEST-MD5身份验证问题,我建议您查看JAXL XMPP Auth类,它可能会帮助您http://github.com/abhinavsingh/JAXL/blob/master/xmpp/xmpp.auth.php#L86

答案 2 :(得分:0)

好吧,我想出了问题: 我忘了增加我的摆脱...... DOI!

然而满意度......现在我得到一个糟糕的协议错误x(

答案 3 :(得分:0)

检查你的代码,你有字符串$ ha1 = md5($ a1);并且只有$一个变量,没有“1”。