-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
63 lines (47 loc) · 961 Bytes
/
client.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
package ldap
import (
"errors"
"github.com/go-ldap/ldap/v3"
)
type Config struct {
Server string
BaseDN string
IsActiveDirectory bool
DialOptions []ldap.DialOpt
}
type LDAP struct {
config Config
user string
password string
}
var ErrDNDuplicated = errors.New("DN is not unique")
func New(config Config, user, password string) (*LDAP, error) {
l := &LDAP{
config,
user,
password,
}
c, err := l.GetConnection()
if err != nil {
return nil, err
}
c.Close()
return l, nil
}
func (l *LDAP) WithCredentials(dn, password string) (*LDAP, error) {
return New(l.config, dn, password)
}
func (l LDAP) GetConnection() (*ldap.Conn, error) {
dialOpts := make([]ldap.DialOpt, 0)
if l.config.DialOptions != nil {
dialOpts = l.config.DialOptions
}
c, err := ldap.DialURL(l.config.Server, dialOpts...)
if err != nil {
return nil, err
}
if err = c.Bind(l.user, l.password); err != nil {
return nil, err
}
return c, nil
}