Simplify authentication choice code. (#169)

* Simplify authentication choice code.

Should be a lot easier to read and understand now.
This commit is contained in:
Martin 2024-01-10 14:53:36 +01:00 committed by GitHub
parent 794ed98f9f
commit 5fdcf18a81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

89
xmpp.go
View File

@ -406,64 +406,37 @@ func (c *Client) init(o *Options) error {
tlsConnOK = true tlsConnOK = true
} }
mechanism = "" mechanism = ""
if o.Mechanism == "" { switch {
for _, m := range f.Mechanisms.Mechanism { case slices.Contains(f.Mechanisms.Mechanism,
switch m { o.Mechanism) && o.Mechanism != "":
case "SCRAM-SHA-512-PLUS": mechanism = o.Mechanism
if tlsConnOK { case slices.Contains(f.Mechanisms.Mechanism,
mechanism = m "SCRAM-SHA-512-PLUS") && tlsConnOK:
} mechanism = "SCRAM-SHA-512-PLUS"
case "SCRAM-SHA-256-PLUS": case slices.Contains(f.Mechanisms.Mechanism,
if mechanism != "SCRAM-SHA-512-PLUS" && tlsConnOK { "SCRAM-SHA-256-PLUS") && tlsConnOK:
mechanism = m mechanism = "SCRAM-SHA-256-PLUS"
} case slices.Contains(f.Mechanisms.Mechanism,
case "SCRAM-SHA-1-PLUS": "SCRAM-SHA-1-PLUS") && tlsConnOK:
if mechanism != "SCRAM-SHA-512-PLUS" && mechanism = "SCRAM-SHA-1-PLUS"
mechanism != "SCRAM-SHA-256-PLUS" && case slices.Contains(f.Mechanisms.Mechanism,
tlsConnOK { "SCRAM-SHA-512"):
mechanism = m mechanism = "SCRAM-SHA-512"
} case slices.Contains(f.Mechanisms.Mechanism,
case "SCRAM-SHA-512": "SCRAM-SHA-256"):
if mechanism != "SCRAM-SHA-512-PLUS" && mechanism = "SCRAM-SHA-256"
mechanism != "SCRAM-SHA-256-PLUS" && case slices.Contains(f.Mechanisms.Mechanism,
mechanism != "SCRAM-SHA-1-PLUS" { "SCRAM-SHA-1"):
mechanism = m mechanism = "SCRAM-SHA-1"
} case slices.Contains(f.Mechanisms.Mechanism,
case "SCRAM-SHA-256": "X-OAUTH2"):
if mechanism != "SCRAM-SHA-512-PLUS" && mechanism = "X-OAUTH2"
mechanism != "SCRAM-SHA-256-PLUS" && case slices.Contains(f.Mechanisms.Mechanism,
mechanism != "SCRAM-SHA-1-PLUS" && "PLAIN") && tlsConnOK:
mechanism != "SCRAM-SHA-512" { mechanism = "PLAIN"
mechanism = m case slices.Contains(f.Mechanisms.Mechanism,
} "DIGEST-MD5"):
case "SCRAM-SHA-1": mechanism = "DIGEST-MD5"
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
}
}
} }
if strings.HasPrefix(mechanism, "SCRAM-SHA") { if strings.HasPrefix(mechanism, "SCRAM-SHA") {
if strings.HasSuffix(mechanism, "PLUS") { if strings.HasSuffix(mechanism, "PLUS") {