From 5fdcf18a81bc0dcaa164a3732de8377dfd78583a Mon Sep 17 00:00:00 2001 From: Martin Date: Wed, 10 Jan 2024 14:53:36 +0100 Subject: [PATCH] Simplify authentication choice code. (#169) * Simplify authentication choice code. Should be a lot easier to read and understand now. --- xmpp.go | 89 ++++++++++++++++++++------------------------------------- 1 file changed, 31 insertions(+), 58 deletions(-) diff --git a/xmpp.go b/xmpp.go index 042c278..6bf51c4 100644 --- a/xmpp.go +++ b/xmpp.go @@ -406,64 +406,37 @@ func (c *Client) init(o *Options) error { tlsConnOK = true } mechanism = "" - if o.Mechanism == "" { - for _, m := range f.Mechanisms.Mechanism { - switch m { - case "SCRAM-SHA-512-PLUS": - if tlsConnOK { - mechanism = m - } - case "SCRAM-SHA-256-PLUS": - if mechanism != "SCRAM-SHA-512-PLUS" && tlsConnOK { - mechanism = m - } - case "SCRAM-SHA-1-PLUS": - if mechanism != "SCRAM-SHA-512-PLUS" && - mechanism != "SCRAM-SHA-256-PLUS" && - tlsConnOK { - mechanism = m - } - case "SCRAM-SHA-512": - if mechanism != "SCRAM-SHA-512-PLUS" && - mechanism != "SCRAM-SHA-256-PLUS" && - mechanism != "SCRAM-SHA-1-PLUS" { - mechanism = m - } - case "SCRAM-SHA-256": - if mechanism != "SCRAM-SHA-512-PLUS" && - mechanism != "SCRAM-SHA-256-PLUS" && - mechanism != "SCRAM-SHA-1-PLUS" && - mechanism != "SCRAM-SHA-512" { - mechanism = m - } - case "SCRAM-SHA-1": - if mechanism != "SCRAM-SHA-512-PLUS" && - mechanism != "SCRAM-SHA-256-PLUS" && - mechanism != "SCRAM-SHA-1-PLUS" && - mechanism != "SCRAM-SHA-512" && - mechanism != "SCRAM-SHA-256" { - mechanism = m - } - case "X-OAUTH2": - if mechanism == "" { - mechanism = m - } - case "PLAIN": - if mechanism == "" { - mechanism = m - } - case "DIGEST-MD5": - if mechanism == "" { - mechanism = m - } - } - } - } else { - for _, m := range f.Mechanisms.Mechanism { - if m == o.Mechanism { - mechanism = o.Mechanism - } - } + switch { + case slices.Contains(f.Mechanisms.Mechanism, + o.Mechanism) && o.Mechanism != "": + mechanism = o.Mechanism + case slices.Contains(f.Mechanisms.Mechanism, + "SCRAM-SHA-512-PLUS") && tlsConnOK: + mechanism = "SCRAM-SHA-512-PLUS" + case slices.Contains(f.Mechanisms.Mechanism, + "SCRAM-SHA-256-PLUS") && tlsConnOK: + mechanism = "SCRAM-SHA-256-PLUS" + case slices.Contains(f.Mechanisms.Mechanism, + "SCRAM-SHA-1-PLUS") && tlsConnOK: + mechanism = "SCRAM-SHA-1-PLUS" + case slices.Contains(f.Mechanisms.Mechanism, + "SCRAM-SHA-512"): + mechanism = "SCRAM-SHA-512" + case slices.Contains(f.Mechanisms.Mechanism, + "SCRAM-SHA-256"): + mechanism = "SCRAM-SHA-256" + case slices.Contains(f.Mechanisms.Mechanism, + "SCRAM-SHA-1"): + mechanism = "SCRAM-SHA-1" + case slices.Contains(f.Mechanisms.Mechanism, + "X-OAUTH2"): + mechanism = "X-OAUTH2" + case slices.Contains(f.Mechanisms.Mechanism, + "PLAIN") && tlsConnOK: + mechanism = "PLAIN" + case slices.Contains(f.Mechanisms.Mechanism, + "DIGEST-MD5"): + mechanism = "DIGEST-MD5" } if strings.HasPrefix(mechanism, "SCRAM-SHA") { if strings.HasSuffix(mechanism, "PLUS") {