简体   繁体   中英

register new xmpp account with node-xmpp ( node.js )

I'm looking at ' XEP-0077 in-band registration ' about how to register a new XMPP account. Here is my code. I use node-xmpp to connect my node.js application to an ejabberd server.

var net = require('net');
var xmpp = require('node-xmpp');
var cache = new Object();

net.createServer( function(socket) {
socket.setEncoding('utf8');
socket.addListener('data',function(data) {
   data = data.substr(0,data.length-2);

    if(cache.admin==undefined && data=='login') {
        var ejabberd =new xmpp.Client({jid:"admin@mine",password:'12345',host:'192.168.7.202',port:'5222'});
        cache.admin = ejabberd;

        cache.admin.addListener('online',function() {
            cache.admin.send(new xmpp.Element('presence',{type:'chat'}).c('show').c('status').t('mine status'));
            cache.admin.send(new xmpp.Element('iq',{type:'get',id:'reg1'}).c('query',{xmlns:'jabber:iq:register'}));            
        })

        cache.admin.addListener('stanza',function(stanza) {
            if(stanza.is('iq')) {
                console.log(stanza.children[1]);
            }
        })

        cache.admin.addListener('end',function() {
            cache.admin.end();
            cache.admin = undefined;
        })

    }

    if(cache.admin!=undefined && data=='logout') {
        cache.admin.end();
        cache.admin = undefined;
    } else if(cache.admin!=undefined && data=='register') {
        cache.admin.send(new xmpp.Element('iq',{type:'set',id:'reg1'}).c('query',{xmlns:'jabber:iq:register'}).c('username').t('alow').up().c('password').t('test'));
    }
});

}).listen(5000);

If i run this code, I get this error:

{ name: 'error',
    parent: 
     { name: 'iq',
     parent: null,
     attrs: 
      { from: 'admin@mine',
        to: 'admin@mine/20108892991316770090454637',
        id: 'reg1',
        type: 'error',
        xmlns: 'jabber:client',
        'xmlns:stream': 'http://etherx.jabber.org/streams' },
     children: [ [Object], [Circular] ] },
    attrs: { code: '403', type: 'auth' },
    children: 
    [ { name: '**forbidden**',
       parent: [Circular],
       attrs: [Object],
       children: [] } ] }

In 'XEP-0077: In-Band Registration' it says that the forbidden reason means that "The sender does not have sufficient permissions to cancel the registration".

How can I get such permissions?

I have been strugling with something similar, I wanted to register a new user account via in-band registraton from nodejs to an ejabberd server running in ubuntu. Here is what I did and worked for me:

//Dependencies
var xmpp = require('node-xmpp');

//Host configuration
var host = "localhost";
var port = "5222";
var admin = "sebastian@localhost";
var adminPass = "adminPass";

var connection = new xmpp.Client({
    jid: admin,
    password: adminPass,
    host: host,
    port: port
});

//user to be registered name & pass
var newUserName = "pepe";
var newUserPass = "pepePass";

//Stream
var iq = "<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:component:accept' to='localhost'><iq type='set' id='reg2'><query xmlns='jabber:iq:register'><username>" + newUserName + "</username><password>" + newUserPass + "</password></query></iq></stream>";

//Send
connection.send(iq);

//End
connection.end();

The var iq is kind of messy, I suppose that if you know how to use Strophe.js in a propper way that part could look a little bit nicer and cleaner. I was missing the section of the xml, it seems that if you want to send a stream, you have to provide a valid ejabberd namespace, that was what was failing for me. Hope this helps you sort your problem out.

Which server are you using? Are you sure it has XEP-77 enabled? Test with an existing client. Ensure that the account you're trying to create does not already exist. Ensure that the account has the correct domain name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM