forked from openshift/library-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attribute.go
47 lines (43 loc) · 1.22 KB
/
attribute.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
package ldaputil
import (
"encoding/base64"
"strings"
"gopkg.in/ldap.v2"
)
// GetAttributeValue finds the first attribute of those given that the LDAP entry has, and
// returns it. GetAttributeValue is able to query the DN as well as Attributes of the LDAP entry.
// If no value is found, the empty string is returned.
func GetAttributeValue(entry *ldap.Entry, attributes []string) string {
for _, k := range attributes {
// Ignore empty attributes
if len(k) == 0 {
continue
}
// Special-case DN, since it's not an attribute
if strings.ToLower(k) == "dn" {
return entry.DN
}
// Otherwise get an attribute and return it if present
if v := entry.GetAttributeValue(k); len(v) > 0 {
return v
}
}
return ""
}
func GetRawAttributeValue(entry *ldap.Entry, attributes []string) string {
for _, k := range attributes {
// Ignore empty attributes
if len(k) == 0 {
continue
}
// Special-case DN, since it's not an attribute
if strings.ToLower(k) == "dn" {
return base64.RawURLEncoding.EncodeToString([]byte(entry.DN))
}
// Otherwise get an attribute and return it if present
if v := entry.GetRawAttributeValue(k); len(v) > 0 {
return base64.RawURLEncoding.EncodeToString(v)
}
}
return ""
}