This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ldapcalls.go
83 lines (70 loc) · 1.58 KB
/
ldapcalls.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package ldapsync
import (
"fmt"
"github.com/go-ldap/ldap"
)
type LDAPCaller struct {
user string
password string
host string
proto string
port int64
conn *ldap.Conn
}
// Constructor of the LDAP caller with default options
func NewLDAPCaller() *LDAPCaller {
lc := new(LDAPCaller)
lc.proto = "tcp"
lc.port = 389
return lc
}
// SetUser for LDAP caller
func (lc *LDAPCaller) SetUser(user string) *LDAPCaller {
lc.user = user
return lc
}
// SetPassword sets a password authentication for the LDAP caller
func (lc *LDAPCaller) SetPassword(password string) *LDAPCaller {
lc.password = password
return lc
}
// SetPort sets a port for the LDAP caller
func (lc *LDAPCaller) SetPort(port int64) *LDAPCaller {
lc.port = port
return lc
}
// SetProto sets a protocol scheme for the LDAP caller
func (lc *LDAPCaller) SetProto(proto string) *LDAPCaller {
lc.proto = proto
return lc
}
// SetHost sets a hostname FQDSN for the LDAP caller
func (lc *LDAPCaller) SetHost(host string) *LDAPCaller {
lc.host = host
return lc
}
// Connect to the LDAP
func (lc *LDAPCaller) Connect() {
var err error
if lc.conn == nil {
lc.conn, err = ldap.Dial(lc.proto, fmt.Sprintf("%s:%d", lc.host, lc.port))
if err != nil {
Log.Fatal(err)
}
}
}
// Disconnect from the LDAP and drain the connection
func (lc *LDAPCaller) Disconnect() {
if lc.conn != nil {
lc.conn.Close()
lc.conn = nil
}
}
// Search LDAP by request
func (lc *LDAPCaller) Search(request *ldap.SearchRequest) *ldap.SearchResult {
res, err := lc.conn.Search(request)
if err != nil {
Log.Fatal(err)
}
return res
}