Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SASL EXTERNAL Client certificate based authorization using node-xmpp
#!/usr/bin/env node
/**
* Echo Bot - the XMPP Hello World
*
* Copied from the echo_bot.js example from node-xmpp.
**/
var xmpp = require('../lib/node-xmpp');
var argv = require('optimist').argv;
var fs = require('fs');
var argv = require('optimist')
.usage('Usage: echo_bot_sasl_external.js --jid=<my-jid> [--host=<host>] [--port=<port>] [--legacy_ssl]')
.demand(['jid'])
.boolean('legacy_ssl')
.argv;
var credentials = {
// These are necessary only if using the client certificate authentication
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem'),
// You might want to put the passphrase of the key file here. You'll be prompted otherwise.
// passphrase: 'YOUR_PASSPHRASE'
};
var options = { jid: argv.jid, 'credentials': credentials };
if (argv.host) options['host'] = argv.host;
if (argv.port) options['port'] = argv.port;
if (argv.legacy_ssl) options['legacySSL'] = argv.legacy_ssl;
var cl = new xmpp.Client(options);
cl.on('online', function() {
console.log('online');
cl.send(new xmpp.Element('presence', { }).
c('show').t('chat').up().
c('status').t('Happily echoing your <message/> stanzas')
);
});
cl.on('stanza', function(stanza) {
if (stanza.is('message') &&
// Important: never reply to errors!
stanza.attrs.type !== 'error') {
// Swap addresses...
stanza.attrs.to = stanza.attrs.from;
delete stanza.attrs.from;
// and send back.
cl.send(stanza);
}
});
cl.on('error', function(e) {
console.error(e);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment