-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathldap.go
104 lines (76 loc) · 1.75 KB
/
ldap.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
package main
import (
"fmt"
"github.com/jtblin/go-ldap-client"
"net/url"
"strconv"
"strings"
)
func createLDAPClient(c *Config) (*ldap.LDAPClient, error) {
client := &ldap.LDAPClient{}
err := configureLDAPServer(c.LdapServer, client)
if err != nil {
return nil, err
}
client.Attributes = packLDAPAttributes(c.HeadersMap)
client.Base = c.LdapBase
client.BindDN = c.LdapBindDN
client.BindPassword = c.LdapBindPassword
client.UserFilter = c.LdapUserFilter
client.GroupFilter = c.LdapGroupFilter
return client, nil
}
func configureLDAPServer(rawURL string, client *ldap.LDAPClient) error {
if strings.Index(rawURL, "://") < 0 {
rawURL = "ldap://" + rawURL
}
u, err := url.Parse(rawURL)
if err != nil {
return err
}
q, err := url.ParseQuery(u.RawQuery)
if err != nil {
return err
}
host := u.Hostname()
port := 389 // tcp
useSSL := false
if u.Scheme != "" && u.Scheme != "ldap" && u.Scheme != "ldaps" {
return fmt.Errorf("invalid LDAP server URL scheme: %s", u.Scheme)
}
if u.Scheme == "ldaps" {
useSSL = true
port = 636 // ssl
}
if u.Port() != "" {
raw, err := strconv.Atoi(u.Port())
if err != nil {
return fmt.Errorf("invalid LDAP server URL port: %s", u.Port())
}
port = raw
}
client.Host = host
client.Port = port
client.UseSSL = useSSL
if useSSL {
client.ServerName = host
}
if q.Get("ServerName") != "" {
client.ServerName = q.Get("ServerName")
}
if q.Get("InsecureSkipVerify") != "" {
v, err := strconv.ParseBool(q.Get("InsecureSkipVerify"))
if err != nil {
return err
}
client.InsecureSkipVerify = v
}
return nil
}
func packLDAPAttributes(headers map[string]string) []string {
a := make([]string, 0, len(headers))
for _, val := range headers {
a = append(a, val)
}
return a
}