Skip to content

Commit

Permalink
* fix: added Config.URL to fix ldap+tls connections
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Mike Carlson <[email protected]>
  • Loading branch information
3 people authored Jan 7, 2022
1 parent da9b88a commit 6801726
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
3 changes: 1 addition & 2 deletions _examples/ldap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
}
Expand Down
3 changes: 1 addition & 2 deletions auth/strategies/ldap/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
}
Expand Down
11 changes: 9 additions & 2 deletions auth/strategies/ldap/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6801726

Please sign in to comment.