2021-11-05 09:37:30 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2022-10-16 20:32:18 +00:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2022-01-25 10:27:27 +00:00
|
|
|
"strings"
|
2021-11-05 09:37:30 +00:00
|
|
|
|
|
|
|
"github.com/nmcclain/ldap"
|
2021-12-02 14:28:58 +00:00
|
|
|
ldapConstants "goauthentik.io/internal/outpost/ldap/constants"
|
2021-11-05 09:37:30 +00:00
|
|
|
)
|
|
|
|
|
2023-02-22 13:18:22 +00:00
|
|
|
func AttributeKeySanitize(key string) string {
|
|
|
|
return strings.ReplaceAll(
|
|
|
|
strings.ReplaceAll(key, "/", "-"),
|
|
|
|
".",
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-12-12 18:30:52 +00:00
|
|
|
func stringify(in interface{}) *string {
|
2021-11-05 09:37:30 +00:00
|
|
|
switch t := in.(type) {
|
|
|
|
case string:
|
|
|
|
return &t
|
|
|
|
case *string:
|
|
|
|
return t
|
|
|
|
case bool:
|
2022-10-16 20:32:18 +00:00
|
|
|
s := strconv.FormatBool(t)
|
|
|
|
return &s
|
|
|
|
case float32:
|
|
|
|
s := strconv.FormatFloat(float64(t), 'f', -1, 64)
|
|
|
|
return &s
|
|
|
|
case float64:
|
|
|
|
s := strconv.FormatFloat(t, 'f', -1, 64)
|
2021-11-05 09:37:30 +00:00
|
|
|
return &s
|
2022-10-16 20:32:18 +00:00
|
|
|
case int:
|
|
|
|
s := strconv.FormatInt(int64(t), 10)
|
2021-11-05 09:37:30 +00:00
|
|
|
return &s
|
|
|
|
default:
|
2022-04-11 13:52:55 +00:00
|
|
|
if in != nil {
|
2022-10-16 20:32:18 +00:00
|
|
|
s := fmt.Sprintf("%s", in)
|
|
|
|
return &s
|
2022-04-11 13:52:55 +00:00
|
|
|
}
|
2021-11-05 09:37:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-22 13:18:22 +00:00
|
|
|
func AttributesToLDAP(attrs map[string]interface{}, sanitize bool) []*ldap.EntryAttribute {
|
2021-11-05 09:37:30 +00:00
|
|
|
attrList := []*ldap.EntryAttribute{}
|
|
|
|
if attrs == nil {
|
|
|
|
return attrList
|
|
|
|
}
|
2022-05-27 14:28:26 +00:00
|
|
|
for attrKey, attrValue := range attrs {
|
2021-11-05 09:37:30 +00:00
|
|
|
entry := &ldap.EntryAttribute{Name: attrKey}
|
2023-02-22 13:18:22 +00:00
|
|
|
if sanitize {
|
|
|
|
entry.Name = AttributeKeySanitize(attrKey)
|
|
|
|
}
|
2021-11-05 09:37:30 +00:00
|
|
|
switch t := attrValue.(type) {
|
|
|
|
case []string:
|
|
|
|
entry.Values = t
|
|
|
|
case *[]string:
|
|
|
|
entry.Values = *t
|
|
|
|
case []interface{}:
|
|
|
|
entry.Values = make([]string, len(t))
|
|
|
|
for idx, v := range t {
|
2022-12-12 18:30:52 +00:00
|
|
|
v := stringify(v)
|
2022-02-22 18:56:50 +00:00
|
|
|
if v != nil {
|
|
|
|
entry.Values[idx] = *v
|
|
|
|
}
|
2021-11-05 09:37:30 +00:00
|
|
|
}
|
|
|
|
default:
|
2022-12-12 18:30:52 +00:00
|
|
|
v := stringify(t)
|
2021-11-05 09:37:30 +00:00
|
|
|
if v != nil {
|
|
|
|
entry.Values = []string{*v}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
attrList = append(attrList, entry)
|
|
|
|
}
|
|
|
|
return attrList
|
|
|
|
}
|
|
|
|
|
|
|
|
func EnsureAttributes(attrs []*ldap.EntryAttribute, shouldHave map[string][]string) []*ldap.EntryAttribute {
|
|
|
|
for name, values := range shouldHave {
|
|
|
|
attrs = MustHaveAttribute(attrs, name, values)
|
|
|
|
}
|
|
|
|
return attrs
|
|
|
|
}
|
|
|
|
|
|
|
|
func MustHaveAttribute(attrs []*ldap.EntryAttribute, name string, value []string) []*ldap.EntryAttribute {
|
|
|
|
shouldSet := true
|
|
|
|
for _, attr := range attrs {
|
|
|
|
if attr.Name == name {
|
|
|
|
shouldSet = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if shouldSet {
|
|
|
|
return append(attrs, &ldap.EntryAttribute{
|
|
|
|
Name: name,
|
|
|
|
Values: value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return attrs
|
|
|
|
}
|
2021-12-02 14:28:58 +00:00
|
|
|
|
|
|
|
func IncludeObjectClass(searchOC string, ocs map[string]bool) bool {
|
|
|
|
if searchOC == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-01-20 10:42:23 +00:00
|
|
|
for key, value := range ocs {
|
|
|
|
if strings.EqualFold(key, searchOC) {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2021-12-02 14:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetContainerEntry(filterOC string, dn string, ou string) *ldap.Entry {
|
|
|
|
if IncludeObjectClass(filterOC, ldapConstants.GetContainerOCs()) {
|
|
|
|
return &ldap.Entry{
|
|
|
|
DN: dn,
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
|
|
|
Name: "distinguishedName",
|
|
|
|
Values: []string{dn},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "objectClass",
|
|
|
|
Values: []string{"top", "nsContainer"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "commonName",
|
|
|
|
Values: []string{ou},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-25 10:27:27 +00:00
|
|
|
|
|
|
|
func HasSuffixNoCase(s1 string, s2 string) bool {
|
|
|
|
return strings.HasSuffix(strings.ToLower(s1), strings.ToLower(s2))
|
|
|
|
}
|