-
Notifications
You must be signed in to change notification settings - Fork 1
/
resolvers.go
55 lines (45 loc) · 1.49 KB
/
resolvers.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
48
49
50
51
52
53
54
55
package filters
import (
"fmt"
ldap "github.com/chezmoi-sh/yaldap/pkg/ldap/directory"
ber "github.com/go-asn1-ber/asn1-ber"
goldap "github.com/go-ldap/ldap/v3"
)
//nolint:gochecknoinits
func init() {
goldap.FilterMap[0xFFFFFFFFFFFFFFFF] = "<unknown>"
}
var berFilterResolvers = map[ber.Tag]BerFilterExpressionResolver{}
// Match uses the given filter to check if the current entry matches it.
func Match(object ldap.Object, filter *ber.Packet) (bool, error) {
return berFilterResolvers[filter.Tag].Resolve(object, filter)
}
// BerFilterExpressionResolver is a function wrapper that apply a specific type of LDAP filter expression on the
// given directory entry. It returns true if the filter match the current entry, false otherwise.
type BerFilterExpressionResolver struct {
resolve func(object ldap.Object, filter *ber.Packet) (bool, error)
}
func (resolver BerFilterExpressionResolver) Resolve(object ldap.Object, filter *ber.Packet) (bool, error) {
if filter == nil {
return false, &Error{
ber.Tag(0xFFFFFFFFFFFFFFFF),
fmt.Errorf("no filter provided"),
}
}
if resolver.resolve == nil {
return false, &Error{
filter.Tag,
fmt.Errorf("not implemented"),
}
}
return resolver.resolve(object, filter)
}
// An Error describes a failure to execute a filter resolver.
type Error struct {
tag ber.Tag
err error
}
func (err Error) Unwrap() error { return err.err }
func (err Error) Error() string {
return fmt.Sprintf("invalid `%s` filter: %s", goldap.FilterMap[uint64(err.tag)], err.err)
}