forked from ant1441/ldap_proxy
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ldap_connection.go
150 lines (126 loc) · 3.51 KB
/
ldap_connection.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
// original work imported from https://github.com/jtblin/go-ldap-client (thank you)
// code is slightly changed
import (
"crypto/tls"
"errors"
"fmt"
"log"
ldap "gopkg.in/ldap.v2"
)
// LDAPConfiguration contains needed information to make ldap queries
type LDAPConfiguration struct {
Attributes []string
Base string
BindDN string
BindPassword string
GroupFilter string // e.g. "(memberUid=%s)"
Host string
ServerName string
UserFilter string // e.g. "(uid=%s)"
Port int
InsecureSkipVerify bool
UseTLS bool
ClientCertificates []tls.Certificate // Adding client certificates
}
// LDAPClient contains an LDAP connection
type LDAPClient struct {
conn *ldap.Conn
cfg *LDAPConfiguration
}
// NewLDAPClient creates a connection to the ldap backend.
func NewLDAPClient(lc *LDAPConfiguration) (*LDAPClient, error) {
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", lc.Host, lc.Port))
if err != nil {
log.Printf("Unable to connect to LDAP Server: %+v", err)
return &LDAPClient{}, err
}
if lc.UseTLS {
err = l.StartTLS(&tls.Config{InsecureSkipVerify: lc.InsecureSkipVerify})
if err != nil {
log.Printf("Unable to connect to LDAP Server with TLS: %+v", err)
}
}
conn := LDAPClient{
conn: l,
cfg: lc,
}
return &conn, err
}
// Close ldap connection
func (c *LDAPClient) Close() {
if c.conn != nil {
c.conn.Close()
}
}
// Authenticate authenticates the user against the ldap backend.
func (c *LDAPClient) Authenticate(username, password string) (bool, map[string]string, error) {
if username == "" || password == "" {
return false, nil, errors.New("invalid user or password")
}
// First bind with a read only user
if c.cfg.BindDN != "" && c.cfg.BindPassword != "" {
err := c.conn.Bind(c.cfg.BindDN, c.cfg.BindPassword)
if err != nil {
return false, nil, err
}
}
attributes := append(c.cfg.Attributes, "dn")
// Search for the given username
searchRequest := ldap.NewSearchRequest(
c.cfg.Base,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf(c.cfg.UserFilter, username),
attributes,
nil,
)
sr, err := c.conn.Search(searchRequest)
if err != nil {
return false, nil, err
}
if len(sr.Entries) < 1 {
return false, nil, errors.New("User does not exist")
}
if len(sr.Entries) > 1 {
return false, nil, errors.New("Too many entries returned")
}
userDN := sr.Entries[0].DN
user := map[string]string{
"dn": userDN,
}
for _, attr := range c.cfg.Attributes {
user[attr] = sr.Entries[0].GetAttributeValue(attr)
}
// Bind as the user to verify their password
err = c.conn.Bind(userDN, password)
if err != nil {
return false, user, err
}
// Rebind as the read only user for any further queries
if c.cfg.BindDN != "" && c.cfg.BindPassword != "" {
err = c.conn.Bind(c.cfg.BindDN, c.cfg.BindPassword)
if err != nil {
return false, user, err
}
}
return true, user, nil
}
// GetGroupsOfUser returns the group for a user.
func (c *LDAPClient) GetGroupsOfUser(username string) ([]string, error) {
searchRequest := ldap.NewSearchRequest(
c.cfg.Base,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf(c.cfg.GroupFilter, username),
[]string{"cn"}, // can it be something else than "cn"?
nil,
)
sr, err := c.conn.Search(searchRequest)
if err != nil {
return nil, err
}
groups := []string{}
for _, entry := range sr.Entries {
groups = append(groups, entry.GetAttributeValue("cn"))
}
return groups, nil
}