Skip to content

Commit

Permalink
refactor: move connection config into own struct
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDevMinerTV committed Oct 4, 2023
1 parent 019ad6a commit 8622caf
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (l *LDAP) ChangePasswordForSAMAccountName(sAMAccountName, oldPassword, newP
return err
}

if l.isActiveDirectory && !strings.HasPrefix(l.server, "ldaps://") {
if l.config.IsActiveDirectory && !strings.HasPrefix(l.config.Server, "ldaps://") {
return ErrActiveDirectoryMustBeLDAPS
}

Expand Down
8 changes: 7 additions & 1 deletion auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,11 @@ func getWorkingLdap() (*LDAP, error) {
return nil, errors.New("LDAP_READ_PASSWORD not set")
}

return New(server, baseDN, readUser, readPassword, false)
config := Config{
Server: server,
BaseDN: baseDN,
IsActiveDirectory: true,
}

return New(config, readUser, readPassword)
}
21 changes: 11 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,27 @@ import (
"github.com/go-ldap/ldap/v3"
)

type LDAP struct {
server string
type Config struct {
Server string
BaseDN string

baseDN string
IsActiveDirectory bool
}

type LDAP struct {
config Config

user string
password string

isActiveDirectory bool
}

var ErrDNDuplicated = errors.New("DN is not unique")

func New(server, baseDN, user, password string, isActiveDirectory bool) (*LDAP, error) {
func New(config Config, user, password string) (*LDAP, error) {
l := &LDAP{
server,
baseDN,
config,
user,
password,
isActiveDirectory,
}

c, err := l.getConnection()
Expand All @@ -38,7 +39,7 @@ func New(server, baseDN, user, password string, isActiveDirectory bool) (*LDAP,
}

func (l LDAP) getConnection() (*ldap.Conn, error) {
c, err := ldap.DialURL(l.server)
c, err := ldap.DialURL(l.config.Server)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions computers.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (l *LDAP) FindComputerBySAMAccountName(sAMAccountName string) (computer *Co
defer c.Close()

r, err := c.Search(&ldap.SearchRequest{
BaseDN: l.baseDN,
BaseDN: l.config.BaseDN,
Scope: ldap.ScopeWholeSubtree,
DerefAliases: ldap.NeverDerefAliases,
Filter: fmt.Sprintf("(&(objectClass=computer)(sAMAccountName=%s))", ldap.EscapeFilter(sAMAccountName)),
Expand Down Expand Up @@ -113,7 +113,7 @@ func (l *LDAP) FindComputers() (computers []Computer, err error) {
defer c.Close()

r, err := c.Search(&ldap.SearchRequest{
BaseDN: l.baseDN,
BaseDN: l.config.BaseDN,
Scope: ldap.ScopeWholeSubtree,
DerefAliases: ldap.NeverDerefAliases,
Filter: "(objectClass=computer)",
Expand Down
2 changes: 1 addition & 1 deletion groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (l *LDAP) FindGroups() (groups []Group, err error) {
defer c.Close()

r, err := c.Search(&ldap.SearchRequest{
BaseDN: l.baseDN,
BaseDN: l.config.BaseDN,
Scope: ldap.ScopeWholeSubtree,
DerefAliases: ldap.NeverDerefAliases,
Filter: "(objectClass=group)",
Expand Down
4 changes: 2 additions & 2 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (l *LDAP) FindUserBySAMAccountName(sAMAccountName string) (user *User, err
defer c.Close()

r, err := c.Search(&ldap.SearchRequest{
BaseDN: l.baseDN,
BaseDN: l.config.BaseDN,
Scope: ldap.ScopeWholeSubtree,
DerefAliases: ldap.NeverDerefAliases,
Filter: fmt.Sprintf("(&(objectClass=user)(sAMAccountName=%s))", ldap.EscapeFilter(sAMAccountName)),
Expand Down Expand Up @@ -110,7 +110,7 @@ func (l *LDAP) FindUsers() (users []User, err error) {
defer c.Close()

r, err := c.Search(&ldap.SearchRequest{
BaseDN: l.baseDN,
BaseDN: l.config.BaseDN,
Scope: ldap.ScopeWholeSubtree,
DerefAliases: ldap.NeverDerefAliases,
Filter: "(objectClass=user)",
Expand Down

0 comments on commit 8622caf

Please sign in to comment.