@@ -3,36 +3,40 @@ package scopie
3
3
import (
4
4
"errors"
5
5
"fmt"
6
+ "strings"
6
7
)
7
8
8
9
const (
9
10
BlockSeperator = byte ('/' )
10
11
ArraySeperator = byte ('|' )
11
12
VariablePrefix = byte ('@' )
12
13
Wildcard = byte ('*' )
14
+
15
+ AllowPermission = "allow"
16
+ DenyPermission = "deny"
13
17
)
14
18
15
19
const (
16
20
fmtAllowedInvalidChar = "scopie-100 in %s: invalid character '%s'"
17
- fmtAllowedVarInArray = "scopie-101 in actor : variable '%s' found in array block"
18
- fmtAllowedVarNotFound = "scopie-104 in actor : variable '%s' not found"
21
+ fmtAllowedVarInArray = "scopie-101: variable '%s' found in array block"
22
+ fmtAllowedVarNotFound = "scopie-104: variable '%s' not found"
19
23
20
24
fmtValidateVarInArray = "scopie-101: variable '%s' found in array block"
21
25
fmtValidateInvalidChar = "scopie-100: invalid character '%s'"
22
26
)
23
27
24
28
var (
25
- errAllowedSuperNotLast = errors .New ("scopie-105 in actor : super wildcard not in the last block" )
26
- errAllowedSuperInArray = errors .New ("scopie-103 in actor : super wildcard found in array block" )
27
- errAllowedWildcardInArray = errors .New ("scopie-102 in actor : wildcard found in array block" )
28
- errAllowedActionScopesEmpty = errors .New ("scopie-106: action scopes was empty" )
29
- errAllowedActionScopeEmpty = errors .New ("scopie-106: action scope was empty" )
30
- errAllowedActorRuleEmpty = errors .New ("scopie-106: actor rule was empty" )
31
-
32
- errValidateWildcardInArray = errors . New ( "scopie-102: wildcard found in array block" )
33
- errValidateSuperInArray = errors .New ("scopie-103: super wildcard found in array block " )
34
- errValidateSuperNotLast = errors .New ("scopie-105: super wildcard not in the last block " )
35
- errValidateEmpty = errors .New ("scopie-106: scope was empty " )
29
+ errSuperNotLast = errors .New ("scopie-105: super wildcard not in the last block" )
30
+ errSuperInArray = errors .New ("scopie-103: super wildcard found in array block" )
31
+ errWildcardInArray = errors .New ("scopie-102: wildcard found in array block" )
32
+ errActionScopesEmpty = errors .New ("scopie-106 in action: scopes was empty" )
33
+ errActionScopeEmpty = errors .New ("scopie-106 in action: scope was empty" )
34
+ errActorRuleEmpty = errors .New ("scopie-106 in actor: rule was empty" )
35
+
36
+ // validation specific
37
+ errValidateScopeRulesEmpty = errors .New ("scopie-106: scope or rule was empty " )
38
+ errValidateNoScopeRules = errors .New ("scopie-106: scope or rule array was empty " )
39
+ errValidateInconsistent = errors .New ("scopie-107: inconsistent array of scopes and rules " )
36
40
)
37
41
38
42
// IsAllowedFunc is a type wrapper for IsAllowed that can be used as
@@ -46,7 +50,7 @@ type ValidateScopeFunc func(string) error
46
50
// IsAllowed returns whether or not the required role scopes are fulfilled by our actor scopes.
47
51
func IsAllowed (actionScopes , actorRules []string , vars map [string ]string ) (bool , error ) {
48
52
if len (actionScopes ) == 0 {
49
- return false , errAllowedActionScopesEmpty
53
+ return false , errActionScopesEmpty
50
54
}
51
55
52
56
if len (actorRules ) == 0 {
@@ -57,19 +61,19 @@ func IsAllowed(actionScopes, actorRules []string, vars map[string]string) (bool,
57
61
58
62
for _ , actorRule := range actorRules {
59
63
if len (actorRule ) == 0 {
60
- return false , errAllowedActorRuleEmpty
64
+ return false , errActorRuleEmpty
61
65
}
62
66
63
67
actorRule := actorRule
64
68
65
- isAllowBlock := actorRule [ 0 ] == 'a'
69
+ isAllowBlock := strings . HasPrefix ( actorRule , AllowPermission )
66
70
if isAllowBlock && hasBeenAllowed {
67
71
continue
68
72
}
69
73
70
74
for _ , actionScope := range actionScopes {
71
75
if len (actionScope ) == 0 {
72
- return false , errAllowedActionScopeEmpty
76
+ return false , errActionScopeEmpty
73
77
}
74
78
75
79
actionScope := actionScope
@@ -90,45 +94,61 @@ func IsAllowed(actionScopes, actorRules []string, vars map[string]string) (bool,
90
94
return hasBeenAllowed , nil
91
95
}
92
96
93
- func ValidateScope ( scope string ) error {
94
- if scope == "" {
95
- return errValidateEmpty
97
+ func ValidateScopes ( scopeOrRules [] string ) error {
98
+ if len ( scopeOrRules ) == 0 {
99
+ return errValidateNoScopeRules
96
100
}
97
101
98
- inArray := false
102
+ isRules := strings .HasPrefix (scopeOrRules [0 ], AllowPermission ) ||
103
+ strings .HasPrefix (scopeOrRules [0 ], DenyPermission )
99
104
100
- for i := range scope {
101
- if scope [i ] == BlockSeperator {
102
- inArray = false
103
- continue
105
+ for _ , scope := range scopeOrRules {
106
+ if scope == "" {
107
+ return errValidateScopeRulesEmpty
104
108
}
105
109
106
- if scope [i ] == ArraySeperator {
107
- inArray = true
108
- continue
110
+ scopeIsRule := strings .HasPrefix (scope , AllowPermission ) ||
111
+ strings .HasPrefix (scope , DenyPermission )
112
+
113
+ if isRules != scopeIsRule {
114
+ return errValidateInconsistent
109
115
}
110
116
111
- if inArray {
112
- if scope [i ] == Wildcard && i < len (scope )- 1 && scope [i + 1 ] == Wildcard {
113
- return errValidateSuperInArray
117
+ inArray := false
118
+
119
+ for i := range scope {
120
+ if scope [i ] == BlockSeperator {
121
+ inArray = false
122
+ continue
114
123
}
115
124
116
- if scope [i ] == Wildcard {
117
- return errValidateWildcardInArray
125
+ if scope [i ] == ArraySeperator {
126
+ inArray = true
127
+ continue
118
128
}
119
129
120
- if scope [i ] == VariablePrefix {
121
- end := endOfArrayElement (& scope , i )
122
- return fmt .Errorf (fmtValidateVarInArray , scope [i + 1 :end ])
130
+ if inArray {
131
+ if scope [i ] == Wildcard && i < len (scope )- 1 && scope [i + 1 ] == Wildcard {
132
+ return errSuperInArray
133
+ }
134
+
135
+ if scope [i ] == Wildcard {
136
+ return errWildcardInArray
137
+ }
138
+
139
+ if scope [i ] == VariablePrefix {
140
+ end := endOfArrayElement (& scope , i )
141
+ return fmt .Errorf (fmtValidateVarInArray , scope [i + 1 :end ])
142
+ }
123
143
}
124
- }
125
144
126
- if ! isValidCharacter (scope [i ]) {
127
- return fmt .Errorf (fmtValidateInvalidChar , string (scope [i ]))
128
- }
145
+ if ! isValidCharacter (scope [i ]) {
146
+ return fmt .Errorf (fmtValidateInvalidChar , string (scope [i ]))
147
+ }
129
148
130
- if scope [i ] == Wildcard && i < len (scope )- 1 && scope [i + 1 ] == Wildcard && i < len (scope )- 2 {
131
- return errValidateSuperNotLast
149
+ if scope [i ] == Wildcard && i < len (scope )- 1 && scope [i + 1 ] == Wildcard && i < len (scope )- 2 {
150
+ return errSuperNotLast
151
+ }
132
152
}
133
153
}
134
154
@@ -165,7 +185,7 @@ func compareActorToAction(
165
185
// Super wildcards are checked here as it skips the who rest of the checks.
166
186
if actorSlider - actorLeft == 2 && (* actor )[actorLeft ] == Wildcard && (* actor )[actorLeft + 1 ] == Wildcard {
167
187
if len (* actor ) > actorSlider {
168
- return false , errAllowedSuperNotLast
188
+ return false , errSuperNotLast
169
189
}
170
190
171
191
return true , nil
@@ -218,10 +238,10 @@ func compareBlock(
218
238
219
239
if (* actor )[actorLeft ] == Wildcard {
220
240
if arrayRight - actorLeft > 1 && (* actor )[actorLeft + 1 ] == Wildcard {
221
- return false , errAllowedSuperInArray
241
+ return false , errSuperInArray
222
242
}
223
243
224
- return false , errAllowedWildcardInArray
244
+ return false , errWildcardInArray
225
245
}
226
246
227
247
if (* actor )[actorLeft :arrayRight ] == (* action )[actionLeft :actionSlider ] {
0 commit comments