11package ldap
22
33import (
4+ "os"
5+ "crypto/tls"
46 "fmt"
5- "go.uber.org/zap"
67
8+ "github.com/PythonHacker24/linux-acl-management-backend/config"
79 "github.com/go-ldap/ldap/v3"
10+ "go.uber.org/zap"
811)
912
1013/* authenticate a user with ldap */
@@ -18,8 +21,22 @@ func AuthenticateUser(username, password, searchbase string) bool {
1821 reducing unauthorized access in edge cases
1922 */
2023
24+ var l * ldap.Conn
25+ var err error
26+ ldapAddress := config .BackendConfig .Authentication .LDAPConfig .Address
27+
28+ if config .BackendConfig .Authentication .LDAPConfig .TLS {
29+ l , err = ldap .DialURL (ldapAddress , ldap .DialWithTLSConfig (& tls.Config {
30+
31+ /* true if using self-signed certs (not recommended) */
32+ InsecureSkipVerify : false ,
33+ }))
34+ } else {
35+ l , err = ldap .DialURL (ldapAddress )
36+ }
37+
2138 /* dial to the ldap server */
22- l , err : = ldap .DialURL ("" )
39+ l , err = ldap .DialURL ("" )
2340 if err != nil {
2441 zap .L ().Error ("Failed to connect to LDAP Server" ,
2542 zap .Error (err ),
@@ -28,8 +45,12 @@ func AuthenticateUser(username, password, searchbase string) bool {
2845 }
2946 defer l .Close ()
3047
48+ /* securely fetch LDAP credentials from the environment */
49+ adminDN := os .Getenv ("LDAP_ADMIN_DN" )
50+ adminPassword := os .Getenv ("LDAP_ADMIN_PASSWORD" )
51+
3152 /* authenticating with the ldap server with admin */
32- err = l .Bind ("" , "" )
53+ err = l .Bind (adminDN , adminPassword )
3354 if err != nil {
3455 zap .L ().Error ("Admin authentication failed" ,
3556 zap .Error (err ),
0 commit comments