-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscopie.go
302 lines (237 loc) · 7.26 KB
/
scopie.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package scopie
import (
"errors"
"fmt"
"strings"
)
const (
BlockSeperator = byte('/')
ArraySeperator = byte('|')
VariablePrefix = byte('@')
Wildcard = byte('*')
AllowPermission = "allow"
DenyPermission = "deny"
)
const (
fmtAllowedInvalidChar = "scopie-100 in %s: invalid character '%s'"
fmtAllowedVarInArray = "scopie-101: variable '%s' found in array block"
fmtAllowedVarNotFound = "scopie-104: variable '%s' not found"
fmtValidateVarInArray = "scopie-101: variable '%s' found in array block"
fmtValidateInvalidChar = "scopie-100: invalid character '%s'"
)
var (
errSuperNotLast = errors.New("scopie-105: super wildcard not in the last block")
errSuperInArray = errors.New("scopie-103: super wildcard found in array block")
errWildcardInArray = errors.New("scopie-102: wildcard found in array block")
errActionScopesEmpty = errors.New("scopie-106 in action: scopes was empty")
errActionScopeEmpty = errors.New("scopie-106 in action: scope was empty")
errActorRuleEmpty = errors.New("scopie-106 in actor: rule was empty")
// validation specific
errValidateScopeRulesEmpty = errors.New("scopie-106: scope or rule was empty")
errValidateNoScopeRules = errors.New("scopie-106: scope or rule array was empty")
errValidateInconsistent = errors.New("scopie-107: inconsistent array of scopes and rules")
)
// IsAllowedFunc is a type wrapper for IsAllowed that can be used as
// a dependency.
type IsAllowedFunc func(map[string]string, string, string) (bool, error)
// ValidateScopeFunc is a type wrapper for ValidateScope that can be
// used as a dependency.
type ValidateScopeFunc func(string) error
// IsAllowed returns whether or not the required role scopes are fulfilled by our actor scopes.
func IsAllowed(actionScopes, actorRules []string, vars map[string]string) (bool, error) {
if len(actionScopes) == 0 {
return false, errActionScopesEmpty
}
if len(actorRules) == 0 {
return false, nil
}
hasBeenAllowed := false
for _, actorRule := range actorRules {
if len(actorRule) == 0 {
return false, errActorRuleEmpty
}
actorRule := actorRule
isAllowBlock := strings.HasPrefix(actorRule, AllowPermission)
if isAllowBlock && hasBeenAllowed {
continue
}
for _, actionScope := range actionScopes {
if len(actionScope) == 0 {
return false, errActionScopeEmpty
}
actionScope := actionScope
match, err := compareActorToAction(&actorRule, &actionScope, vars)
if err != nil {
return false, err
}
if match && isAllowBlock {
hasBeenAllowed = true
} else if match && !isAllowBlock {
return false, nil
}
}
}
return hasBeenAllowed, nil
}
func ValidateScopes(scopeOrRules []string) error {
if len(scopeOrRules) == 0 {
return errValidateNoScopeRules
}
isRules := strings.HasPrefix(scopeOrRules[0], AllowPermission) ||
strings.HasPrefix(scopeOrRules[0], DenyPermission)
for _, scope := range scopeOrRules {
if scope == "" {
return errValidateScopeRulesEmpty
}
scopeIsRule := strings.HasPrefix(scope, AllowPermission) ||
strings.HasPrefix(scope, DenyPermission)
if isRules != scopeIsRule {
return errValidateInconsistent
}
inArray := false
for i := range scope {
if scope[i] == BlockSeperator {
inArray = false
continue
}
if scope[i] == ArraySeperator {
inArray = true
continue
}
if inArray {
if scope[i] == Wildcard && i < len(scope)-1 && scope[i+1] == Wildcard {
return errSuperInArray
}
if scope[i] == Wildcard {
return errWildcardInArray
}
if scope[i] == VariablePrefix {
end := endOfArrayElement(&scope, i)
return fmt.Errorf(fmtValidateVarInArray, scope[i+1:end])
}
}
if !isValidCharacter(scope[i]) {
return fmt.Errorf(fmtValidateInvalidChar, string(scope[i]))
}
if scope[i] == Wildcard && i < len(scope)-1 && scope[i+1] == Wildcard && i < len(scope)-2 {
return errSuperNotLast
}
}
}
return nil
}
func compareActorToAction(
actor *string,
action *string,
vars map[string]string,
) (bool, error) {
// Skip the allow and deny prefix for actors
actorLeft, _, _ := endOfBlock(actor, 0, "actor")
actorLeft += 1 // don't forget to skip the slash
actionLeft := 0
for actorLeft < len(*actor) || actionLeft < len(*action) {
// In case one is longer then the other
if (actorLeft < len(*actor)) != (actionLeft < len(*action)) {
return false, nil
}
actionSlider, _, err := endOfBlock(action, actionLeft, "action")
if err != nil {
return false, err
}
actorSlider, actorArray, err := endOfBlock(actor, actorLeft, "actor")
if err != nil {
return false, err
}
// Super wildcards are checked here as it skips the who rest of the checks.
if actorSlider-actorLeft == 2 && (*actor)[actorLeft] == Wildcard && (*actor)[actorLeft+1] == Wildcard {
if len(*actor) > actorSlider {
return false, errSuperNotLast
}
return true, nil
} else {
match, err := compareBlock(actor, actorLeft, actorSlider, actorArray, action, actionLeft, actionSlider, vars)
if err != nil {
return false, err
}
if !match {
return false, nil
}
}
actionLeft = actionSlider + 1
actorLeft = actorSlider + 1
}
return true, nil
}
func compareBlock(
actor *string, actorLeft, actorSlider int, actorArray bool,
action *string, actionLeft, actionSlider int,
vars map[string]string,
) (bool, error) {
if (*actor)[actorLeft] == VariablePrefix {
key := (*actor)[actorLeft+1 : actorSlider]
varValue, found := vars[key]
if !found {
return false, fmt.Errorf(fmtAllowedVarNotFound, key)
}
return varValue == (*action)[actionLeft:actionSlider], nil
}
if actorSlider-actorLeft == 1 && (*actor)[actorLeft] == Wildcard {
return true, nil
}
if actorArray {
for actorLeft < actorSlider {
arrayRight := endOfArrayElement(actor, actorLeft)
if (*actor)[actorLeft] == VariablePrefix {
key := (*actor)[actorLeft+1 : arrayRight]
return false, fmt.Errorf(fmtAllowedVarInArray, key)
}
if (*actor)[actorLeft] == Wildcard {
if arrayRight-actorLeft > 1 && (*actor)[actorLeft+1] == Wildcard {
return false, errSuperInArray
}
return false, errWildcardInArray
}
if (*actor)[actorLeft:arrayRight] == (*action)[actionLeft:actionSlider] {
return true, nil
}
actorLeft = arrayRight + 1
}
return false, nil
}
return (*actor)[actorLeft:actorSlider] == (*action)[actionLeft:actionSlider], nil
}
func endOfBlock(value *string, start int, category string) (int, bool, error) {
isArray := false
for i := start; i < len(*value); i++ {
if (*value)[i] == ArraySeperator {
isArray = true
} else if (*value)[i] == BlockSeperator {
return i, isArray, nil
} else if !isValidCharacter((*value)[i]) {
invalidChar := string((*value)[i])
return 0, false, fmt.Errorf(fmtAllowedInvalidChar, category, invalidChar)
}
}
return len(*value), isArray, nil
}
func endOfArrayElement(value *string, start int) int {
for i := start + 1; i < len(*value); i++ {
if (*value)[i] == BlockSeperator ||
(*value)[i] == ArraySeperator {
return i
}
}
return len(*value)
}
func isValidCharacter(char byte) bool {
if char >= 'a' && char <= 'z' {
return true
}
if char >= 'A' && char <= 'Z' {
return true
}
if char >= '0' && char <= '9' {
return true
}
return char == '_' || char == '-' || char == VariablePrefix || char == Wildcard
}