diff options
author | xengineering <me@xengineering.eu> | 2023-06-30 12:44:58 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-06-30 12:44:58 +0200 |
commit | 98740fdc1c777778054dc3e444b9f4542fe04abe (patch) | |
tree | 97008b01a03d131866ffc71d251328709d2827fe | |
parent | d6fa15b7b66d679b7a6291aa6c85780a09cb1755 (diff) | |
download | limox-98740fdc1c777778054dc3e444b9f4542fe04abe.tar limox-98740fdc1c777778054dc3e444b9f4542fe04abe.tar.zst limox-98740fdc1c777778054dc3e444b9f4542fe04abe.zip |
Move SASL mechanism detection to xmpp/sasl.go
-rw-r--r-- | xmpp/sasl.go | 30 | ||||
-rw-r--r-- | xmpp/stream_pair.go | 30 |
2 files changed, 35 insertions, 25 deletions
diff --git a/xmpp/sasl.go b/xmpp/sasl.go index 8687782..e06bcd5 100644 --- a/xmpp/sasl.go +++ b/xmpp/sasl.go @@ -34,3 +34,33 @@ func (s *session) sasl() { } } } + +// hasSaslPlain scans the given stream features XML element for the SASL PLAIN +// mechanism which is supported by xengineering.eu/limox/xmpp. It returns true +// if the stream has support for this mechanism and false otherwise. +func hasSaslPlain(e []xml.Token) bool { + mechanism := xml.Name{`urn:ietf:params:xml:ns:xmpp-sasl`, `mechanism`} + + for i, t := range e { + switch s := t.(type) { + case xml.StartElement: + if s.Name == mechanism { + if i+1 < len(e) { + subtype := func() string { + switch c := e[i+1].(type) { + case xml.CharData: + return string(c) + default: + return "" + } + }() + if subtype == `PLAIN` { + return true + } + } + } + } + } + + return false +} diff --git a/xmpp/stream_pair.go b/xmpp/stream_pair.go index b353947..8f70318 100644 --- a/xmpp/stream_pair.go +++ b/xmpp/stream_pair.go @@ -91,31 +91,11 @@ func closeStream(s *session) { } } -func getCharData(t xml.Token) string { - switch c := t.(type) { - case xml.CharData: - return string(c) - default: - return "" - } -} - func streamFeaturesHandler(e []xml.Token) { - mechanism := xml.Name{`urn:ietf:params:xml:ns:xmpp-sasl`, `mechanism`} - - for i, t := range e { - switch s := t.(type) { - case xml.StartElement: - if s.Name == mechanism { - if i+1 < len(e) { - if getCharData(e[i+1]) == `PLAIN` { - log.Println("Got offer for SASL PLAIN") - return - } - } - } - } + sasl := hasSaslPlain(e) + if sasl { + log.Println("Stream is compatible with SASL PLAIN mechanism") + } else { + log.Println("Stream is not compatible with SASL PLAIN mechanism!") } - - log.Println("No compatible SASL mechanism offered!") } |