8
8
"log"
9
9
"net/http"
10
10
"os"
11
+ "regexp"
11
12
"strings"
12
13
"time"
13
14
@@ -215,23 +216,9 @@ func getFnNamespace() (string, error) {
215
216
216
217
func isAuthorized (permissions []string , namespace , fn string ) bool {
217
218
for _ , permission := range permissions {
218
- if permission == "*" {
219
+ if match := matchString ( permission , fmt . Sprintf ( "%s:%s" , namespace , fn )); match {
219
220
return true
220
221
}
221
-
222
- parts := strings .Split (permission , ":" )
223
- allowedNamespace := parts [0 ]
224
- allowedFunction := parts [1 ]
225
-
226
- if namespace != allowedNamespace {
227
- continue
228
- }
229
-
230
- if allowedFunction != "*" && fn != allowedFunction {
231
- continue
232
- }
233
-
234
- return true
235
222
}
236
223
237
224
return false
@@ -256,3 +243,29 @@ func getPermissions(mapClaims jwt.MapClaims) []string {
256
243
}
257
244
return values
258
245
}
246
+
247
+ func matchString (pattern string , value string ) bool {
248
+ if len (pattern ) > 0 {
249
+ result , _ := regexp .MatchString (wildCardToRegexp (pattern ), value )
250
+ return result
251
+ }
252
+
253
+ return pattern == value
254
+ }
255
+
256
+ // wildCardToRegexp converts a wildcard pattern to a regular expression pattern.
257
+ func wildCardToRegexp (pattern string ) string {
258
+ var result strings.Builder
259
+ for i , literal := range strings .Split (pattern , "*" ) {
260
+
261
+ // Replace * with .*
262
+ if i > 0 {
263
+ result .WriteString (".*" )
264
+ }
265
+
266
+ // Quote any regular expression meta characters in the
267
+ // literal text.
268
+ result .WriteString (regexp .QuoteMeta (literal ))
269
+ }
270
+ return result .String ()
271
+ }
0 commit comments