author | Chris Jones <chris@cjones.org> |
Wed, 28 Dec 2011 11:35:21 -0700 | |
changeset 14 | fd6781a41e6f |
parent 13 | c9527bbe99a6 |
child 15 | aa2cf77f0ed3 |
permissions | -rw-r--r-- |
10 | 1 |
// Copyright 2011 The Go Authors. All rights reserved. |
2 |
// Use of this source code is governed by a BSD-style |
|
3 |
// license that can be found in the LICENSE file. |
|
4 |
||
5 |
// This file contains the three layers of processing for the |
|
6 |
// communication with the server: transport (where TLS happens), XML |
|
7 |
// (where strings are converted to go structures), and Stream (where |
|
8 |
// we respond to XMPP events on behalf of the library client). |
|
9 |
||
10 |
package xmpp |
|
11 |
||
12 |
import ( |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
13 |
"big" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
14 |
"crypto/md5" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
15 |
"crypto/rand" |
10 | 16 |
"crypto/tls" |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
17 |
"encoding/base64" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
18 |
"fmt" |
10 | 19 |
"io" |
20 |
"log" |
|
21 |
"net" |
|
22 |
"os" |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
23 |
"regexp" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
24 |
"strings" |
10 | 25 |
"time" |
26 |
"xml" |
|
27 |
) |
|
28 |
||
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
29 |
type stanzaHandler struct { |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
30 |
id string |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
31 |
f func(Stanza) bool |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
32 |
} |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
33 |
|
10 | 34 |
func (cl *Client) readTransport(w io.Writer) { |
35 |
defer tryClose(cl.socket, w) |
|
36 |
cl.socket.SetReadTimeout(1e8) |
|
37 |
p := make([]byte, 1024) |
|
38 |
for { |
|
39 |
if cl.socket == nil { |
|
40 |
cl.waitForSocket() |
|
41 |
} |
|
42 |
nr, err := cl.socket.Read(p) |
|
43 |
if nr == 0 { |
|
44 |
if errno, ok := err.(*net.OpError) ; ok { |
|
45 |
if errno.Timeout() { |
|
46 |
continue |
|
47 |
} |
|
48 |
} |
|
49 |
log.Printf("read: %s", err.String()) |
|
50 |
break |
|
51 |
} |
|
52 |
nw, err := w.Write(p[:nr]) |
|
53 |
if nw < nr { |
|
54 |
log.Println("read: %s", err.String()) |
|
55 |
break |
|
56 |
} |
|
57 |
} |
|
58 |
} |
|
59 |
||
60 |
func (cl *Client) writeTransport(r io.Reader) { |
|
61 |
defer tryClose(r, cl.socket) |
|
62 |
p := make([]byte, 1024) |
|
63 |
for { |
|
64 |
nr, err := r.Read(p) |
|
65 |
if nr == 0 { |
|
66 |
log.Printf("write: %s", err.String()) |
|
67 |
break |
|
68 |
} |
|
69 |
nw, err := cl.socket.Write(p[:nr]) |
|
70 |
if nw < nr { |
|
71 |
log.Println("write: %s", err.String()) |
|
72 |
break |
|
73 |
} |
|
74 |
} |
|
75 |
} |
|
76 |
||
77 |
func readXml(r io.Reader, ch chan<- interface{}) { |
|
78 |
if debug { |
|
79 |
pr, pw := io.Pipe() |
|
80 |
go tee(r, pw, "S: ") |
|
81 |
r = pr |
|
82 |
} |
|
83 |
defer tryClose(r, ch) |
|
84 |
||
85 |
p := xml.NewParser(r) |
|
86 |
for { |
|
87 |
// Sniff the next token on the stream. |
|
88 |
t, err := p.Token() |
|
89 |
if t == nil { |
|
90 |
if err != os.EOF { |
|
91 |
log.Printf("read: %v", err) |
|
92 |
} |
|
93 |
break |
|
94 |
} |
|
95 |
var se xml.StartElement |
|
96 |
var ok bool |
|
97 |
if se, ok = t.(xml.StartElement) ; !ok { |
|
98 |
continue |
|
99 |
} |
|
100 |
||
101 |
// Allocate the appropriate structure for this token. |
|
102 |
var obj interface{} |
|
103 |
switch se.Name.Space + " " + se.Name.Local { |
|
104 |
case nsStream + " stream": |
|
105 |
st, err := parseStream(se) |
|
106 |
if err != nil { |
|
107 |
log.Printf("unmarshal stream: %v", |
|
108 |
err) |
|
109 |
break |
|
110 |
} |
|
111 |
ch <- st |
|
112 |
continue |
|
113 |
case "stream error", nsStream + " error": |
|
114 |
obj = &StreamError{} |
|
115 |
case nsStream + " features": |
|
116 |
obj = &Features{} |
|
117 |
case nsTLS + " proceed", nsTLS + " failure": |
|
118 |
obj = &starttls{} |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
119 |
case nsSASL + " challenge", nsSASL + " failure", |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
120 |
nsSASL + " success": |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
121 |
obj = &auth{} |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
122 |
case "jabber:client iq": |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
123 |
obj = &Iq{} |
10 | 124 |
default: |
125 |
obj = &Unrecognized{} |
|
126 |
log.Printf("Ignoring unrecognized: %s %s\n", |
|
127 |
se.Name.Space, se.Name.Local) |
|
128 |
} |
|
129 |
||
130 |
// Read the complete XML stanza. |
|
131 |
err = p.Unmarshal(obj, &se) |
|
132 |
if err != nil { |
|
133 |
log.Printf("unmarshal: %v", err) |
|
134 |
break |
|
135 |
} |
|
136 |
||
137 |
// Put it on the channel. |
|
138 |
ch <- obj |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
func writeXml(w io.Writer, ch <-chan interface{}) { |
|
143 |
if debug { |
|
144 |
pr, pw := io.Pipe() |
|
145 |
go tee(pr, w, "C: ") |
|
146 |
w = pw |
|
147 |
} |
|
148 |
defer tryClose(w, ch) |
|
149 |
||
150 |
for obj := range ch { |
|
151 |
err := xml.Marshal(w, obj) |
|
152 |
if err != nil { |
|
153 |
log.Printf("write: %v", err) |
|
154 |
break |
|
155 |
} |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
func writeText(w io.Writer, ch <-chan *string) { |
|
160 |
if debug { |
|
161 |
pr, pw := io.Pipe() |
|
162 |
go tee(pr, w, "C: ") |
|
163 |
w = pw |
|
164 |
} |
|
165 |
defer tryClose(w, ch) |
|
166 |
||
167 |
for str := range ch { |
|
168 |
_, err := w.Write([]byte(*str)) |
|
169 |
if err != nil { |
|
170 |
log.Printf("writeStr: %v", err) |
|
171 |
break |
|
172 |
} |
|
173 |
} |
|
174 |
} |
|
175 |
||
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
176 |
func (cl *Client) readStream(srvIn <-chan interface{}, cliOut chan<- interface{}) { |
10 | 177 |
defer tryClose(srvIn, cliOut) |
178 |
||
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
179 |
handlers := make(map[string] func(Stanza) bool) |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
180 |
// TODO This for loop will never terminate, even when the |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
181 |
// channels are closed. |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
182 |
for { |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
183 |
select { |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
184 |
case h := <- cl.handlers: |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
185 |
handlers[h.id] = h.f |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
186 |
case x := <- srvIn: |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
187 |
send := false |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
188 |
switch obj := x.(type) { |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
189 |
case *Stream: |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
190 |
handleStream(obj) |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
191 |
case *Features: |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
192 |
cl.handleFeatures(obj) |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
193 |
case *starttls: |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
194 |
cl.handleTls(obj) |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
195 |
case *auth: |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
196 |
cl.handleSasl(obj) |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
197 |
default: |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
198 |
send = true |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
199 |
} |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
200 |
if st, ok := x.(Stanza) ; ok && |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
201 |
handlers[st.XId()] != nil { |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
202 |
f := handlers[st.XId()] |
14
fd6781a41e6f
Don't forget to remove the stanza handler after it's been used.
Chris Jones <chris@cjones.org>
parents:
13
diff
changeset
|
203 |
handlers[st.XId()] = nil |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
204 |
send = f(st) |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
205 |
} |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
206 |
if send { |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
207 |
cliOut <- x |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
208 |
} |
10 | 209 |
} |
210 |
} |
|
211 |
} |
|
212 |
||
213 |
func writeStream(srvOut chan<- interface{}, cliIn <-chan interface{}) { |
|
214 |
defer tryClose(srvOut, cliIn) |
|
215 |
||
216 |
for x := range cliIn { |
|
217 |
srvOut <- x |
|
218 |
} |
|
219 |
} |
|
220 |
||
221 |
func handleStream(ss *Stream) { |
|
222 |
} |
|
223 |
||
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
224 |
func (cl *Client) handleFeatures(fe *Features) { |
10 | 225 |
if fe.Starttls != nil { |
226 |
start := &starttls{XMLName: xml.Name{Space: nsTLS, |
|
227 |
Local: "starttls"}} |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
228 |
cl.xmlOut <- start |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
229 |
return |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
230 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
231 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
232 |
if len(fe.Mechanisms.Mechanism) > 0 { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
233 |
cl.chooseSasl(fe) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
234 |
return |
10 | 235 |
} |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
236 |
|
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
237 |
if fe.Bind != nil { |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
238 |
cl.bind(fe.Bind) |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
239 |
} |
10 | 240 |
} |
241 |
||
242 |
// readTransport() is running concurrently. We need to stop it, |
|
243 |
// negotiate TLS, then start it again. It calls waitForSocket() in |
|
244 |
// its inner loop; see below. |
|
245 |
func (cl *Client) handleTls(t *starttls) { |
|
246 |
tcp := cl.socket |
|
247 |
||
248 |
// Set the socket to nil, and wait for the reader routine to |
|
249 |
// signal that it's paused. |
|
250 |
cl.socket = nil |
|
251 |
cl.socketSync.Add(1) |
|
252 |
cl.socketSync.Wait() |
|
253 |
||
254 |
// Negotiate TLS with the server. |
|
255 |
tls := tls.Client(tcp, nil) |
|
256 |
||
257 |
// Make the TLS connection available to the reader, and wait |
|
258 |
// for it to signal that it's working again. |
|
259 |
cl.socketSync.Add(1) |
|
260 |
cl.socket = tls |
|
261 |
cl.socketSync.Wait() |
|
262 |
||
263 |
// Reset the read timeout on the (underlying) socket so the |
|
264 |
// reader doesn't get woken up unnecessarily. |
|
265 |
tcp.SetReadTimeout(0) |
|
266 |
||
267 |
log.Println("TLS negotiation succeeded.") |
|
268 |
||
269 |
// Now re-send the initial handshake message to start the new |
|
270 |
// session. |
|
271 |
hsOut := &Stream{To: cl.Jid.Domain, Version: Version} |
|
272 |
cl.xmlOut <- hsOut |
|
273 |
} |
|
274 |
||
275 |
// Synchronize with handleTls(). Called from readTransport() when |
|
276 |
// cl.socket is nil. |
|
277 |
func (cl *Client) waitForSocket() { |
|
278 |
// Signal that we've stopped reading from the socket. |
|
279 |
cl.socketSync.Done() |
|
280 |
||
281 |
// Wait until the socket is available again. |
|
282 |
for cl.socket == nil { |
|
283 |
time.Sleep(1e8) |
|
284 |
} |
|
285 |
||
286 |
// Signal that we're going back to the read loop. |
|
287 |
cl.socketSync.Done() |
|
288 |
} |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
289 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
290 |
func (cl *Client) chooseSasl(fe *Features) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
291 |
var digestMd5 bool |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
292 |
for _, m := range(fe.Mechanisms.Mechanism) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
293 |
switch strings.ToLower(m) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
294 |
case "digest-md5": |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
295 |
digestMd5 = true |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
296 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
297 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
298 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
299 |
if digestMd5 { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
300 |
auth := &auth{XMLName: xml.Name{Space: nsSASL, Local: |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
301 |
"auth"}, Mechanism: "DIGEST-MD5"} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
302 |
cl.xmlOut <- auth |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
303 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
304 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
305 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
306 |
func (cl *Client) handleSasl(srv *auth) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
307 |
switch strings.ToLower(srv.XMLName.Local) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
308 |
case "challenge": |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
309 |
b64 := base64.StdEncoding |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
310 |
str, err := b64.DecodeString(srv.Chardata) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
311 |
if err != nil { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
312 |
log.Printf("SASL challenge decode: %s", |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
313 |
err.String()) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
314 |
return; |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
315 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
316 |
srvMap := parseSasl(string(str)) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
317 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
318 |
if cl.saslExpected == "" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
319 |
cl.saslDigest1(srvMap) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
320 |
} else { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
321 |
cl.saslDigest2(srvMap) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
322 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
323 |
case "failure": |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
324 |
log.Println("SASL authentication failed") |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
325 |
case "success": |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
326 |
log.Println("SASL authentication succeeded") |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
327 |
ss := &Stream{To: cl.Jid.Domain, Version: Version} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
328 |
cl.xmlOut <- ss |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
329 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
330 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
331 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
332 |
func (cl *Client) saslDigest1(srvMap map[string] string) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
333 |
// Make sure it supports qop=auth |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
334 |
var hasAuth bool |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
335 |
for _, qop := range(strings.Fields(srvMap["qop"])) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
336 |
if qop == "auth" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
337 |
hasAuth = true |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
338 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
339 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
340 |
if !hasAuth { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
341 |
log.Println("Server doesn't support SASL auth") |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
342 |
return; |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
343 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
344 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
345 |
// Pick a realm. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
346 |
var realm string |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
347 |
if srvMap["realm"] != "" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
348 |
realm = strings.Fields(srvMap["realm"])[0] |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
349 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
350 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
351 |
passwd := cl.password |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
352 |
nonce := srvMap["nonce"] |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
353 |
digestUri := "xmpp/" + cl.Jid.Domain |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
354 |
nonceCount := int32(1) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
355 |
nonceCountStr := fmt.Sprintf("%08x", nonceCount) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
356 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
357 |
// Begin building the response. Username is |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
358 |
// user@domain or just domain. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
359 |
var username string |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
360 |
if cl.Jid.Node == nil { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
361 |
username = cl.Jid.Domain |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
362 |
} else { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
363 |
username = *cl.Jid.Node |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
364 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
365 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
366 |
// Generate our own nonce from random data. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
367 |
randSize := big.NewInt(0) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
368 |
randSize.Lsh(big.NewInt(1), 64) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
369 |
cnonce, err := rand.Int(rand.Reader, randSize) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
370 |
if err != nil { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
371 |
log.Println("SASL rand: %s", err.String()) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
372 |
return |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
373 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
374 |
cnonceStr := fmt.Sprintf("%016x", cnonce) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
375 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
376 |
/* Now encode the actual password response, as well as the |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
377 |
* expected next challenge from the server. */ |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
378 |
response := saslDigestResponse(username, realm, passwd, nonce, |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
379 |
cnonceStr, "AUTHENTICATE", digestUri, nonceCountStr) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
380 |
next := saslDigestResponse(username, realm, passwd, nonce, |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
381 |
cnonceStr, "", digestUri, nonceCountStr) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
382 |
cl.saslExpected = next |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
383 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
384 |
// Build the map which will be encoded. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
385 |
clMap := make(map[string]string) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
386 |
clMap["realm"] = `"` + realm + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
387 |
clMap["username"] = `"` + username + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
388 |
clMap["nonce"] = `"` + nonce + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
389 |
clMap["cnonce"] = `"` + cnonceStr + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
390 |
clMap["nc"] = nonceCountStr |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
391 |
clMap["qop"] = "auth" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
392 |
clMap["digest-uri"] = `"` + digestUri + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
393 |
clMap["response"] = response |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
394 |
if srvMap["charset"] == "utf-8" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
395 |
clMap["charset"] = "utf-8" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
396 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
397 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
398 |
// Encode the map and send it. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
399 |
clStr := packSasl(clMap) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
400 |
b64 := base64.StdEncoding |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
401 |
clObj := &auth{XMLName: xml.Name{Space: nsSASL, Local: |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
402 |
"response"}, Chardata: |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
403 |
b64.EncodeToString([]byte(clStr))} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
404 |
cl.xmlOut <- clObj |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
405 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
406 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
407 |
func (cl *Client) saslDigest2(srvMap map[string] string) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
408 |
if cl.saslExpected == srvMap["rspauth"] { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
409 |
clObj := &auth{XMLName: xml.Name{Space: nsSASL, Local: |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
410 |
"response"}} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
411 |
cl.xmlOut <- clObj |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
412 |
} else { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
413 |
clObj := &auth{XMLName: xml.Name{Space: nsSASL, Local: |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
414 |
"failure"}, Any: |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
415 |
&Unrecognized{XMLName: xml.Name{Space: nsSASL, |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
416 |
Local: "abort"}}} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
417 |
cl.xmlOut <- clObj |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
418 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
419 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
420 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
421 |
// Takes a string like `key1=value1,key2="value2"...` and returns a |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
422 |
// key/value map. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
423 |
func parseSasl(in string) map[string]string { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
424 |
re := regexp.MustCompile(`([^=]+)="?([^",]+)"?,?`) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
425 |
strs := re.FindAllStringSubmatch(in, -1) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
426 |
m := make(map[string]string) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
427 |
for _, pair := range(strs) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
428 |
key := strings.ToLower(string(pair[1])) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
429 |
value := string(pair[2]) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
430 |
m[key] = value |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
431 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
432 |
return m |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
433 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
434 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
435 |
func packSasl(m map[string]string) string { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
436 |
var terms []string |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
437 |
for key, value := range(m) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
438 |
if key == "" || value == "" || value == `""` { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
439 |
continue |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
440 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
441 |
terms = append(terms, key + "=" + value) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
442 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
443 |
return strings.Join(terms, ",") |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
444 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
445 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
446 |
func saslDigestResponse(username, realm, passwd, nonce, cnonceStr, |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
447 |
authenticate, digestUri, nonceCountStr string) string { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
448 |
h := func(text string) []byte { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
449 |
h := md5.New() |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
450 |
h.Write([]byte(text)) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
451 |
return h.Sum() |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
452 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
453 |
hex := func(bytes []byte) string { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
454 |
return fmt.Sprintf("%x", bytes) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
455 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
456 |
kd := func(secret, data string) []byte { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
457 |
return h(secret + ":" + data) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
458 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
459 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
460 |
a1 := string(h(username + ":" + realm + ":" + passwd)) + ":" + |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
461 |
nonce + ":" + cnonceStr |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
462 |
a2 := authenticate + ":" + digestUri |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
463 |
response := hex(kd(hex(h(a1)), nonce + ":" + |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
464 |
nonceCountStr + ":" + cnonceStr + ":auth:" + |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
465 |
hex(h(a2)))) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
466 |
return response |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
467 |
} |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
468 |
|
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
469 |
func (cl *Client) bind(bind *Unrecognized) { |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
470 |
res := cl.Jid.Resource |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
471 |
msg := &Iq{Type: "set", Id: cl.NextId(), Any: |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
472 |
&Unrecognized{XMLName: xml.Name{Space: nsBind, Local: |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
473 |
"bind"}}} |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
474 |
if res != "" { |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
475 |
msg.Any.Any = &Unrecognized{XMLName: xml.Name{Local: |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
476 |
"resource"}, Chardata: res} |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
477 |
} |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
478 |
cl.xmlOut <- msg |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
479 |
} |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
480 |
|
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
481 |
func (cl *Client) HandleStanza(id string, f func(Stanza) bool) { |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
482 |
h := &stanzaHandler{id: id, f: f} |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
483 |
cl.handlers <- h |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
484 |
} |