author | Chris Jones <chris@cjones.org> |
Wed, 28 Dec 2011 13:14:46 -0700 | |
changeset 22 | d6b7b4cbf50d |
parent 21 | 8f6ae5cfc9b9 |
child 23 | b5de44679389 |
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 |
|
17 | 8 |
// we respond to XMPP events on behalf of the library client), or send |
9 |
// those events to the client. |
|
10 | 10 |
|
11 |
package xmpp |
|
12 |
||
13 |
import ( |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
14 |
"big" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
15 |
"crypto/md5" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
16 |
"crypto/rand" |
10 | 17 |
"crypto/tls" |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
18 |
"encoding/base64" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
19 |
"fmt" |
10 | 20 |
"io" |
21 |
"log" |
|
22 |
"net" |
|
23 |
"os" |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
24 |
"regexp" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
25 |
"strings" |
10 | 26 |
"time" |
27 |
"xml" |
|
28 |
) |
|
29 |
||
17 | 30 |
// Callback to handle a stanza with a particular id. |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
31 |
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
|
32 |
id string |
17 | 33 |
// Return true means pass this to the application |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
34 |
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
|
35 |
} |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
36 |
|
20
e119444a1119
Replaced TODO comments with Go-style BUG(me) comments.
Chris Jones <chris@cjones.org>
parents:
19
diff
changeset
|
37 |
// BUG(cjyar) Review all these *Client receiver methods. They should |
17 | 38 |
// probably either all be receivers, or none. |
39 |
||
10 | 40 |
func (cl *Client) readTransport(w io.Writer) { |
41 |
defer tryClose(cl.socket, w) |
|
42 |
cl.socket.SetReadTimeout(1e8) |
|
43 |
p := make([]byte, 1024) |
|
44 |
for { |
|
45 |
if cl.socket == nil { |
|
46 |
cl.waitForSocket() |
|
47 |
} |
|
48 |
nr, err := cl.socket.Read(p) |
|
49 |
if nr == 0 { |
|
50 |
if errno, ok := err.(*net.OpError) ; ok { |
|
51 |
if errno.Timeout() { |
|
52 |
continue |
|
53 |
} |
|
54 |
} |
|
55 |
log.Printf("read: %s", err.String()) |
|
56 |
break |
|
57 |
} |
|
58 |
nw, err := w.Write(p[:nr]) |
|
59 |
if nw < nr { |
|
60 |
log.Println("read: %s", err.String()) |
|
61 |
break |
|
62 |
} |
|
63 |
} |
|
64 |
} |
|
65 |
||
66 |
func (cl *Client) writeTransport(r io.Reader) { |
|
67 |
defer tryClose(r, cl.socket) |
|
68 |
p := make([]byte, 1024) |
|
69 |
for { |
|
70 |
nr, err := r.Read(p) |
|
71 |
if nr == 0 { |
|
72 |
log.Printf("write: %s", err.String()) |
|
73 |
break |
|
74 |
} |
|
75 |
nw, err := cl.socket.Write(p[:nr]) |
|
76 |
if nw < nr { |
|
77 |
log.Println("write: %s", err.String()) |
|
78 |
break |
|
79 |
} |
|
80 |
} |
|
81 |
} |
|
82 |
||
83 |
func readXml(r io.Reader, ch chan<- interface{}) { |
|
84 |
if debug { |
|
85 |
pr, pw := io.Pipe() |
|
86 |
go tee(r, pw, "S: ") |
|
87 |
r = pr |
|
88 |
} |
|
89 |
defer tryClose(r, ch) |
|
90 |
||
91 |
p := xml.NewParser(r) |
|
92 |
for { |
|
93 |
// Sniff the next token on the stream. |
|
94 |
t, err := p.Token() |
|
95 |
if t == nil { |
|
96 |
if err != os.EOF { |
|
97 |
log.Printf("read: %v", err) |
|
98 |
} |
|
99 |
break |
|
100 |
} |
|
101 |
var se xml.StartElement |
|
102 |
var ok bool |
|
103 |
if se, ok = t.(xml.StartElement) ; !ok { |
|
104 |
continue |
|
105 |
} |
|
106 |
||
107 |
// Allocate the appropriate structure for this token. |
|
108 |
var obj interface{} |
|
109 |
switch se.Name.Space + " " + se.Name.Local { |
|
110 |
case nsStream + " stream": |
|
111 |
st, err := parseStream(se) |
|
112 |
if err != nil { |
|
113 |
log.Printf("unmarshal stream: %v", |
|
114 |
err) |
|
115 |
break |
|
116 |
} |
|
117 |
ch <- st |
|
118 |
continue |
|
119 |
case "stream error", nsStream + " error": |
|
120 |
obj = &StreamError{} |
|
121 |
case nsStream + " features": |
|
122 |
obj = &Features{} |
|
123 |
case nsTLS + " proceed", nsTLS + " failure": |
|
124 |
obj = &starttls{} |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
125 |
case nsSASL + " challenge", nsSASL + " failure", |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
126 |
nsSASL + " success": |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
127 |
obj = &auth{} |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
128 |
case "jabber:client iq": |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
129 |
obj = &Iq{} |
16
b839e37b3f29
Parse <presence> and <message> stanzas.
Chris Jones <chris@cjones.org>
parents:
15
diff
changeset
|
130 |
case "jabber:client message": |
b839e37b3f29
Parse <presence> and <message> stanzas.
Chris Jones <chris@cjones.org>
parents:
15
diff
changeset
|
131 |
obj = &Message{} |
b839e37b3f29
Parse <presence> and <message> stanzas.
Chris Jones <chris@cjones.org>
parents:
15
diff
changeset
|
132 |
case "jabber:client presence": |
b839e37b3f29
Parse <presence> and <message> stanzas.
Chris Jones <chris@cjones.org>
parents:
15
diff
changeset
|
133 |
obj = &Presence{} |
10 | 134 |
default: |
21
8f6ae5cfc9b9
Renamed Unrecognized to Generic.
Chris Jones <chris@cjones.org>
parents:
20
diff
changeset
|
135 |
obj = &Generic{} |
10 | 136 |
log.Printf("Ignoring unrecognized: %s %s\n", |
137 |
se.Name.Space, se.Name.Local) |
|
138 |
} |
|
139 |
||
140 |
// Read the complete XML stanza. |
|
141 |
err = p.Unmarshal(obj, &se) |
|
142 |
if err != nil { |
|
143 |
log.Printf("unmarshal: %v", err) |
|
144 |
break |
|
145 |
} |
|
146 |
||
20
e119444a1119
Replaced TODO comments with Go-style BUG(me) comments.
Chris Jones <chris@cjones.org>
parents:
19
diff
changeset
|
147 |
// BUG(cjyar) If it's a Stanza, use reflection to |
21
8f6ae5cfc9b9
Renamed Unrecognized to Generic.
Chris Jones <chris@cjones.org>
parents:
20
diff
changeset
|
148 |
// search for any Generic elements and fill in |
20
e119444a1119
Replaced TODO comments with Go-style BUG(me) comments.
Chris Jones <chris@cjones.org>
parents:
19
diff
changeset
|
149 |
// their attributes. |
17 | 150 |
|
10 | 151 |
// Put it on the channel. |
152 |
ch <- obj |
|
153 |
} |
|
154 |
} |
|
155 |
||
156 |
func writeXml(w io.Writer, ch <-chan interface{}) { |
|
157 |
if debug { |
|
158 |
pr, pw := io.Pipe() |
|
159 |
go tee(pr, w, "C: ") |
|
160 |
w = pw |
|
161 |
} |
|
162 |
defer tryClose(w, ch) |
|
163 |
||
164 |
for obj := range ch { |
|
165 |
err := xml.Marshal(w, obj) |
|
166 |
if err != nil { |
|
167 |
log.Printf("write: %v", err) |
|
168 |
break |
|
169 |
} |
|
170 |
} |
|
171 |
} |
|
172 |
||
20
e119444a1119
Replaced TODO comments with Go-style BUG(me) comments.
Chris Jones <chris@cjones.org>
parents:
19
diff
changeset
|
173 |
// BUG(cjyar) This should go away. We shouldn't allow writing of |
17 | 174 |
// unstructured data. |
10 | 175 |
func writeText(w io.Writer, ch <-chan *string) { |
176 |
if debug { |
|
177 |
pr, pw := io.Pipe() |
|
178 |
go tee(pr, w, "C: ") |
|
179 |
w = pw |
|
180 |
} |
|
181 |
defer tryClose(w, ch) |
|
182 |
||
183 |
for str := range ch { |
|
184 |
_, err := w.Write([]byte(*str)) |
|
185 |
if err != nil { |
|
186 |
log.Printf("writeStr: %v", err) |
|
187 |
break |
|
188 |
} |
|
189 |
} |
|
190 |
} |
|
191 |
||
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
192 |
func (cl *Client) readStream(srvIn <-chan interface{}, cliOut chan<- interface{}) { |
10 | 193 |
defer tryClose(srvIn, cliOut) |
194 |
||
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
195 |
handlers := make(map[string] func(Stanza) bool) |
20
e119444a1119
Replaced TODO comments with Go-style BUG(me) comments.
Chris Jones <chris@cjones.org>
parents:
19
diff
changeset
|
196 |
// BUG(cjyar) This for loop will never terminate, even when |
e119444a1119
Replaced TODO comments with Go-style BUG(me) comments.
Chris Jones <chris@cjones.org>
parents:
19
diff
changeset
|
197 |
// the channels are closed. |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
198 |
for { |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
199 |
select { |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
200 |
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
|
201 |
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
|
202 |
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
|
203 |
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
|
204 |
switch obj := x.(type) { |
22
d6b7b4cbf50d
Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents:
21
diff
changeset
|
205 |
case *stream: |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
206 |
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
|
207 |
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
|
208 |
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
|
209 |
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
|
210 |
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
|
211 |
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
|
212 |
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
|
213 |
default: |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
214 |
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
|
215 |
} |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
216 |
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
|
217 |
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
|
218 |
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
|
219 |
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
|
220 |
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
|
221 |
} |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
222 |
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
|
223 |
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
|
224 |
} |
10 | 225 |
} |
226 |
} |
|
227 |
} |
|
228 |
||
20
e119444a1119
Replaced TODO comments with Go-style BUG(me) comments.
Chris Jones <chris@cjones.org>
parents:
19
diff
changeset
|
229 |
// BUG(cjyar) Disable this loop until resource binding is |
17 | 230 |
// complete. Otherwise the app might inject something weird into our |
231 |
// negotiation stream. |
|
10 | 232 |
func writeStream(srvOut chan<- interface{}, cliIn <-chan interface{}) { |
233 |
defer tryClose(srvOut, cliIn) |
|
234 |
||
235 |
for x := range cliIn { |
|
236 |
srvOut <- x |
|
237 |
} |
|
238 |
} |
|
239 |
||
22
d6b7b4cbf50d
Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents:
21
diff
changeset
|
240 |
func handleStream(ss *stream) { |
10 | 241 |
} |
242 |
||
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
243 |
func (cl *Client) handleFeatures(fe *Features) { |
10 | 244 |
if fe.Starttls != nil { |
245 |
start := &starttls{XMLName: xml.Name{Space: nsTLS, |
|
246 |
Local: "starttls"}} |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
247 |
cl.xmlOut <- start |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
248 |
return |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
249 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
250 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
251 |
if len(fe.Mechanisms.Mechanism) > 0 { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
252 |
cl.chooseSasl(fe) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
253 |
return |
10 | 254 |
} |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
255 |
|
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
256 |
if fe.Bind != nil { |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
257 |
cl.bind(fe.Bind) |
17 | 258 |
return |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
259 |
} |
10 | 260 |
} |
261 |
||
262 |
// readTransport() is running concurrently. We need to stop it, |
|
263 |
// negotiate TLS, then start it again. It calls waitForSocket() in |
|
264 |
// its inner loop; see below. |
|
265 |
func (cl *Client) handleTls(t *starttls) { |
|
266 |
tcp := cl.socket |
|
267 |
||
268 |
// Set the socket to nil, and wait for the reader routine to |
|
269 |
// signal that it's paused. |
|
270 |
cl.socket = nil |
|
271 |
cl.socketSync.Add(1) |
|
272 |
cl.socketSync.Wait() |
|
273 |
||
274 |
// Negotiate TLS with the server. |
|
275 |
tls := tls.Client(tcp, nil) |
|
276 |
||
277 |
// Make the TLS connection available to the reader, and wait |
|
278 |
// for it to signal that it's working again. |
|
279 |
cl.socketSync.Add(1) |
|
280 |
cl.socket = tls |
|
281 |
cl.socketSync.Wait() |
|
282 |
||
283 |
// Reset the read timeout on the (underlying) socket so the |
|
284 |
// reader doesn't get woken up unnecessarily. |
|
285 |
tcp.SetReadTimeout(0) |
|
286 |
||
287 |
log.Println("TLS negotiation succeeded.") |
|
288 |
||
289 |
// Now re-send the initial handshake message to start the new |
|
290 |
// session. |
|
22
d6b7b4cbf50d
Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents:
21
diff
changeset
|
291 |
hsOut := &stream{To: cl.Jid.Domain, Version: Version} |
10 | 292 |
cl.xmlOut <- hsOut |
293 |
} |
|
294 |
||
295 |
// Synchronize with handleTls(). Called from readTransport() when |
|
296 |
// cl.socket is nil. |
|
297 |
func (cl *Client) waitForSocket() { |
|
298 |
// Signal that we've stopped reading from the socket. |
|
299 |
cl.socketSync.Done() |
|
300 |
||
301 |
// Wait until the socket is available again. |
|
302 |
for cl.socket == nil { |
|
303 |
time.Sleep(1e8) |
|
304 |
} |
|
305 |
||
306 |
// Signal that we're going back to the read loop. |
|
307 |
cl.socketSync.Done() |
|
308 |
} |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
309 |
|
19
e923f28d65aa
Added an example of the use of godoc's BUG comment.
Chris Jones <chris@cjones.org>
parents:
17
diff
changeset
|
310 |
// BUG(cjyar) Doesn't implement TLS/SASL EXTERNAL. |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
311 |
func (cl *Client) chooseSasl(fe *Features) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
312 |
var digestMd5 bool |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
313 |
for _, m := range(fe.Mechanisms.Mechanism) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
314 |
switch strings.ToLower(m) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
315 |
case "digest-md5": |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
316 |
digestMd5 = true |
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 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
319 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
320 |
if digestMd5 { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
321 |
auth := &auth{XMLName: xml.Name{Space: nsSASL, Local: |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
322 |
"auth"}, Mechanism: "DIGEST-MD5"} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
323 |
cl.xmlOut <- auth |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
324 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
325 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
326 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
327 |
func (cl *Client) handleSasl(srv *auth) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
328 |
switch strings.ToLower(srv.XMLName.Local) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
329 |
case "challenge": |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
330 |
b64 := base64.StdEncoding |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
331 |
str, err := b64.DecodeString(srv.Chardata) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
332 |
if err != nil { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
333 |
log.Printf("SASL challenge decode: %s", |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
334 |
err.String()) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
335 |
return; |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
336 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
337 |
srvMap := parseSasl(string(str)) |
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 |
if cl.saslExpected == "" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
340 |
cl.saslDigest1(srvMap) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
341 |
} else { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
342 |
cl.saslDigest2(srvMap) |
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 |
case "failure": |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
345 |
log.Println("SASL authentication failed") |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
346 |
case "success": |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
347 |
log.Println("SASL authentication succeeded") |
22
d6b7b4cbf50d
Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents:
21
diff
changeset
|
348 |
ss := &stream{To: cl.Jid.Domain, Version: Version} |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
349 |
cl.xmlOut <- ss |
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 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
352 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
353 |
func (cl *Client) saslDigest1(srvMap map[string] string) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
354 |
// Make sure it supports qop=auth |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
355 |
var hasAuth bool |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
356 |
for _, qop := range(strings.Fields(srvMap["qop"])) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
357 |
if qop == "auth" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
358 |
hasAuth = true |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
359 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
360 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
361 |
if !hasAuth { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
362 |
log.Println("Server doesn't support SASL auth") |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
363 |
return; |
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 |
// Pick a realm. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
367 |
var realm string |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
368 |
if srvMap["realm"] != "" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
369 |
realm = strings.Fields(srvMap["realm"])[0] |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
370 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
371 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
372 |
passwd := cl.password |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
373 |
nonce := srvMap["nonce"] |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
374 |
digestUri := "xmpp/" + cl.Jid.Domain |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
375 |
nonceCount := int32(1) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
376 |
nonceCountStr := fmt.Sprintf("%08x", nonceCount) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
377 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
378 |
// Begin building the response. Username is |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
379 |
// user@domain or just domain. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
380 |
var username string |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
381 |
if cl.Jid.Node == nil { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
382 |
username = cl.Jid.Domain |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
383 |
} else { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
384 |
username = *cl.Jid.Node |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
385 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
386 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
387 |
// Generate our own nonce from random data. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
388 |
randSize := big.NewInt(0) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
389 |
randSize.Lsh(big.NewInt(1), 64) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
390 |
cnonce, err := rand.Int(rand.Reader, randSize) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
391 |
if err != nil { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
392 |
log.Println("SASL rand: %s", err.String()) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
393 |
return |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
394 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
395 |
cnonceStr := fmt.Sprintf("%016x", cnonce) |
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 |
/* Now encode the actual password response, as well as the |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
398 |
* expected next challenge from the server. */ |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
399 |
response := saslDigestResponse(username, realm, passwd, nonce, |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
400 |
cnonceStr, "AUTHENTICATE", digestUri, nonceCountStr) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
401 |
next := saslDigestResponse(username, realm, passwd, nonce, |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
402 |
cnonceStr, "", digestUri, nonceCountStr) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
403 |
cl.saslExpected = next |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
404 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
405 |
// Build the map which will be encoded. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
406 |
clMap := make(map[string]string) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
407 |
clMap["realm"] = `"` + realm + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
408 |
clMap["username"] = `"` + username + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
409 |
clMap["nonce"] = `"` + nonce + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
410 |
clMap["cnonce"] = `"` + cnonceStr + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
411 |
clMap["nc"] = nonceCountStr |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
412 |
clMap["qop"] = "auth" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
413 |
clMap["digest-uri"] = `"` + digestUri + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
414 |
clMap["response"] = response |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
415 |
if srvMap["charset"] == "utf-8" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
416 |
clMap["charset"] = "utf-8" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
417 |
} |
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 |
// Encode the map and send it. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
420 |
clStr := packSasl(clMap) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
421 |
b64 := base64.StdEncoding |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
422 |
clObj := &auth{XMLName: xml.Name{Space: nsSASL, Local: |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
423 |
"response"}, Chardata: |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
424 |
b64.EncodeToString([]byte(clStr))} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
425 |
cl.xmlOut <- clObj |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
426 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
427 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
428 |
func (cl *Client) saslDigest2(srvMap map[string] string) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
429 |
if cl.saslExpected == srvMap["rspauth"] { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
430 |
clObj := &auth{XMLName: xml.Name{Space: nsSASL, Local: |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
431 |
"response"}} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
432 |
cl.xmlOut <- clObj |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
433 |
} else { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
434 |
clObj := &auth{XMLName: xml.Name{Space: nsSASL, Local: |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
435 |
"failure"}, Any: |
21
8f6ae5cfc9b9
Renamed Unrecognized to Generic.
Chris Jones <chris@cjones.org>
parents:
20
diff
changeset
|
436 |
&Generic{XMLName: xml.Name{Space: nsSASL, |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
437 |
Local: "abort"}}} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
438 |
cl.xmlOut <- clObj |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
439 |
} |
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 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
442 |
// 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
|
443 |
// key/value map. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
444 |
func parseSasl(in string) map[string]string { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
445 |
re := regexp.MustCompile(`([^=]+)="?([^",]+)"?,?`) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
446 |
strs := re.FindAllStringSubmatch(in, -1) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
447 |
m := make(map[string]string) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
448 |
for _, pair := range(strs) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
449 |
key := strings.ToLower(string(pair[1])) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
450 |
value := string(pair[2]) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
451 |
m[key] = value |
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 |
return m |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
454 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
455 |
|
17 | 456 |
// Inverse of parseSasl(). |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
457 |
func packSasl(m map[string]string) string { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
458 |
var terms []string |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
459 |
for key, value := range(m) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
460 |
if key == "" || value == "" || value == `""` { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
461 |
continue |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
462 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
463 |
terms = append(terms, key + "=" + value) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
464 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
465 |
return strings.Join(terms, ",") |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
466 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
467 |
|
17 | 468 |
// Computes the response string for digest authentication. |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
469 |
func saslDigestResponse(username, realm, passwd, nonce, cnonceStr, |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
470 |
authenticate, digestUri, nonceCountStr string) string { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
471 |
h := func(text string) []byte { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
472 |
h := md5.New() |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
473 |
h.Write([]byte(text)) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
474 |
return h.Sum() |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
475 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
476 |
hex := func(bytes []byte) string { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
477 |
return fmt.Sprintf("%x", bytes) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
478 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
479 |
kd := func(secret, data string) []byte { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
480 |
return h(secret + ":" + data) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
481 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
482 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
483 |
a1 := string(h(username + ":" + realm + ":" + passwd)) + ":" + |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
484 |
nonce + ":" + cnonceStr |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
485 |
a2 := authenticate + ":" + digestUri |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
486 |
response := hex(kd(hex(h(a1)), nonce + ":" + |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
487 |
nonceCountStr + ":" + cnonceStr + ":auth:" + |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
488 |
hex(h(a2)))) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
489 |
return response |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
490 |
} |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
491 |
|
17 | 492 |
// Send a request to bind a resource. RFC 3920, section 7. |
21
8f6ae5cfc9b9
Renamed Unrecognized to Generic.
Chris Jones <chris@cjones.org>
parents:
20
diff
changeset
|
493 |
func (cl *Client) bind(bind *Generic) { |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
494 |
res := cl.Jid.Resource |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
495 |
msg := &Iq{Type: "set", Id: cl.NextId(), Any: |
21
8f6ae5cfc9b9
Renamed Unrecognized to Generic.
Chris Jones <chris@cjones.org>
parents:
20
diff
changeset
|
496 |
&Generic{XMLName: xml.Name{Space: nsBind, Local: |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
497 |
"bind"}}} |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
498 |
if res != "" { |
21
8f6ae5cfc9b9
Renamed Unrecognized to Generic.
Chris Jones <chris@cjones.org>
parents:
20
diff
changeset
|
499 |
msg.Any.Any = &Generic{XMLName: xml.Name{Local: |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
500 |
"resource"}, Chardata: res} |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
501 |
} |
15
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
502 |
f := func(st Stanza) bool { |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
503 |
if st.XType() == "error" { |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
504 |
log.Println("Resource binding failed") |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
505 |
return false |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
506 |
} |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
507 |
bind := st.XChild() |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
508 |
if bind == nil { |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
509 |
log.Println("nil resource bind") |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
510 |
return false |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
511 |
} |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
512 |
jidEle := bind.Any |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
513 |
if jidEle == nil { |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
514 |
log.Println("nil resource") |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
515 |
return false |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
516 |
} |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
517 |
jidStr := jidEle.Chardata |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
518 |
if jidStr == "" { |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
519 |
log.Println("empty resource") |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
520 |
return false |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
521 |
} |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
522 |
jid := new(JID) |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
523 |
if !jid.Set(jidStr) { |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
524 |
log.Println("Can't parse JID %s", jidStr) |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
525 |
return false |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
526 |
} |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
527 |
cl.Jid = *jid |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
528 |
log.Printf("Bound resource: %s", cl.Jid.String()) |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
529 |
return true |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
530 |
} |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
531 |
cl.HandleStanza(msg.Id, f) |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
532 |
cl.xmlOut <- msg |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
533 |
} |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
534 |
|
17 | 535 |
// Register a callback to handle the next XMPP stanza (iq, message, or |
536 |
// presence) with a given id. The provided function will not be called |
|
537 |
// more than once. If it returns false, the stanza will not be made |
|
538 |
// available on the normal Client.In channel. |
|
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
539 |
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
|
540 |
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
|
541 |
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
|
542 |
} |