summaryrefslogtreecommitdiff
path: root/xmpp/sasl.go
blob: 91cbf6b4891c816a3cc613ecccce44cfac8e233d (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package xmpp

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

func sendSaslAuth(b []xml.Token, c *Conn) error {
	mechanisms := make([]string, 0)
	for i, v := range(b) {
		switch token := v.(type) {
		case xml.StartElement:
			expected := xml.Name{"urn:ietf:params:xml:ns:xmpp-sasl", "mechanism"}
			if token.Name == expected {
				if i >= (len(b)-1) { continue }
				switch payload := b[i+1].(type) {
				case xml.CharData:
					mechanisms = append(mechanisms, string(payload))
				}
			}
		}
	}

	for _, v := range(mechanisms) {
		if v == "PLAIN" {
			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(c.jid) + "\x00" + c.pwd)
			dst := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
			base64.StdEncoding.Encode(dst, data)
			payload := xml.CharData(dst)

			end := start.End()

			c.enc.encodeNow(start)
			c.enc.encode(payload)
			c.enc.encodeNow(end)

			return nil
		}
	}

	return errors.New("No compatible SASL mechanism given")
}

func onSaslSuccess(b []xml.Token, c *Conn) error {
	sendStreamStart(&c.enc, c.jid)
	return nil
}

func onSaslFailure(b []xml.Token, c *Conn) error {
	return errors.New("Authentication failed")
}