Added support for SASL PLAIN, from "Ildar Hizbulin" <hizel@vyborg.ru>.
authorChris Jones <christian.jones@sri.com>
Thu, 20 Feb 2014 15:55:19 -0700
changeset 184 ce49140fe60b
parent 183 b4bd77d58a3e
child 185 ba8a4ae40e13
Added support for SASL PLAIN, from "Ildar Hizbulin" <hizel@vyborg.ru>.
xmpp/sasl.go
--- a/xmpp/sasl.go	Sun Feb 09 09:52:28 2014 -0700
+++ b/xmpp/sasl.go	Thu Feb 20 15:55:19 2014 -0700
@@ -17,11 +17,15 @@
 // respond.
 // BUG(cjyar): Doesn't implement TLS/SASL EXTERNAL.
 func (cl *Client) chooseSasl(fe *Features) {
-	var digestMd5 bool
+	var digestMd5, plain bool
+	var mechs []string
 	for _, m := range fe.Mechanisms.Mechanism {
+		mechs = append(mechs, m)
 		switch strings.ToLower(m) {
 		case "digest-md5":
 			digestMd5 = true
+		case "plain":
+			plain = true
 		}
 	}
 
@@ -29,6 +33,15 @@
 		auth := &auth{XMLName: xml.Name{Space: NsSASL, Local: "auth"},
 			Mechanism: "DIGEST-MD5"}
 		cl.sendRaw <- auth
+	} else if plain {
+		raw := "\x00" + cl.Jid.Node() + "\x00" + cl.password
+		enc := base64.StdEncoding.EncodeToString([]byte(raw))
+		auth := &auth{XMLName: xml.Name{Space: NsSASL, Local: "auth"},
+			Mechanism: "PLAIN", Chardata: enc}
+		cl.sendRaw <- auth
+	} else {
+		cl.setError(fmt.Errorf("No supported auth mechanism in %v",
+			mechs))
 	}
 }