-
Notifications
You must be signed in to change notification settings - Fork 371
/
sasl.js
354 lines (298 loc) · 8.36 KB
/
sasl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* global describe, it, before, after */
'use strict'
var nodexmppserver = require('node-xmpp-server')
var C2SServer = nodexmppserver.C2SServer
var Stanza = require('node-xmpp-core').Stanza
var net = require('net')
var util = require('util')
var Client = require('../index')
var Plain = nodexmppserver.auth.Plain
var XOAuth2 = nodexmppserver.auth.XOAuth2
var DigestMD5 = nodexmppserver.auth.DigestMD5
function ServerCustomMechanism () {}
util.inherits(ServerCustomMechanism, nodexmppserver.auth.AbstractMechanism)
ServerCustomMechanism.prototype.extractSasl = function (auth) {
var authRequest = {
password: auth
}
return authRequest
}
ServerCustomMechanism.prototype.name = 'CUSTOM'
ServerCustomMechanism.id = 'CUSTOM'
function ClientCustomMechanism () {}
util.inherits(ClientCustomMechanism, Client.SASL.AbstractMechanism)
ClientCustomMechanism.prototype.name = 'CUSTOM'
ClientCustomMechanism.prototype.auth = function () {
return this.password
}
ClientCustomMechanism.prototype.match = function () {
return true
}
var user = {
jid: 'me@localhost',
password: 'secret'
}
function startServer (mechanism, done) {
// Sets up the server.
var c2s = new C2SServer({
port: 5222,
domain: 'localhost',
autostart: false
})
if (mechanism) {
// remove plain
c2s.availableSaslMechanisms = []
c2s.registerSaslMechanism(mechanism)
}
c2s.listen()
// 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 (stream) {
// That's the way you add mods to a given server.
// Allows the developer to authenticate users against anything they want.
stream.on('authenticate', function (opts, cb) {
if ((opts.saslmech === Plain.id) &&
(opts.jid.toString() === user.jid) &&
(opts.password === user.password)) {
// PLAIN OKAY
cb(null, opts)
} else if ((opts.saslmech === XOAuth2.id) &&
(opts.jid.toString() === 'me@gmail.com') &&
(opts.oauth_token === 'xxxx.xxxxxxxxxxx')) {
// OAUTH2 OKAY
cb(null, opts)
} else if ((opts.saslmech === DigestMD5.id) &&
(opts.jid.toString() === user.jid)) {
// DIGEST-MD5 OKAY
opts.password = 'secret'
cb(null, opts)
} else if ((opts.saslmech === ServerCustomMechanism.id) &&
(opts.password === user.password)) {
// CUSTOM OKAY
cb(null, opts)
} else {
cb(new Error('Authentication failure'), null)
}
})
stream.on('online', function () {
stream.send(new Stanza('message', {
type: 'chat'
})
.c('body')
.t('Hello there, little client.')
)
})
// Stanza handling
stream.on('stanza', function () {
// got stanza
})
// On Disconnect event. When a client disconnects
stream.on('disconnect', function () {
// client disconnect
})
stream.on('error', function (e) {
done(e)
})
})
c2s.on('error', function (e) {
done(e)
})
return c2s
}
function createClient (opts) {
var cl = new Client(opts)
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)
} else {
console.log('INCLIENT STANZA PRE', stanza.toString())
}
}
)
return cl
}
describe('SASL', function () {
describe('PLAIN', function () {
var c2s = null
before(function (done) {
c2s = startServer(Plain, done)
done()
})
after(function (done) {
c2s.shutdown(done)
})
it('should accept plain authentication', function (done) {
var cl = createClient({
jid: user.jid,
password: user.password,
preferred: Plain.id
})
cl.on('online', function () {
cl.once('offline', done)
cl.end()
})
cl.on('error', function (e) {
done(e)
})
})
it('should not accept plain authentication', function (done) {
var cl = createClient({
jid: user.jid,
password: 'secretsecret'
})
cl.on('online', function () {
done('user is not valid')
})
cl.on('error', function () {
// this should happen
done()
})
})
})
describe('XOAUTH-2', function () {
var c2s = null
before(function (done) {
c2s = startServer(XOAuth2)
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) {
var gtalk = createClient({
jid: 'me@gmail.com',
oauth2_token: 'xxxx.xxxxxxxxxxx', // from OAuth2
oauth2_auth: 'http://www.google.com/talk/protocol/auth',
host: 'localhost'
})
gtalk.on('online', function () {
gtalk.once('offline', done)
gtalk.end()
})
gtalk.on('error', function (e) {
done(e)
})
})
})
describe('DIGEST MD5', function () {
var c2s = null
before(function (done) {
c2s = startServer(DigestMD5)
done()
})
after(function (done) {
c2s.shutdown(done)
})
it('should accept digest md5 authentication', function (done) {
var cl = createClient({
jid: user.jid,
password: user.password,
preferred: 'DIGEST-MD5'
})
cl.on('online', function () {
cl.once('offline', done)
cl.end()
})
cl.on('error', function (e) {
done(e)
})
})
it('should not allow to skip digest md5 challenges', function (done) {
// preparing a sequence of stanzas to send
var handshakeStanza = '<?xml version="1.0" encoding="UTF-8"?>' +
'<stream:stream to="localhost" xmlns="jabber:client" ' +
'xmlns:stream="http://etherx.jabber.org/streams" ' +
'xml:l="en" version="1.0">'
var authStanza = '<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" ' +
'mechanism="DIGEST-MD5"/>'
var earlyAccessStanza = '<response ' +
'xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>'
/*
* we cannot use existing client realization
* because we need to skip challenge response
*/
var client = net.connect({port: c2s.options.port}, function () {
client.write(handshakeStanza, function () {
client.write(authStanza, function () {
client.write(earlyAccessStanza, function () {
// send earlyAccessStanza to receive 'success'
client.write(earlyAccessStanza)
})
})
})
})
var receivedData = ''
client.on('data', function (data) {
receivedData += data
})
client.setTimeout(500, function () {
if (/<\/failure>$/.test(receivedData.toString())) {
client.end()
done()
} else {
client.end()
done('wrong server response')
}
})
})
})
describe('CUSTOM', function () {
var c2s = null
before(function (done) {
c2s = startServer(ServerCustomMechanism, done)
done()
})
after(function (done) {
c2s.shutdown(done)
})
it('should accept custom authentication', function (done) {
var cl = createClient({
jid: user.jid,
password: user.password,
preferred: ClientCustomMechanism.id,
autostart: false
})
cl.clearSaslMechanism()
cl.registerSaslMechanism(ClientCustomMechanism)
cl.connect()
cl.on('online', function () {
cl.once('offline', done)
cl.end()
})
cl.on('error', function (e) {
done(e)
})
})
it('should not accept custom authentication', function (done) {
var cl = createClient({
jid: user.jid,
password: 'secretsecret',
preferred: ClientCustomMechanism.id,
autostart: false
})
cl.clearSaslMechanism()
cl.registerSaslMechanism(ClientCustomMechanism)
cl.connect()
cl.on('online', function () {
done('user is not valid')
})
cl.on('error', function () {
// this should happen
done()
})
})
})
})