From 667cf731fd2c6ff8e7142d0f5e1b74c675a7df45 Mon Sep 17 00:00:00 2001 From: BetaPictoris Date: Fri, 27 Oct 2023 15:48:41 -0700 Subject: [PATCH] Extract out LDAP connection creation --- go.mod | 3 +-- server/ctrlsubsonic/middleware.go | 40 ++++++++++++++++--------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/go.mod b/go.mod index 865d805..f511dba 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,6 @@ require ( github.com/gorilla/mux v1.8.0 github.com/gorilla/securecookie v1.1.1 github.com/gorilla/sessions v1.2.1 - jaytaylor.com/html2text v0.0.0-20230321000545-74c2419ad056 github.com/jinzhu/gorm v1.9.17-0.20211120011537-5c235b72a414 github.com/josephburnett/jd v1.5.2 github.com/mattn/go-sqlite3 v1.14.16 @@ -34,6 +33,7 @@ require ( golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 golang.org/x/net v0.7.0 gopkg.in/gormigrate.v1 v1.6.0 + jaytaylor.com/html2text v0.0.0-20230321000545-74c2419ad056 ) require ( @@ -71,5 +71,4 @@ require ( gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect - jaytaylor.com/html2text v0.0.0-20230321000545-74c2419ad056 // indirect ) diff --git a/server/ctrlsubsonic/middleware.go b/server/ctrlsubsonic/middleware.go index b1eb12e..fa0ed71 100644 --- a/server/ctrlsubsonic/middleware.go +++ b/server/ctrlsubsonic/middleware.go @@ -91,18 +91,10 @@ func (c *Controller) WithUser(next http.Handler) http.Handler { filter, _ := c.DB.GetSetting("ldap_filter") tls, _ := c.DB.GetSetting("ldap_tls") - protocol := "ldap" - if tls == "true" { - protocol = "ldaps" - } - // Now, we can try to connect to the LDAP server. - l, err := ldap.DialURL(fmt.Sprintf("%s://%s:%s", protocol, ldapFQDN, ldapPort)) + l, err := createLDAPconnection(tls, ldapFQDN, ldapPort) if err != nil { - - // Warn the server and return a generic error. - log.Println("Failed to connect to LDAP server", err) - + // Return a generic error. _ = writeResp(w, r, spec.NewError(0, "Failed to connect to LDAP server.")) return } @@ -171,17 +163,10 @@ func (c *Controller) WithUser(next http.Handler) http.Handler { baseDN, _ := c.DB.GetSetting("ldap_base_dn") tls, _ := c.DB.GetSetting("ldap_tls") - protocol := "ldap" - if tls == "true" { - protocol = "ldaps" - } - // Now, we can try to connect to the LDAP server. - l, err := ldap.DialURL(fmt.Sprintf("%s://%s:%s", protocol, ldapFQDN, ldapPort)) + l, err := createLDAPconnection(tls, ldapFQDN, ldapPort) if err != nil { - // Warn the server and return a generic error. - log.Println("Failed to connect to LDAP server", err) - + // Return a generic error. _ = writeResp(w, r, spec.NewError(0, "Failed to connect to LDAP server.")) return } @@ -207,3 +192,20 @@ func (c *Controller) WithUser(next http.Handler) http.Handler { next.ServeHTTP(w, r.WithContext(withUser)) }) } + +func createLDAPconnection(tls string, fqdn string, port string) (*ldap.Conn, error) { + protocol := "ldap" + if tls == "true" { + protocol = "ldaps" + } + + // Now, we can try to connect to the LDAP server. + l, err := ldap.DialURL(fmt.Sprintf("%s://%s:%s", protocol, fqdn, port)) + if err != nil { + // Warn the server and return a generic error. + log.Println("Failed to connect to LDAP server", err) + return nil, err + } + + return l, nil +} \ No newline at end of file