2021-07-22 18:38:30 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
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-07-23 13:41:09 +00:00
|
|
|
func parseFilterForUser(req api.ApiCoreUsersListRequest, f *ber.Packet) api.ApiCoreUsersListRequest {
|
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-07-23 13:41:09 +00:00
|
|
|
req = parseFilterForUser(req, child)
|
2021-07-22 18:38:30 +00:00
|
|
|
}
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseFilterForUserSingle(req api.ApiCoreUsersListRequest, f *ber.Packet) api.ApiCoreUsersListRequest {
|
2021-07-23 13:41:09 +00:00
|
|
|
// We can only handle key = value pairs here
|
|
|
|
if len(f.Children) < 2 {
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
k := f.Children[0].Value
|
|
|
|
// Ensure key is string
|
|
|
|
if _, ok := k.(string); !ok {
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
v := f.Children[1].Value
|
|
|
|
// Null values are ignored
|
|
|
|
if v == nil {
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
// Switch on type of the value, then check the key
|
|
|
|
switch vv := v.(type) {
|
|
|
|
case string:
|
|
|
|
switch k {
|
|
|
|
case "cn":
|
|
|
|
return req.Username(vv)
|
|
|
|
case "name":
|
|
|
|
case "displayName":
|
|
|
|
return req.Name(vv)
|
|
|
|
case "mail":
|
|
|
|
return req.Email(vv)
|
2021-08-21 14:23:07 +00:00
|
|
|
case "member":
|
|
|
|
case "memberOf":
|
|
|
|
groupDN, err := goldap.ParseDN(vv)
|
|
|
|
if err != nil {
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
name := groupDN.RDNs[0].Attributes[0].Value
|
|
|
|
return req.GroupsByName([]string{name})
|
2021-07-23 13:41:09 +00:00
|
|
|
}
|
|
|
|
// TODO: Support int
|
|
|
|
default:
|
|
|
|
return req
|
2021-07-22 18:38:30 +00:00
|
|
|
}
|
|
|
|
return req
|
|
|
|
}
|