annotate plugins/PLAIN.js @ 64:d9f5ae0b6d98

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