2021-11-05 09:37:30 +00:00
|
|
|
package utils
|
2021-07-22 18:38:30 +00:00
|
|
|
|
|
|
|
import (
|
2022-01-25 10:27:27 +00:00
|
|
|
"strings"
|
|
|
|
|
2023-06-06 19:40:19 +00:00
|
|
|
"beryju.io/ldap"
|
2021-07-23 18:00:23 +00:00
|
|
|
goldap "github.com/go-ldap/ldap/v3"
|
2021-07-22 18:38:30 +00:00
|
|
|
ber "github.com/nmcclain/asn1-ber"
|
2022-03-03 09:40:07 +00:00
|
|
|
"goauthentik.io/api/v3"
|
2021-11-05 09:37:30 +00:00
|
|
|
"goauthentik.io/internal/outpost/ldap/constants"
|
2021-07-22 18:38:30 +00:00
|
|
|
)
|
|
|
|
|
2021-11-05 09:37:30 +00:00
|
|
|
func ParseFilterForGroup(req api.ApiCoreGroupsListRequest, f *ber.Packet, skip bool) (api.ApiCoreGroupsListRequest, bool) {
|
2021-07-22 18:38:30 +00:00
|
|
|
switch f.Tag {
|
|
|
|
case ldap.FilterEqualityMatch:
|
|
|
|
return parseFilterForGroupSingle(req, f)
|
|
|
|
case ldap.FilterAnd:
|
|
|
|
for _, child := range f.Children {
|
2021-11-05 09:37:30 +00:00
|
|
|
r, s := ParseFilterForGroup(req, child, skip)
|
2021-08-21 15:53:09 +00:00
|
|
|
skip = skip || s
|
|
|
|
req = r
|
2021-07-22 18:38:30 +00:00
|
|
|
}
|
2021-08-21 15:53:09 +00:00
|
|
|
return req, skip
|
2021-07-22 18:38:30 +00:00
|
|
|
}
|
2021-08-21 15:53:09 +00:00
|
|
|
return req, skip
|
2021-07-22 18:38:30 +00:00
|
|
|
}
|
|
|
|
|
2021-08-21 15:53:09 +00:00
|
|
|
func parseFilterForGroupSingle(req api.ApiCoreGroupsListRequest, f *ber.Packet) (api.ApiCoreGroupsListRequest, bool) {
|
2021-07-23 13:41:09 +00:00
|
|
|
// We can only handle key = value pairs here
|
|
|
|
if len(f.Children) < 2 {
|
2021-08-21 15:53:09 +00:00
|
|
|
return req, false
|
2021-07-23 13:41:09 +00:00
|
|
|
}
|
|
|
|
k := f.Children[0].Value
|
|
|
|
// Ensure key is string
|
|
|
|
if _, ok := k.(string); !ok {
|
2021-08-21 15:53:09 +00:00
|
|
|
return req, false
|
2021-07-23 13:41:09 +00:00
|
|
|
}
|
|
|
|
v := f.Children[1].Value
|
|
|
|
// Null values are ignored
|
|
|
|
if v == nil {
|
2021-08-21 15:53:09 +00:00
|
|
|
return req, false
|
2021-07-23 13:41:09 +00:00
|
|
|
}
|
2022-12-12 18:30:52 +00:00
|
|
|
val := stringify(v)
|
|
|
|
if val == nil {
|
|
|
|
return req, false
|
|
|
|
}
|
|
|
|
// Check key
|
|
|
|
switch strings.ToLower(k.(string)) {
|
|
|
|
case "cn":
|
|
|
|
return req.Name(*val), false
|
|
|
|
case "member":
|
|
|
|
fallthrough
|
|
|
|
case "memberOf":
|
|
|
|
userDN, err := goldap.ParseDN(*val)
|
|
|
|
if err != nil {
|
|
|
|
return req.MembersByUsername([]string{*val}), false
|
|
|
|
}
|
|
|
|
username := userDN.RDNs[0].Attributes[0].Value
|
|
|
|
// If the DN's first ou is virtual-groups, ignore this filter
|
|
|
|
if len(userDN.RDNs) > 1 {
|
|
|
|
if strings.EqualFold(userDN.RDNs[1].Attributes[0].Value, constants.OUVirtualGroups) || strings.EqualFold(userDN.RDNs[1].Attributes[0].Value, constants.OUGroups) {
|
|
|
|
// Since we know we're not filtering anything, skip this request
|
|
|
|
return req, true
|
2021-08-21 15:53:09 +00:00
|
|
|
}
|
2021-07-23 13:41:09 +00:00
|
|
|
}
|
2022-12-12 18:30:52 +00:00
|
|
|
return req.MembersByUsername([]string{username}), false
|
2021-07-22 18:38:30 +00:00
|
|
|
}
|
2021-08-21 15:53:09 +00:00
|
|
|
return req, false
|
2021-07-22 18:38:30 +00:00
|
|
|
}
|