-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldapurl.go
101 lines (85 loc) · 1.81 KB
/
ldapurl.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
package ldapurl
import (
"errors"
"fmt"
"net"
"net/url"
"strconv"
"strings"
)
const (
DefaultLdapPort = 389
DefaultLdapsPort = 636
)
type LdapURL struct {
Scheme string
Host string
Port int
DN string
Attributes []string
Scope string
Filter string
Extensions []string
}
func SplitHostPort(hostport string, defaultport int) (host string, port int) {
host, portstring, err := net.SplitHostPort(hostport)
if err != nil {
port = defaultport
host = hostport
return
}
// Need to convert string port to int
port, err = strconv.Atoi(portstring)
if err != nil {
port = defaultport
}
return
}
func Parse(rawurl string) (ldapurl *LdapURL, err error) {
u, err := url.Parse(rawurl)
if err != nil {
return
}
host, port := SplitHostPort(u.Host, 0)
// Start building the object
ldapurl = &LdapURL{Scheme: u.Scheme, Host: host}
// Check for supported schemes and set port defaults and TLS status appropriately
switch u.Scheme {
case "ldap":
if ldapurl.Port = port; port == 0 {
ldapurl.Port = DefaultLdapPort
}
break
case "ldaps":
if ldapurl.Port = port; port == 0 {
ldapurl.Port = DefaultLdapsPort
}
break
default:
err = errors.New(fmt.Sprintf("Unsupported LDAP URL scheme: %s", u.Scheme))
return
}
// DN part of the URL
ldapurl.DN = strings.TrimPrefix(u.Path, "/")
parts := strings.Split(u.RawQuery, "?")
for i, v := range parts {
switch i {
case 0:
ldapurl.Attributes = strings.Split(v,",")
case 1:
ldapurl.Scope = v
case 2:
ldapurl.Filter = v
case 3:
ldapurl.Extensions = strings.Split(v,",")
}
}
return
}
func (ldapurl LdapURL) BuildHostnamePortString() (hostname string) {
hostname = fmt.Sprintf("%s:%d", ldapurl.Host, ldapurl.Port)
return
}
func (ldapurl LdapURL) IsTLS() bool {
return ldapurl.Scheme == "ldaps"
}