Browse files

update tests to meet new sasl authentication behavior

  • Loading branch information...
1 parent 71f8cfb commit 497b3964e1ab3525a4c575782d8b437a5a91cbf8 @chris-rock chris-rock committed Dec 5, 2013
Showing with 124 additions and 43 deletions.
  1. +1 −1 test/bosh-test.js
  2. +120 −39 test/sasl-test.js
  3. +1 −1 test/server-event-test.js
  4. +1 −1 test/tcp-test.js
  5. +1 −1 test/tls-test.js
View
2 test/bosh-test.js
@@ -20,7 +20,7 @@ describe('BOSH client/server', function() {
svcl = svcl_
c2s = new C2SStream({ connection: svcl })
c2s.on('authenticate', function(opts, cb) {
- cb()
+ cb(null, opts)
})
})
View
159 test/sasl-test.js
@@ -1,65 +1,82 @@
'use strict';
var xmpp = require('../index')
+ , Client = xmpp.Client
+ , Message = xmpp.Message
+ , Plain = require('node-xmpp-server/lib/authentication/plain')
, XOAuth = require('node-xmpp-server/lib/authentication/xoauth2')
+ , DigestMD5 = require('node-xmpp-server/lib/authentication/digestmd5')
var user = {
jid: 'me@localhost',
password: 'secret'
}
-var c2s = null
-function startServer() {
+function startServer(mechanism) {
// Sets up the server.
- c2s = new xmpp.C2SServer({
+ var c2s = new xmpp.C2SServer({
port: 5222,
domain: 'localhost'
})
- c2s.registerSaslMechanism(new XOAuth())
+ if (mechanism) {
+ // remove plain
+ c2s.availableSaslMechanisms = [];
+ c2s.registerSaslMechanism(mechanism)
+ }
+
+ // Allows the developer to register the jid against anything they want
+ c2s.on('register', function(opts, cb) {
+ cb(true)
+ })
// On Connect event. When a client connects.
- c2s.on('connect', function(client) {
+ c2s.on('connect', function(stream) {
// That's the way you add mods to a given server.
- // Allows the developer to register the jid against anything they want
- c2s.on('register', function(opts, cb) {
- cb(true)
- })
-
// Allows the developer to authenticate users against anything they want.
- client.on('authenticate', function(opts, cb) {
+ stream.on('authenticate', function(opts, cb) {
/*jshint camelcase: false */
- if ((opts.saslmech = 'PLAIN') &&
+ if ((opts.saslmech === 'PLAIN') &&
(opts.jid.toString() === user.jid) &&
(opts.password === user.password)) {
- cb(false)
- } else if ((opts.saslmech = 'X-OAUTH2') &&
- (opts.jid.toString() === 'me@gmail.com') &&
- (opts.oauth_token === 'xxxx.xxxxxxxxxxx')) {
- cb(false)
+ // PLAIN OKAY
+ cb(null, opts)
+ } else if ((opts.saslmech === 'X-OAUTH2') &&
+ (opts.jid.toString() === 'me@gmail.com') &&
+ (opts.oauth_token === 'xxxx.xxxxxxxxxxx')) {
+ // OAUTH2 OKAY
+ cb(null, opts)
+ } else if ((opts.saslmech === 'DIGEST-MD5') &&
+ (opts.jid.toString() === user.jid)) {
+ // DIGEST-MD5 OKAY
+
+ opts.password = "secret"
+ cb(null, opts)
} else {
- cb(new Error('Authentication failure'))
+ cb(new Error('Authentication failure'), null)
}
})
- client.on('online', function() {
- client.send(new xmpp.Message({ type: 'chat' })
+ stream.on('online', function() {
+ stream.send(new Message({
+ type: 'chat'
+ })
.c('body')
.t('Hello there, little client.')
)
})
// Stanza handling
- client.on('stanza', function() {
- //console.log('STANZA' + stanza)
+ stream.on('stanza', function() {
+ // got stanza
})
// On Disconnect event. When a client disconnects
- client.on('disconnect', function() {
- //console.log('DISCONNECT')
+ stream.on('disconnect', function() {
+ // client disconnect
})
})
@@ -86,25 +103,27 @@ function registerHandler(cl) {
)
}
-
describe('SASL', function() {
+ describe('PLAIN', function() {
+ var c2s = null
- before(function(done) {
- startServer()
- done()
- })
+ before(function(done) {
+ c2s = startServer(Plain)
+ done()
+ })
- after(function(done) {
- c2s.shutdown()
- done()
- })
+ after(function(done) {
+ c2s.shutdown()
+ done()
+ })
- describe('server', function() {
it('should accept plain authentication', function(done) {
+
var cl = new xmpp.Client({
jid: user.jid,
- password: user.password
- })
+ password: user.password,
+ preferred: 'PLAIN'
+ })
registerHandler(cl)
@@ -119,7 +138,7 @@ describe('SASL', function() {
})
it('should not accept plain authentication', function(done) {
- var cl = new xmpp.Client({
+ var cl = new Client({
jid: user.jid,
password: 'secretsecret'
})
@@ -135,14 +154,28 @@ describe('SASL', function() {
})
})
+ })
+
+ describe('XOAUTH-2', function() {
+ var c2s = null
+
+ before(function(done) {
+ c2s = startServer(XOAuth)
+ done()
+ })
+
+ after(function(done) {
+ c2s.shutdown()
+ done()
+ })
/*
* google talk is replaced by google hangout,
* but we can support the protocol anyway
*/
it('should accept google authentication', function(done) {
/*jshint camelcase: false */
- var gtalk = new xmpp.Client({
+ var gtalk = new Client({
jid: 'me@gmail.com',
oauth2_token: 'xxxx.xxxxxxxxxxx', // from OAuth2
oauth2_auth: 'http://www.google.com/talk/protocol/auth',
@@ -159,5 +192,53 @@ describe('SASL', function() {
done(e)
})
})
+
+ })
+
+ describe('DIGEST MD5', function() {
+ var c2s = null
+
+ before(function (done) {
+ c2s = startServer(DigestMD5);
+
+ c2s.on('connect', function(stream) {
+
+ stream.on('authenticate-digestmd5', function(opts, cb) {
+ console.log("authenticate-digestmd5 %s", JSON.stringify(opts));
+ if (opts === 'me') {
+ cb('secret');
+ } else {
+ cb();
+ }
+ });
+ })
+
+ done()
+ })
+
+ after(function(done) {
+ c2s.shutdown()
+ done()
+ })
+
+
+ it('should accept digest md5 authentication', function(done) {
+ var cl = new Client({
+ jid: user.jid,
+ password: user.password,
+ preferred: 'DIGEST-MD5'
+ })
+
+ registerHandler(cl)
+
+ cl.on('online', function() {
+ done()
+ })
+ cl.on('error', function(e) {
+ console.log(e)
+ done(e)
+ })
+
+ })
})
-})
+})
View
2 test/server-event-test.js
@@ -26,7 +26,7 @@ function startServer() {
// allow anything
client.on('authenticate', function(opts, cb) {
eventChain.push('authenticate')
- cb(null)
+ cb(null, opts)
})
client.on('online', function() {
View
2 test/tcp-test.js
@@ -21,7 +21,7 @@ describe('TCP client/server', function() {
svcl = svcl_
// Always authenticate
svcl.on('authenticate', function(opts, cb) {
- cb()
+ cb(null, opts)
})
})
var cl
View
2 test/tls-test.js
@@ -39,7 +39,7 @@ function startServer(done) {
if ((opts.saslmech = 'PLAIN') &&
(opts.jid.toString() === user.jid) &&
(opts.password === user.password)) {
- cb(false)
+ cb(null, opts)
} else {
cb(new Error('Authentication failure'))
}

0 comments on commit 497b396

Please sign in to comment.