annotate XMPP.js @ 0:96898e3812a5

initial push
author Sonny Piers <sonny.piers@gmail.com>
date Sun, 18 Dec 2011 19:03:28 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
1 'use strict';
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
2
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
3 var Lightstring = {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
4 NS: {},
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
5 stanza: {},
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
6 };
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
7
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
8 Lightstring.Connection = function (aURL) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
9 var parser = new DOMParser();
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
10 var serializer = new XMLSerializer();
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
11 this.handlers = {};
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
12 this.iqid = 1024;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
13 this.getNewId = function() {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
14 this.iqid++;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
15 return 'sendiq:'+this.iqid;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
16 };
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
17 this.parse = function(str) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
18 return parser.parseFromString(str, 'text/xml').documentElement;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
19 };
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
20 this.serialize = function(elm) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
21 return serializer.serializeToString(elm);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
22 };
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
23 this.connect = function(jid, password) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
24 this.domain = jid.split('@')[1];
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
25 this.node = jid.split('@')[0];
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
26 this.jid = jid;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
27 this.password = password;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
28 if(typeof WebSocket === 'undefined')
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
29 this.socket = new MozWebSocket(aURL);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
30 else
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
31 this.socket = new WebSocket(aURL);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
32
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
33 var that = this;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
34 this.socket.addEventListener('open', function() {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
35 that.emit('open');
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
36 that.send("<stream:stream to='"+that.domain+"' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' />");
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
37 });
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
38 this.socket.addEventListener('error', function(err) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
39 that.emit('error');
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
40 });
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
41 this.socket.addEventListener('close', function(close) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
42 that.emit('close');
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
43 that.emit('disconnected');
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
44 });
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
45 this.socket.addEventListener('message', function(e) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
46 that.emit('XMLInput', e.data);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
47 var elm = that.parse(e.data);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
48 that.emit('DOMInput', elm);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
49 that.emit(elm.tagName, elm);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
50
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
51 if((elm.tagName === 'iq'))
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
52 that.emit(elm.getAttribute('id'), elm);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
53 });
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
54 };
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
55
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
56
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
57
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
58 this.send = function(stanza, callback) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
59 //FIXME support for E4X
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
60 //~ if(typeof stanza === 'xml') {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
61 //~ stanza = stanza.toXMLString();
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
62 //~ }
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
63 if(stanza.cnode) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
64 stanza = stanza.toString();
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
65 //~ console.log(typeof stanza);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
66 }
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
67 if(typeof stanza === 'string') {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
68 var str = stanza;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
69 var elm = this.parse(stanza);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
70 }
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
71 else {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
72 var elm = stanza;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
73 var str = this.serialize(stanza);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
74 }
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
75
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
76
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
77 if(elm.tagName === 'iq') {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
78 var id = elm.getAttribute('id');
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
79 if(!id) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
80 elm.setAttribute('id', this.getNewId())
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
81 str = this.serialize(elm)
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
82 }
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
83 if(callback) this.on(elm.getAttribute('id'), callback);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
84 }
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
85
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
86
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
87 this.socket.send(str);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
88 this.emit('XMLOutput', str);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
89 this.emit('DOMOutput', elm);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
90 };
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
91 this.disconnect = function() {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
92 this.send('</stream:stream>');
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
93 this.emit('disconnected');
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
94 this.socket.close();
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
95 };
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
96 //FIXME Callbacks sucks, better idea?
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
97 this.emit = function(name, data) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
98 var handlers = this.handlers[name];
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
99 if(!handlers)
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
100 return;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
101
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
102 //FIXME Better idea than passing the context as argument?
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
103 for(var i=0; i<handlers.length; i++)
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
104 handlers[i](data, this);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
105
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
106 if(name.match('sendiq:'))
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
107 delete this.handlers[name];
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
108 };
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
109 this.on = function(name, callback) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
110 if(!this.handlers[name])
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
111 this.handlers[name] = [];
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
112 this.handlers[name].push(callback);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
113 };
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
114 //FIXME do this!
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
115 this.once = function(name, callback) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
116 if(!this.handlers[name])
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
117 this.handlers[name] = [];
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
118 this.handlers[name].push(callback);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
119 };
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
120 //Internal
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
121 this.on('stream:features', function(stanza, that) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
122 var nodes = stanza.querySelectorAll('mechanism');
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
123 //SASL/Auth features
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
124 if(nodes.length > 0) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
125 that.emit('mechanisms', stanza);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
126 var mechanisms = {};
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
127 for(var i=0; i<nodes.length; i++)
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
128 mechanisms[nodes[i].textContent] = true;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
129
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
130
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
131 //FIXME support SCRAM-SHA1 && allow specify method preferences
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
132 if('DIGEST-MD5' in mechanisms)
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
133 that.send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='DIGEST-MD5'/>");
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
134 else if('PLAIN' in mechanisms) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
135 var token = btoa(that.jid + "\u0000" + that.jid.split('@')[0] + "\u0000" + that.password);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
136 that.send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>"+token+"</auth>");
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
137 }
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
138 }
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
139 //XMPP features
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
140 else {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
141 that.emit('features', stanza);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
142 //Bind http://xmpp.org/rfcs/rfc3920.html#bind
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
143 that.send("<iq type='set' xmlns='jabber:client'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/></iq>", function() {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
144 //Session http://xmpp.org/rfcs/rfc3921.html#session
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
145 that.send("<iq type='set' xmlns='jabber:client'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>", function() {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
146 that.emit('connected');
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
147 });
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
148 });
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
149 }
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
150 });
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
151 //Internal
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
152 this.on('success', function(stanza, that) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
153 that.send("<stream:stream to='"+that.domain+"' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' />");
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
154 });
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
155 //Internal
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
156 this.on('challenge', function(stanza, that) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
157 //FIXME this is mostly Strophe code
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
158
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
159 function _quote(str) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
160 return '"' + str.replace(/\\/g, "\\\\").replace(/"/g, '\\"') + '"';
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
161 };
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
162
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
163 var challenge = atob(stanza.textContent);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
164
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
165 var attribMatch = /([a-z]+)=("[^"]+"|[^,"]+)(?:,|$)/;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
166
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
167 var cnonce = MD5.hexdigest(Math.random() * 1234567890);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
168 var realm = "";
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
169 var host = null;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
170 var nonce = "";
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
171 var qop = "";
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
172 var matches;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
173
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
174 while (challenge.match(attribMatch)) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
175 matches = challenge.match(attribMatch);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
176 challenge = challenge.replace(matches[0], "");
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
177 matches[2] = matches[2].replace(/^"(.+)"$/, "$1");
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
178 switch (matches[1]) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
179 case "realm":
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
180 realm = matches[2];
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
181 break;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
182 case "nonce":
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
183 nonce = matches[2];
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
184 break;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
185 case "qop":
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
186 qop = matches[2];
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
187 break;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
188 case "host":
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
189 host = matches[2];
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
190 break;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
191 }
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
192 }
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
193
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
194 var digest_uri = "xmpp/" + that.domain;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
195 if (host !== null) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
196 digest_uri = digest_uri + "/" + host;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
197 }
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
198
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
199 var A1 = MD5.hash(that.node +
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
200 ":" + realm + ":" + that.password) +
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
201 ":" + nonce + ":" + cnonce;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
202 var A2 = 'AUTHENTICATE:' + digest_uri;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
203
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
204 var responseText = "";
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
205 responseText += 'username=' + _quote(that.node) + ',';
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
206 responseText += 'realm=' + _quote(realm) + ',';
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
207 responseText += 'nonce=' + _quote(nonce) + ',';
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
208 responseText += 'cnonce=' + _quote(cnonce) + ',';
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
209 responseText += 'nc="00000001",';
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
210 responseText += 'qop="auth",';
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
211 responseText += 'digest-uri=' + _quote(digest_uri) + ',';
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
212 responseText += 'response=' + _quote(
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
213 MD5.hexdigest(MD5.hexdigest(A1) + ":" +
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
214 nonce + ":00000001:" +
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
215 cnonce + ":auth:" +
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
216 MD5.hexdigest(A2))) + ',';
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
217 responseText += 'charset="utf-8"';
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
218
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
219 that.send("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"+btoa(responseText)+"</response>");
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
220 });
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
221 };