Skip to content

Commit 19d7e56

Browse files
committed
Use regex for permission pattern matching
Signed-off-by: Han Verstraete (OpenFaaS Ltd) <[email protected]>
1 parent 8805db3 commit 19d7e56

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

executor/jwt_authenticator.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log"
99
"net/http"
1010
"os"
11+
"regexp"
1112
"strings"
1213
"time"
1314

@@ -219,19 +220,9 @@ func isAuthorized(permissions []string, namespace, fn string) bool {
219220
return true
220221
}
221222

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
223+
if matchString(permission, fmt.Sprintf("%s:%s", namespace, fn)) {
224+
return true
232225
}
233-
234-
return true
235226
}
236227

237228
return false
@@ -256,3 +247,29 @@ func getPermissions(mapClaims jwt.MapClaims) []string {
256247
}
257248
return values
258249
}
250+
251+
func matchString(pattern string, value string) bool {
252+
if len(pattern) > 0 {
253+
result, _ := regexp.MatchString(wildCardToRegexp(pattern), value)
254+
return result
255+
}
256+
257+
return pattern == value
258+
}
259+
260+
// wildCardToRegexp converts a wildcard pattern to a regular expression pattern.
261+
func wildCardToRegexp(pattern string) string {
262+
var result strings.Builder
263+
for i, literal := range strings.Split(pattern, "*") {
264+
265+
// Replace * with .*
266+
if i > 0 {
267+
result.WriteString(".*")
268+
}
269+
270+
// Quote any regular expression meta characters in the
271+
// literal text.
272+
result.WriteString(regexp.QuoteMeta(literal))
273+
}
274+
return result.String()
275+
}

0 commit comments

Comments
 (0)