Skip to content

Commit 973c5e1

Browse files
committed
Use regex for permission pattern matching
Signed-off-by: Han Verstraete (OpenFaaS Ltd) <[email protected]>
1 parent 5ac0dbc commit 973c5e1

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

executor/jwt_authenticator.go

+28-15
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

@@ -215,23 +216,9 @@ func getFnNamespace() (string, error) {
215216

216217
func isAuthorized(permissions []string, namespace, fn string) bool {
217218
for _, permission := range permissions {
218-
if permission == "*" {
219+
if match := matchString(permission, fmt.Sprintf("%s:%s", namespace, fn)); match {
219220
return true
220221
}
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
235222
}
236223

237224
return false
@@ -256,3 +243,29 @@ func getPermissions(mapClaims jwt.MapClaims) []string {
256243
}
257244
return values
258245
}
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

Comments
 (0)