summaryrefslogtreecommitdiff
path: root/xmpp/sasl.go
blob: 69b536d66a73fe27d34bde23fe30f5f5a1118682 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package xmpp

import (
	"encoding/base64"
	"encoding/xml"
	"log"
)

type saslRequest struct {
	Payload []byte `xml:",chardata"`
}

func (s *session) sasl() {
	start := xml.StartElement{
		xml.Name{"urn:ietf:params:xml:ns:xmpp-sasl", "auth"},
		[]xml.Attr{
			xml.Attr{xml.Name{"", "mechanism"}, "PLAIN"},
		},
	}

	data := []byte("\x00" + username(s.jid) + "\x00" + s.pwd)
	inner := saslRequest{}
	inner.Payload = make([]byte, base64.StdEncoding.EncodedLen(len(data)))
	base64.StdEncoding.Encode(inner.Payload, data)

	err := s.tx.EncodeElement(inner, start)
	if err != nil {
		log.Println("Could not encode SASL PLAIN element!")
	}
}

type saslSuccess struct{}

func handleSaslSuccess(s *session) {
	openStream(s.tx, s.jid)
}