Mercurial > eldonilo > lightstring
comparison plugins/PLAIN.js @ 63:20da4fb67977
Auth PLAIN as plugin. Several fixes.
author | Sonny Piers <sonny.piers@gmail.com> |
---|---|
date | Wed, 01 Feb 2012 19:24:41 +0100 |
parents | |
children | d9f5ae0b6d98 |
comparison
equal
deleted
inserted
replaced
62:b1e75cdbb0ad | 63:20da4fb67977 |
---|---|
1 'use strict'; | |
2 | |
3 /** | |
4 Copyright (c) 2012, Sonny Piers <sonny at fastmail dot net> | |
5 | |
6 Permission to use, copy, modify, and/or distribute this software for any | |
7 purpose with or without fee is hereby granted, provided that the above | |
8 copyright notice and this permission notice appear in all copies. | |
9 | |
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 Lightstring.plugins['PLAIN'] = { | |
20 handlers: { | |
21 'mechanisms': function (stanza) { | |
22 if(stanza.mechanisms.indexOf('PLAIN') === -1) | |
23 return; | |
24 | |
25 var token = btoa( | |
26 this.jid + | |
27 '\u0000' + | |
28 this.jid.node + | |
29 '\u0000' + | |
30 this.password | |
31 ); | |
32 this.send( | |
33 "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl'" + | |
34 " mechanism='PLAIN'>" + token + "</auth>" | |
35 ); | |
36 }, | |
37 //TODO twice success event | |
38 'success': function (stanza) { | |
39 this.send( | |
40 "<stream:stream to='" + this.jid.domain + "'" + | |
41 " xmlns='jabber:client'" + | |
42 " xmlns:stream='http://etherx.jabber.org/streams'" + | |
43 " version='1.0'/>" | |
44 ); | |
45 }, | |
46 'features': function (stanza) { | |
47 var that = this; | |
48 //TODO check if bind supported | |
49 var bind = | |
50 "<iq type='set' id='"+Lightstring.newId('sendiq:')+"' xmlns='jabber:client'>" + | |
51 "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>" + | |
52 (this.jid.resource? "<resource>" + this.jid.resource + "</resource>": "") + | |
53 "</bind>" + | |
54 "</iq>"; | |
55 this.send( | |
56 bind, | |
57 //Success | |
58 function(stanza) { | |
59 //Session http://xmpp.org/rfcs/rfc3921.html#session | |
60 that.jid = new Lightstring.JID(stanza.DOM.textContent); | |
61 that.send( | |
62 "<iq type='set' id='"+Lightstring.newId('sendiq:')+"' xmlns='jabber:client'>" + | |
63 "<session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>" + | |
64 "</iq>", | |
65 function() { | |
66 that.emit('connected'); | |
67 } | |
68 ); | |
69 }, | |
70 //Error | |
71 function(stanza) { | |
72 //TODO: Error? | |
73 } | |
74 ); | |
75 } | |
76 } | |
77 }; |