summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2023-06-27 16:43:45 +0200
committerxengineering <me@xengineering.eu>2023-06-27 16:45:07 +0200
commitc600e10faf59e96be6b0ad440bd4c5d8cc13d38a (patch)
treeca584829183369178cf67c27dc7f5c1c1b0e86d2
parent35a9c27783524a035ab6545061f5ecd5fdee6ea7 (diff)
downloadlimox-c600e10faf59e96be6b0ad440bd4c5d8cc13d38a.tar
limox-c600e10faf59e96be6b0ad440bd4c5d8cc13d38a.tar.zst
limox-c600e10faf59e96be6b0ad440bd4c5d8cc13d38a.zip
xmpp: Re-introduce SASL authentication
-rw-r--r--xmpp/sasl.go36
-rw-r--r--xmpp/session.go4
2 files changed, 40 insertions, 0 deletions
diff --git a/xmpp/sasl.go b/xmpp/sasl.go
new file mode 100644
index 0000000..1cb3670
--- /dev/null
+++ b/xmpp/sasl.go
@@ -0,0 +1,36 @@
+package xmpp
+
+import (
+ "encoding/xml"
+ "encoding/base64"
+ "log"
+)
+
+func (s *session) sasl() {
+ tokens := make([]xml.Token, 0, 3)
+
+ start := xml.StartElement{
+ xml.Name{"urn:ietf:params:xml:ns:xmpp-sasl", "auth"},
+ []xml.Attr{
+ xml.Attr{xml.Name{"", "mechanism"}, "PLAIN"},
+ },
+ }
+ tokens = append(tokens, start)
+
+ data := []byte("\x00" + username(s.jid) + "\x00" + s.pwd)
+ dst := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
+ base64.StdEncoding.Encode(dst, data)
+ payload := xml.CharData(dst)
+ tokens = append(tokens, payload)
+
+ end := start.End()
+ tokens = append(tokens, end)
+
+ for _, t := range tokens {
+ err := s.ed.encodeToken(t)
+ if err != nil {
+ log.Println("Could not encode stream end!")
+ return
+ }
+ }
+}
diff --git a/xmpp/session.go b/xmpp/session.go
index 88a01ae..e0819da 100644
--- a/xmpp/session.go
+++ b/xmpp/session.go
@@ -13,6 +13,7 @@ type SessionShouldDisconnect struct{}
type session struct {
jid string
+ pwd string
in chan any
out chan<- any
transport *tls.Conn
@@ -25,6 +26,7 @@ func StartSession(out chan<- any, jid string, pwd string) (in chan<- any) {
s := session{}
s.jid = jid
+ s.pwd = pwd
s.in = make(chan any)
s.out = out
s.rx = make(chan xml.Token, 0)
@@ -50,6 +52,8 @@ func (s *session) run() {
s.openStream()
defer s.closeStreams()
+ s.sasl()
+
s.out <- SessionConnect{}
for {