From 424970d23c722805151b8ae47182f8b2374c3fe7 Mon Sep 17 00:00:00 2001 From: Martin Date: Wed, 10 Jan 2024 15:49:08 +0100 Subject: [PATCH] Fix manual choice of auth mechanism. (#173) --- xmpp.go | 65 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/xmpp.go b/xmpp.go index 6bf51c4..ac341a3 100644 --- a/xmpp.go +++ b/xmpp.go @@ -406,37 +406,40 @@ func (c *Client) init(o *Options) error { tlsConnOK = true } 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 o.Mechanism != "" { + if slices.Contains(f.Mechanisms.Mechanism, o.Mechanism) { + mechanism = o.Mechanism + } + } else { + switch { + 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") {