diff options
author | xengineering <me@xengineering.eu> | 2023-05-21 15:30:39 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-05-21 15:30:39 +0200 |
commit | 50618675c84d2246f48a18de206200d27a733300 (patch) | |
tree | af731af431d5d3059cfe8a9a5806860bf2ca8b12 /xmpp/routing.go | |
parent | 8d4ebab923ae23d1dfbc9b47eeabec33d3605ef5 (diff) | |
download | limox-50618675c84d2246f48a18de206200d27a733300.tar limox-50618675c84d2246f48a18de206200d27a733300.tar.zst limox-50618675c84d2246f48a18de206200d27a733300.zip |
Implement SASL PLAIN authentication
Diffstat (limited to 'xmpp/routing.go')
-rw-r--r-- | xmpp/routing.go | 39 |
1 files changed, 28 insertions, 11 deletions
diff --git a/xmpp/routing.go b/xmpp/routing.go index 55ec135..23f92bb 100644 --- a/xmpp/routing.go +++ b/xmpp/routing.go @@ -2,6 +2,7 @@ package xmpp import ( "encoding/xml" + "encoding/base64" "errors" "log" ) @@ -14,15 +15,14 @@ type tokenRouter struct { enc *encoder } -func newTokenRouter(e *encoder) tokenRouter { +func newTokenRouter() tokenRouter { return tokenRouter{ buffer: make([]xml.Token, 0), level: 0, - enc: e, } } -func (r *tokenRouter) route(t xml.Token) error { +func (r *tokenRouter) route(t xml.Token, c *Conn) error { r.buffer = append(r.buffer, t) switch unwrapped := t.(type) { @@ -43,8 +43,7 @@ func (r *tokenRouter) route(t xml.Token) error { case 0: return errors.New("Stream was closed by server") case 1: - // call elementRouter - err := routeElement(r.buffer) + err := routeElement(r.buffer, c) if err != nil { return err } @@ -64,7 +63,7 @@ func (r *tokenRouter) route(t xml.Token) error { return nil } -func routeElement(b []xml.Token) error { +func routeElement(b []xml.Token, c *Conn) error { tab := elementRoutingTable{ {xml.Name{"http://etherx.jabber.org/streams", "features"}, streamFeatures}, } @@ -73,7 +72,7 @@ func routeElement(b []xml.Token) error { case xml.StartElement: for _, v := range(tab) { if unwrapped.Name == v.name { - err := v.handler(b) + err := v.handler(b, c) if err != nil { return err } @@ -91,11 +90,11 @@ func routeElement(b []xml.Token) error { type elementRoutingTable []struct { name xml.Name - handler func(b []xml.Token) error + handler func(b []xml.Token, c *Conn) error } -func streamFeatures(b []xml.Token) error { - err := sendSaslAuth(b) +func streamFeatures(b []xml.Token, c *Conn) error { + err := sendSaslAuth(b, c) if err != nil { return err } @@ -103,7 +102,7 @@ func streamFeatures(b []xml.Token) error { return nil } -func sendSaslAuth(b []xml.Token) error { +func sendSaslAuth(b []xml.Token, c *Conn) error { mechanisms := make([]string, 0) for i, v := range(b) { switch token := v.(type) { @@ -121,6 +120,24 @@ func sendSaslAuth(b []xml.Token) error { 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 } } |