XMPP带内注册(XEP-0077):与简单的jid / pw登录不同,步骤是什么?

时间:2014-09-11 23:47:37

标签: javascript xmpp registration user-accounts strophe

所以我有一个连接到ejabberd服务器的JavaScript / Strophejs xmpp客户端。到目前为止,我已经在ejabberd网站管理员中创建了用户,例如我创建了用户a@testserver.p1.im和用户b@testserver.p1.im。

我的客户支持登录其中一个现有帐户,并在这些帐户/用户之间发送邮件。

但是,我也希望能够从HTML表单在服务器上创建用户,用户可以在其中输入所需的jid和密码。

在xmpp中,这被称为带内注册(http://xmpp.org/extensions/xep-0077.html)。

现在,这是用户登录现有帐户的基本方案:

$(document).ready(function () {
$('#SignIn').bind('click', function () {
    $(document).trigger('connect', { 
                                    jid: $('#LogineMail').val(), password: $('#LoginPassword_f').val()
                                    }
                        );
                        });
});

因此,上面的代码触发'connect',如下所示:

$(document).bind('connect', function (ev, data) {
console.log('connect fired');
var conn = new Strophe.Connection("http://bosh.metajack.im:5280/xmpp-httpbind");
conn.connect(data.jid, data.password, function (status) {
    console.log('callback being done');
    if (status === Strophe.Status.CONNECTED) {
        alert('connected!');
        $(document).trigger('connected');
        alert('Connected successfully');
    } else if (status === Strophe.Status.DISCONNECTED) {
        $(document).trigger('disconnected');
    }
    else
    {
        Hello.log("error");
        console.log('error');
    }
});

Hello.connection = conn;
});

即,这会创建一个新的Strophe.connection,并使用给定的jid和密码登录。然后我可以发送消息。

然而,带内注册的情况略有不同。 XEP-0077声明:

  

3.1实体注册主机

     

为了确定向主机注册需要哪些字段,实体应该首先将IQ get发送给主机。实体不应该首先发送IQ集来尝试猜测所需的字段,因为所需数据的性质受服务提供的影响。

     

示例1.实体请求来自主机的注册字段

<iq type='get' id='reg1' to='shakespeare.lit'>
  <query xmlns='jabber:iq:register'/>
</iq>

这意味着,在我以用户身份登录服务器之前,我需要能够发送IQ请求并收到响应,因为很明显,如果我是在服务器上请求帐户的用户,我可能在服务器上还没有帐号,因此无法使用jid /密码组合登录。

如何才能解决这个问题?关于入境注册的连接和节发送的常见步骤是什么?例如,我是否应该在服务器上安装某种管理员帐户,然后在想要添加新用户时从代码登录,并使用此管理员帐户获取带内注册所需的要求/字段并注册新用户?或者一个好的/标准的方法是什么?

谢谢和最诚挚的问候,

克里斯

编辑:我刚刚找到了strophe.register.js插件,我现在正在尝试使用它。但是我在strophe.register.js代码中遇到错误,而不是我自己的错误。

这是我的代码注册时的样子:

$('#JoinSend').bind('click', function () {
    var tempConn = new Strophe.Connection("http://bosh.metajack.im:5280/xmpp-httpbind");
    tempConn.register.connect("testserver.host56.com", function (status) {
        if (status === Strophe.Status.REGISTER) {
            // fill out the fields
            connection.register.fields.username = "juliet";
            connection.register.fields.password = "R0m30";
            // calling submit will continue the registration process
            connection.register.submit();
        } else if (status === Strophe.Status.REGISTERED) {
            console.log("registered!");
            // calling login will authenticate the registered JID.
            connection.authenticate();
        } else if (status === Strophe.Status.CONFLICT) {
            console.log("Contact already existed!");
        } else if (status === Strophe.Status.NOTACCEPTABLE) {
            console.log("Registration form not properly filled out.")
        } else if (status === Strophe.Status.REGIFAIL) {
            console.log("The Server does not support In-Band Registration")
        } else if (status === Strophe.Status.CONNECTED) {
            // do something after successful authentication
        } else {
            // Do other stuff
        }
    });
    });

我在chrome JS控制台中遇到以下错误:

未捕获的TypeError:无法读取undefined的属性'bind':strophe.register.js:90。

不确定我是否遗漏了某些内容,或者strophe.register.js是否有其他依赖项?

我在这里使用strophe.register.js:https://github.com/strophe/strophejs-plugins/blob/master/register/strophe.register.js

1 个答案:

答案 0 :(得分:3)

连接&#39;之间存在差异。到服务器并且连接被认证&#39;。为了执行带内注册,您只需要连接,不执行身份验证步骤,然后只需发送节来执行注册。如果服务器支持带内注册,他将允许(虽然他应该阻止与连接未经过身份验证时与其他XMPP实体的任何通信)。