From 6801726c00a94c079bec8e1be69ad778d92bbac8 Mon Sep 17 00:00:00 2001 From: Mike Carlson Date: Fri, 7 Jan 2022 02:47:38 -0800 Subject: [PATCH] * fix: added Config.URL to fix ldap+tls connections * Adding Config.URL This should help existing use cases where Host and Port are used, but we can now use a LDAP URL/URI: ldap://ldap.example.com:389 * fix: added Config.URL to fix ldap+tls connections Co-authored-by: Mike Carlson Co-authored-by: Mike Carlson --- _examples/ldap/main.go | 3 +-- auth/strategies/ldap/example_test.go | 3 +-- auth/strategies/ldap/ldap.go | 11 +++++++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/_examples/ldap/main.go b/_examples/ldap/main.go index bfa9e37..8cd4140 100644 --- a/_examples/ldap/main.go +++ b/_examples/ldap/main.go @@ -48,8 +48,7 @@ func setupGoGuardian() { cfg := &ldap.Config{ BaseDN: "dc=example,dc=com", BindDN: "cn=read-only-admin,dc=example,dc=com", - Port: "389", - Host: "ldap.forumsys.com", + URL: "ldap://ldap.forumsys.com:389", BindPassword: "password", Filter: "(uid=%s)", } diff --git a/auth/strategies/ldap/example_test.go b/auth/strategies/ldap/example_test.go index 06bfc58..30c0c9e 100644 --- a/auth/strategies/ldap/example_test.go +++ b/auth/strategies/ldap/example_test.go @@ -9,8 +9,7 @@ func Example() { cfg := Config{ BaseDN: "dc=example,dc=org", BindDN: "cn=readonly,dc=example,dc=org", - Port: "389", - Host: "127.0.0.1", + URL: "ldap://127.0.0.1:389", BindPassword: "readonly", Filter: "(cn=%s)", } diff --git a/auth/strategies/ldap/ldap.go b/auth/strategies/ldap/ldap.go index ec9b920..63a89f1 100644 --- a/auth/strategies/ldap/ldap.go +++ b/auth/strategies/ldap/ldap.go @@ -30,9 +30,13 @@ type conn interface { // Config define the configuration to connect to LDAP. type Config struct { // Port LDAP server port. + // Deprecated: Use URL instead. Port string // Host LDAP server host. + // Deprecated: Use URL instead. Host string + // Specify LDAP URL + URL string // TLS configuration, if nil connect without TLS. TLS *tls.Config // BindDN represents LDAP DN for searching for the user DN. @@ -60,8 +64,11 @@ func dial(cfg *Config) (conn, error) { opts = append(opts, ldap.DialWithTLSConfig(cfg.TLS)) } - addr := fmt.Sprintf("%s://%s:%s", scheme, cfg.Host, cfg.Port) - return ldap.DialURL(addr, opts...) + if cfg.URL == "" { + cfg.URL = fmt.Sprintf("%s://%s:%s", scheme, cfg.Host, cfg.Port) + } + + return ldap.DialURL(cfg.URL, opts...) } type client struct {