Extract out LDAP connection creation

This commit is contained in:
BetaPictoris
2023-10-27 15:48:41 -07:00
parent 38ea72bbc6
commit 667cf731fd
2 changed files with 22 additions and 21 deletions

No files matched your search

+1 -2
View File
@@ -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
)
+21 -19
View File
@@ -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
}