2021-11-05 09:37:30 +00:00
|
|
|
package utils
|
2021-07-22 18:38:30 +00:00
|
|
|
|
|
|
|
import (
|
2021-08-21 14:23:07 +00:00
|
|
|
goldap "github.com/go-ldap/ldap/v3"
|
2021-07-22 18:38:30 +00:00
|
|
|
ber "github.com/nmcclain/asn1-ber"
|
|
|
|
"github.com/nmcclain/ldap"
|
|
|
|
"goauthentik.io/api"
|
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 ParseFilterForUser(req api.ApiCoreUsersListRequest, f *ber.Packet, skip bool) (api.ApiCoreUsersListRequest, bool) {
|
2021-07-22 18:38:30 +00:00
|
|
|
switch f.Tag {
|
|
|
|
case ldap.FilterEqualityMatch:
|
|
|
|
return parseFilterForUserSingle(req, f)
|
|
|
|
case ldap.FilterAnd:
|
|
|
|
for _, child := range f.Children {
|
2021-11-05 09:37:30 +00:00
|
|
|
r, s := ParseFilterForUser(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 parseFilterForUserSingle(req api.ApiCoreUsersListRequest, f *ber.Packet) (api.ApiCoreUsersListRequest, 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
|
|
|
}
|
|
|
|
// Switch on type of the value, then check the key
|
|
|
|
switch vv := v.(type) {
|
|
|
|
case string:
|
|
|
|
switch k {
|
|
|
|
case "cn":
|
2021-08-21 15:53:09 +00:00
|
|
|
return req.Username(vv), false
|
2021-07-23 13:41:09 +00:00
|
|
|
case "name":
|
|
|
|
case "displayName":
|
2021-08-21 15:53:09 +00:00
|
|
|
return req.Name(vv), false
|
2021-07-23 13:41:09 +00:00
|
|
|
case "mail":
|
2021-08-21 15:53:09 +00:00
|
|
|
return req.Email(vv), false
|
2021-08-21 14:23:07 +00:00
|
|
|
case "member":
|
2021-08-21 15:53:09 +00:00
|
|
|
fallthrough
|
2021-08-21 14:23:07 +00:00
|
|
|
case "memberOf":
|
|
|
|
groupDN, err := goldap.ParseDN(vv)
|
|
|
|
if err != nil {
|
2021-08-21 15:53:09 +00:00
|
|
|
return req.GroupsByName([]string{vv}), false
|
2021-08-21 14:23:07 +00:00
|
|
|
}
|
|
|
|
name := groupDN.RDNs[0].Attributes[0].Value
|
2021-08-21 15:53:09 +00:00
|
|
|
// If the DN's first ou is virtual-groups, ignore this filter
|
|
|
|
if len(groupDN.RDNs) > 1 {
|
2021-11-05 09:37:30 +00:00
|
|
|
if groupDN.RDNs[1].Attributes[0].Value == constants.OUUsers || groupDN.RDNs[1].Attributes[0].Value == constants.OUVirtualGroups {
|
2021-08-21 15:53:09 +00:00
|
|
|
// Since we know we're not filtering anything, skip this request
|
|
|
|
return req, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return req.GroupsByName([]string{name}), false
|
2021-07-23 13:41:09 +00:00
|
|
|
}
|
|
|
|
// TODO: Support int
|
|
|
|
default:
|
2021-08-21 15:53:09 +00:00
|
|
|
return req, 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
|
|
|
}
|