diff options
author | xengineering <me@xengineering.eu> | 2023-07-04 22:10:55 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-07-04 22:15:17 +0200 |
commit | 48811e7d2487ebc3db49b8af7e20f57db4ac28f4 (patch) | |
tree | a317f8dc44ac9828ae5806e1fa1dee7547118619 /xmpp/sasl.go | |
parent | 5570ccd1d6b50042acbf2fad3793afa6dde79ca2 (diff) | |
parent | d9fe0a4360770b1e4b6b4fb3686c3275ad1b6e6e (diff) | |
download | limox-48811e7d2487ebc3db49b8af7e20f57db4ac28f4.tar limox-48811e7d2487ebc3db49b8af7e20f57db4ac28f4.tar.zst limox-48811e7d2487ebc3db49b8af7e20f57db4ac28f4.zip |
Merge branch 'element-handling'
This moves away from the concept to parse each individual XML token from
the token and group them as a []xml.Token slice for further processing.
While this is still possible, receiving aswell as sending has switched
to define structs with XML tags which can be marshalled and unmarshalled
with the xml.Encoder.EncodeElement() and xml.Decoder.DecodeElement()
functions.
Further documentation can be found at https://pkg.go.dev/encoding/xml#Unmarshal
and https://pkg.go.dev/encoding/xml#Marshal.
This merge reduces with its changes the number of code lines in the XMPP
implementation by around 41 percent!
Diffstat (limited to 'xmpp/sasl.go')
-rw-r--r-- | xmpp/sasl.go | 64 |
1 files changed, 13 insertions, 51 deletions
diff --git a/xmpp/sasl.go b/xmpp/sasl.go index 512fd58..69b536d 100644 --- a/xmpp/sasl.go +++ b/xmpp/sasl.go @@ -6,69 +6,31 @@ import ( "log" ) -func (s *session) sasl() { - tokens := make([]xml.Token, 0, 3) +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"}, }, } - 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 SASL PLAIN element!") - return - } - } -} - -// 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`} + inner := saslRequest{} + inner.Payload = make([]byte, base64.StdEncoding.EncodedLen(len(data))) + base64.StdEncoding.Encode(inner.Payload, data) - 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 - } - } - } - } + err := s.tx.EncodeElement(inner, start) + if err != nil { + log.Println("Could not encode SASL PLAIN element!") } - - return false } -func saslSuccessHandler(s *session, e []xml.Token) { - runStreamPair(s) -} +type saslSuccess struct{} -func saslFailureHandler(s *session, e []xml.Token) { - log.Println("SASL autentication failed!") +func handleSaslSuccess(s *session) { + openStream(s.tx, s.jid) } |