MSP-625 reimplement authorize
This commit is contained in:
1 parent
668cbb37d2
commit
1a63a616ee
7 files changed
+27
-194
No files matched your search
@@ -0,0 +1,27 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"isp-gate-service/domain"
|
||||
)
|
||||
|
||||
type AdminAuthorize interface {
|
||||
Authorize(id int, path string) error
|
||||
}
|
||||
|
||||
func Authorize(adminAuthorize AdminAuthorize) Middleware {
|
||||
return func(next Handler) Handler {
|
||||
return HandlerFunc(func(ctx *Context) error {
|
||||
if !ctx.authenticated {
|
||||
return errors.WithMessage(domain.ErrAuthorize, "authentication is required")
|
||||
}
|
||||
|
||||
err := adminAuthorize.Authorize(ctx.AdminId, ctx.Path)
|
||||
if err != nil {
|
||||
return errors.WithMessagef(domain.ErrAuthorize, "authorize admin: %v", err)
|
||||
}
|
||||
|
||||
return next.Handle(ctx)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package matcher
|
||||
|
||||
import "sync"
|
||||
|
||||
type atLeastOneMatcher struct {
|
||||
cache map[string]bool
|
||||
lock sync.RWMutex
|
||||
rm runtimeMatcher
|
||||
}
|
||||
|
||||
func (mm *atLeastOneMatcher) Match(method string) bool {
|
||||
if len(mm.rm.patterns) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
mm.lock.RLock()
|
||||
matched, ok := mm.cache[method]
|
||||
mm.lock.RUnlock()
|
||||
if ok {
|
||||
return matched
|
||||
}
|
||||
|
||||
mm.lock.Lock()
|
||||
defer mm.lock.Unlock()
|
||||
if matched, ok = mm.cache[method]; ok {
|
||||
return matched
|
||||
} else {
|
||||
arrayPattern := mm.rm.Match(method)
|
||||
matched = len(arrayPattern) > 0
|
||||
}
|
||||
mm.cache[method] = matched
|
||||
return matched
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package matcher
|
||||
|
||||
import "sync"
|
||||
|
||||
type cacheableMatcher struct {
|
||||
cache map[string][]string
|
||||
lock sync.RWMutex
|
||||
rm runtimeMatcher
|
||||
}
|
||||
|
||||
func (mm *cacheableMatcher) Match(method string) []string {
|
||||
if len(mm.rm.patterns) == 0 {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
mm.lock.RLock()
|
||||
arrayPattern, ok := mm.cache[method]
|
||||
mm.lock.RUnlock()
|
||||
if ok {
|
||||
return arrayPattern
|
||||
}
|
||||
|
||||
mm.lock.Lock()
|
||||
defer mm.lock.Unlock()
|
||||
if arrayPattern, ok = mm.cache[method]; ok {
|
||||
return arrayPattern
|
||||
} else if arrayPattern = mm.rm.Match(method); len(arrayPattern) > 0 {
|
||||
mm.cache[method] = arrayPattern
|
||||
}
|
||||
|
||||
return arrayPattern
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package matcher
|
||||
|
||||
var JournalMethods AtLeastOneMatcher = &atLeastOneMatcher{}
|
||||
|
||||
type Matcher interface {
|
||||
Match(method string) []string
|
||||
}
|
||||
|
||||
type AtLeastOneMatcher interface {
|
||||
Match(method string) bool
|
||||
}
|
||||
|
||||
func NewRuntimeMatcher(patterns []string) Matcher {
|
||||
return &runtimeMatcher{
|
||||
patterns: patterns,
|
||||
}
|
||||
}
|
||||
|
||||
func NewCacheableMatcher(patterns []string) Matcher {
|
||||
return &cacheableMatcher{
|
||||
cache: make(map[string][]string),
|
||||
rm: runtimeMatcher{patterns: patterns},
|
||||
}
|
||||
}
|
||||
|
||||
func NewAtLeastOneMatcher(patterns []string) AtLeastOneMatcher {
|
||||
return &atLeastOneMatcher{
|
||||
cache: make(map[string]bool),
|
||||
rm: runtimeMatcher{patterns: patterns},
|
||||
}
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
package matcher
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
patterns = []string{
|
||||
"mdm-master/group/method",
|
||||
"mdm-master/group/*",
|
||||
"mdm-master/group2/method",
|
||||
"mdm-master/group3/*",
|
||||
}
|
||||
cases = []struct {
|
||||
method string
|
||||
coincidence int
|
||||
atLeastOne bool
|
||||
}{
|
||||
{method: "mdm-master/group/method", coincidence: 2, atLeastOne: true},
|
||||
{method: "mdm-master/group/method2", coincidence: 1, atLeastOne: true},
|
||||
{method: "mdm-master/group2/", coincidence: 0, atLeastOne: false},
|
||||
{method: "mdm-master/group2/method3", coincidence: 0, atLeastOne: false},
|
||||
{method: "mdm-master/group2/method", coincidence: 1, atLeastOne: true},
|
||||
{method: "mdm-master/group3/some", coincidence: 1, atLeastOne: true},
|
||||
{method: "mdm-master/group3/", coincidence: 1, atLeastOne: true},
|
||||
}
|
||||
)
|
||||
|
||||
func TestCacheableMatcher_Match(t *testing.T) {
|
||||
matcher := NewCacheableMatcher(patterns)
|
||||
for _, c := range cases {
|
||||
res := matcher.Match(c.method)
|
||||
if c.coincidence != len(res) {
|
||||
t.Error(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAtLeastOneMatcher_Match(t *testing.T) {
|
||||
matcher := NewAtLeastOneMatcher(patterns)
|
||||
for _, c := range cases {
|
||||
res := matcher.Match(c.method)
|
||||
if c.atLeastOne != res {
|
||||
t.Error(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCacheableMatcher_Match(b *testing.B) {
|
||||
matcher := NewCacheableMatcher(patterns)
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, c := range cases {
|
||||
_ = matcher.Match(c.method)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRuntimeMatcher_Match(b *testing.B) {
|
||||
matcher := NewRuntimeMatcher(patterns)
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, c := range cases {
|
||||
_ = matcher.Match(c.method)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package matcher
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type runtimeMatcher struct {
|
||||
patterns []string
|
||||
}
|
||||
|
||||
func (mm runtimeMatcher) Match(method string) []string {
|
||||
resp := make([]string, 0)
|
||||
for _, pattern := range mm.patterns {
|
||||
patternLen := len(pattern)
|
||||
if patternLen > 0 && pattern[patternLen-1] == '*' {
|
||||
if strings.HasPrefix(method, pattern[:patternLen-1]) {
|
||||
resp = append(resp, pattern)
|
||||
}
|
||||
} else if strings.EqualFold(method, pattern) {
|
||||
resp = append(resp, pattern)
|
||||
}
|
||||
}
|
||||
return resp
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package veritification
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrorInvalidUserId = errors.New("received unexpected user identity")
|
||||
ErrorPermittedToCallApplication = errors.New("application has no rights to call this method")
|
||||
ErrorPermittedToCallUser = errors.New("user has no rights to call this method")
|
||||
)
|
||||
Reference in new issue
Block a user