mirror of
https://github.com/datarhei/core.git
synced 2025-10-06 08:27:08 +08:00
77 lines
1.4 KiB
Go
77 lines
1.4 KiB
Go
package access
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/datarhei/core/v16/glob"
|
|
)
|
|
|
|
func resourceMatch(request, policy string) bool {
|
|
reqPrefix, reqResource := getPrefix(request)
|
|
polPrefix, polResource := getPrefix(policy)
|
|
|
|
if reqPrefix != polPrefix {
|
|
return false
|
|
}
|
|
|
|
var match bool
|
|
var err error
|
|
|
|
if reqPrefix == "api" || reqPrefix == "fs" || reqPrefix == "rtmp" || reqPrefix == "srt" {
|
|
match, err = glob.Match(polResource, reqResource, rune('/'))
|
|
if err != nil {
|
|
return false
|
|
}
|
|
} else {
|
|
match, err = glob.Match(polResource, reqResource)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return match
|
|
}
|
|
|
|
func resourceMatchFunc(args ...interface{}) (interface{}, error) {
|
|
request := args[0].(string)
|
|
policy := args[1].(string)
|
|
|
|
return (bool)(resourceMatch(request, policy)), nil
|
|
}
|
|
|
|
func actionMatch(request string, policy string) bool {
|
|
request = strings.ToUpper(request)
|
|
actions := strings.Split(strings.ToUpper(policy), "|")
|
|
if len(actions) == 0 {
|
|
return false
|
|
}
|
|
|
|
if len(actions) == 1 && actions[0] == "ANY" {
|
|
return true
|
|
}
|
|
|
|
for _, a := range actions {
|
|
if request == a {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func actionMatchFunc(args ...interface{}) (interface{}, error) {
|
|
request := args[0].(string)
|
|
policy := args[1].(string)
|
|
|
|
return (bool)(actionMatch(request, policy)), nil
|
|
}
|
|
|
|
func getPrefix(s string) (string, string) {
|
|
prefix, resource, found := strings.Cut(s, ":")
|
|
if !found {
|
|
return "", s
|
|
}
|
|
|
|
return prefix, resource
|
|
}
|