-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathroute.go
195 lines (163 loc) · 3.83 KB
/
route.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright (c) Jim Lambert
// SPDX-License-Identifier: MIT
package gldap
import (
"strings"
)
// routeOperation represents the ldap operation for a route.
type routeOperation string
const (
// undefinedRouteOperation is an undefined operation.
undefinedRouteOperation routeOperation = "" // nolint:unused
// bindRouteOperation is a route supporting the bind operation
bindRouteOperation routeOperation = "bind"
// searchRouteOperation is a route supporting the search operation
searchRouteOperation routeOperation = "search"
// extendedRouteOperation is a route supporting an extended operation
extendedRouteOperation routeOperation = "extendedOperation"
// modifyRouteOperation is a route supporting the modify operation
modifyRouteOperation routeOperation = "modify"
// addRouteOperation is a route supporting the add operation
addRouteOperation routeOperation = "add"
// deleteRouteOperation is a route supporting the delete operation
deleteRouteOperation routeOperation = "delete"
// unbindRouteOperation is a route supporting the unbind operation
unbindRouteOperation routeOperation = "unbind"
// defaultRouteOperation is a default route which is used when there are no routes
// defined for a particular operation
defaultRouteOperation routeOperation = "noRoute" // nolint:unused
)
// HandlerFunc defines a function for handling an LDAP request.
type HandlerFunc func(*ResponseWriter, *Request)
type route interface {
match(req *Request) bool
handler() HandlerFunc
op() routeOperation
}
type baseRoute struct {
h HandlerFunc
routeOp routeOperation
label string
}
func (r *baseRoute) handler() HandlerFunc {
return r.h
}
func (r *baseRoute) op() routeOperation {
return r.routeOp
}
func (r *baseRoute) match(req *Request) bool {
return false
}
type searchRoute struct {
*baseRoute
basedn string
filter string
scope Scope
}
type simpleBindRoute struct {
*baseRoute
authChoice AuthChoice
}
type unbindRoute struct {
*baseRoute
}
type extendedRoute struct {
*baseRoute
extendedName ExtendedOperationName
}
type modifyRoute struct {
*baseRoute
}
type addRoute struct {
*baseRoute
}
type deleteRoute struct {
*baseRoute
}
func (r *deleteRoute) match(req *Request) bool {
if req == nil {
return false
}
if r.op() != req.routeOp {
return false
}
if _, ok := req.message.(*DeleteMessage); !ok {
return false
}
return true
}
func (r *addRoute) match(req *Request) bool {
if req == nil {
return false
}
if r.op() != req.routeOp {
return false
}
if _, ok := req.message.(*AddMessage); !ok {
return false
}
return true
}
func (r *modifyRoute) match(req *Request) bool {
if req == nil {
return false
}
if r.op() != req.routeOp {
return false
}
if _, ok := req.message.(*ModifyMessage); !ok {
return false
}
return true
}
func (r *simpleBindRoute) match(req *Request) bool {
if req == nil {
return false
}
if r.op() != req.routeOp {
return false
}
if m, ok := req.message.(*SimpleBindMessage); ok {
if r.authChoice != "" && r.authChoice == m.AuthChoice {
return true
}
}
return false
}
func (r *extendedRoute) match(req *Request) bool {
if req == nil {
return false
}
if r.op() != req.routeOp {
return false
}
if r.extendedName != req.extendedName {
return false
}
_, ok := req.message.(*ExtendedOperationMessage)
return ok
}
func (r *searchRoute) match(req *Request) bool {
if req == nil {
return false
}
if r.op() != req.routeOp {
return false
}
searchMsg, ok := req.message.(*SearchMessage)
if !ok {
return false
}
if r.basedn != "" && !strings.EqualFold(searchMsg.BaseDN, r.basedn) {
return false
}
if r.filter != "" && !strings.EqualFold(searchMsg.Filter, r.filter) {
return false
}
if r.scope != 0 && searchMsg.Scope != r.scope {
return false
}
// if it didn't get eliminated by earlier request criteria, then it's a
// match.
return true
}