-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
65 lines (55 loc) · 1.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package goldap
import (
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/go-ldap/ldap/v3"
)
// Client represents an LDAP client instance
type Client struct {
Conn *ldap.Conn
Host string
Port int
BindUser string
BindPassword string
TLS bool
TLSCACert string
TLSInsecure bool
}
// Connect Creates un ldap connection
func (c *Client) Connect() error {
uri := fmt.Sprintf("%s:%d", c.Host, c.Port)
if c.TLS {
// Get the System Cert Pool
caCertPool, err := x509.SystemCertPool()
if err != nil {
return fmt.Errorf("error tls: %s", err)
}
// Use the provided CA certificate
if c.TLSCACert != "" {
caCertPool = x509.NewCertPool()
if ok := caCertPool.AppendCertsFromPEM([]byte(c.TLSCACert)); !ok {
return fmt.Errorf("error tls: Can't add the CA certificate to certificate pool")
}
}
conn, err := ldap.DialTLS("tcp", uri, &tls.Config{
RootCAs: caCertPool,
InsecureSkipVerify: c.TLSInsecure,
})
if err != nil {
return fmt.Errorf("error dialing: %s", err)
}
c.Conn = conn
} else {
conn, err := ldap.Dial("tcp", uri)
if err != nil {
return fmt.Errorf("error dialing: %s", err)
}
c.Conn = conn
}
err := c.Conn.Bind(c.BindUser, c.BindPassword)
if err != nil {
return fmt.Errorf("error binding: %s", err)
}
return nil
}