@@ -2,6 +2,7 @@ package auth
22
33import (
44 "fmt"
5+ "log"
56
67 ldapv3 "github.com/go-ldap/ldap/v3"
78)
@@ -38,51 +39,71 @@ type LDAPService struct {
3839
3940// NewLDAPService creates a new LDAP authentication service
4041func NewLDAPService () (* LDAPService , error ) {
42+ log .Println ("[DEBUG] NewLDAPService: Starting LDAP service initialization" )
43+
4144 config , err := LoadConfig ()
4245 if err != nil {
46+ log .Printf ("[ERROR] NewLDAPService: Failed to load LDAP configuration: %v" , err )
4347 return nil , fmt .Errorf ("failed to load LDAP configuration: %w" , err )
4448 }
49+ log .Printf ("[DEBUG] NewLDAPService: LDAP configuration loaded successfully - URL: %s, BindUser: %s" , config .URL , config .BindUser )
4550
4651 client := NewClient (config )
4752 if err := client .Connect (); err != nil {
53+ log .Printf ("[ERROR] NewLDAPService: Failed to connect to LDAP: %v" , err )
4854 return nil , fmt .Errorf ("failed to connect to LDAP: %w" , err )
4955 }
56+ log .Println ("[DEBUG] NewLDAPService: LDAP client connected successfully" )
5057
58+ log .Println ("[INFO] NewLDAPService: LDAP service initialized successfully" )
5159 return & LDAPService {
5260 client : client ,
5361 }, nil
5462}
5563
5664// Authenticate performs user authentication against LDAP
5765func (s * LDAPService ) Authenticate (username , password string ) (bool , error ) {
66+ log .Printf ("[DEBUG] Authenticate: Starting authentication for user: %s" , username )
67+
5868 userDN , err := s .GetUserDN (username )
5969 if err != nil {
70+ log .Printf ("[ERROR] Authenticate: Failed to get user DN for %s: %v" , username , err )
6071 return false , fmt .Errorf ("failed to get user DN: %v" , err )
6172 }
73+ log .Printf ("[DEBUG] Authenticate: Retrieved user DN for %s: %s" , username , userDN )
6274
6375 // Bind as user to verify password
76+ log .Printf ("[DEBUG] Authenticate: Attempting to bind as user: %s" , username )
6477 err = s .client .Bind (userDN , password )
6578 if err != nil {
79+ log .Printf ("[WARN] Authenticate: Authentication failed for user %s: %v" , username , err )
6680 return false , nil // Invalid credentials, not an error
6781 }
82+ log .Printf ("[DEBUG] Authenticate: User bind successful for: %s" , username )
6883
6984 // Rebind as service account for further operations
7085 config := s .client .Config ()
7186 if config .BindUser != "" {
87+ log .Printf ("[DEBUG] Authenticate: Rebinding as service account: %s" , config .BindUser )
7288 err = s .client .Bind (config .BindUser , config .BindPassword )
7389 if err != nil {
90+ log .Printf ("[ERROR] Authenticate: Failed to rebind as service account: %v" , err )
7491 return false , fmt .Errorf ("failed to rebind as service account: %v" , err )
7592 }
93+ log .Println ("[DEBUG] Authenticate: Service account rebind successful" )
7694 }
7795
96+ log .Printf ("[INFO] Authenticate: Authentication successful for user: %s" , username )
7897 return true , nil
7998}
8099
81100// IsAdmin checks if a user is a member of the admin group
82101func (s * LDAPService ) IsAdmin (username string ) (bool , error ) {
102+ log .Printf ("[DEBUG] IsAdmin: Checking admin status for user: %s" , username )
83103 config := s .client .Config ()
84104
85105 // Search for admin group
106+ log .Printf ("[DEBUG] IsAdmin: Searching for admin group: %s" , config .AdminGroupDN )
86107 adminGroupReq := ldapv3 .NewSearchRequest (
87108 config .AdminGroupDN ,
88109 ldapv3 .ScopeWholeSubtree , ldapv3 .NeverDerefAliases , 0 , 0 , false ,
@@ -92,6 +113,7 @@ func (s *LDAPService) IsAdmin(username string) (bool, error) {
92113 )
93114
94115 // Search for user DN
116+ log .Printf ("[DEBUG] IsAdmin: Searching for user DN for: %s" , username )
95117 userDNReq := ldapv3 .NewSearchRequest (
96118 config .BaseDN ,
97119 ldapv3 .ScopeWholeSubtree , ldapv3 .NeverDerefAliases , 0 , 0 , false ,
@@ -102,53 +124,93 @@ func (s *LDAPService) IsAdmin(username string) (bool, error) {
102124
103125 adminGroupEntry , err := s .client .SearchEntry (adminGroupReq )
104126 if err != nil {
127+ log .Printf ("[ERROR] IsAdmin: Failed to search admin group: %v" , err )
105128 return false , fmt .Errorf ("failed to search admin group: %v" , err )
106129 }
107130
108131 userEntry , err := s .client .SearchEntry (userDNReq )
109132 if err != nil {
133+ log .Printf ("[ERROR] IsAdmin: Failed to search user %s: %v" , username , err )
110134 return false , fmt .Errorf ("failed to search user: %v" , err )
111135 }
112136
113137 if adminGroupEntry == nil {
138+ log .Printf ("[ERROR] IsAdmin: Admin group not found: %s" , config .AdminGroupDN )
114139 return false , fmt .Errorf ("admin group not found" )
115140 }
116141
117142 if userEntry == nil {
143+ log .Printf ("[ERROR] IsAdmin: User not found: %s" , username )
118144 return false , fmt .Errorf ("user not found" )
119145 }
120146
147+ log .Printf ("[DEBUG] IsAdmin: User DN found: %s" , userEntry .DN )
148+ adminMembers := adminGroupEntry .GetAttributeValues ("member" )
149+ log .Printf ("[DEBUG] IsAdmin: Admin group has %d members" , len (adminMembers ))
150+
121151 // Check if user DN is in admin group members
122- for _ , member := range adminGroupEntry . GetAttributeValues ( "member" ) {
152+ for _ , member := range adminMembers {
123153 if member == userEntry .DN {
154+ log .Printf ("[INFO] IsAdmin: User %s is an admin" , username )
124155 return true , nil
125156 }
126157 }
127158
159+ log .Printf ("[DEBUG] IsAdmin: User %s is not an admin" , username )
128160 return false , nil
129161}
130162
131163// Close closes the LDAP connection
132164func (s * LDAPService ) Close () error {
133- return s .client .Disconnect ()
165+ log .Println ("[DEBUG] Close: Closing LDAP connection" )
166+ err := s .client .Disconnect ()
167+ if err != nil {
168+ log .Printf ("[ERROR] Close: Failed to close LDAP connection: %v" , err )
169+ } else {
170+ log .Println ("[INFO] Close: LDAP connection closed successfully" )
171+ }
172+ return err
134173}
135174
136175// HealthCheck verifies that the LDAP connection is working
137176func (s * LDAPService ) HealthCheck () error {
138- return s .client .HealthCheck ()
177+ log .Println ("[DEBUG] HealthCheck: Performing LDAP health check" )
178+ err := s .client .HealthCheck ()
179+ if err != nil {
180+ log .Printf ("[ERROR] HealthCheck: LDAP health check failed: %v" , err )
181+ } else {
182+ log .Println ("[DEBUG] HealthCheck: LDAP health check passed" )
183+ }
184+ return err
139185}
140186
141187// Reconnect attempts to reconnect to the LDAP server
142188func (s * LDAPService ) Reconnect () error {
143- return s .client .Connect ()
189+ log .Println ("[DEBUG] Reconnect: Attempting to reconnect to LDAP server" )
190+ err := s .client .Connect ()
191+ if err != nil {
192+ log .Printf ("[ERROR] Reconnect: Failed to reconnect to LDAP server: %v" , err )
193+ } else {
194+ log .Println ("[INFO] Reconnect: Successfully reconnected to LDAP server" )
195+ }
196+ return err
144197}
145198
146199// SetPassword sets the password for a user using User struct
147200func (s * LDAPService ) SetPassword (user User , password string ) error {
201+ log .Printf ("[DEBUG] SetPassword: Setting password for user: %s" , user .Name )
148202 userDN , err := s .GetUserDN (user .Name )
149203 if err != nil {
204+ log .Printf ("[ERROR] SetPassword: Failed to get user DN for %s: %v" , user .Name , err )
150205 return fmt .Errorf ("failed to get user DN: %v" , err )
151206 }
207+ log .Printf ("[DEBUG] SetPassword: Retrieved user DN: %s" , userDN )
152208
153- return s .SetUserPassword (userDN , password )
209+ err = s .SetUserPassword (userDN , password )
210+ if err != nil {
211+ log .Printf ("[ERROR] SetPassword: Failed to set password for user %s: %v" , user .Name , err )
212+ } else {
213+ log .Printf ("[INFO] SetPassword: Password set successfully for user: %s" , user .Name )
214+ }
215+ return err
154216}
0 commit comments