From 1a63a616eea9947a49d7e1408f90c6fc80f8edb1 Mon Sep 17 00:00:00 2001 From: DeniskaRediska Date: Mon, 30 May 2022 17:34:54 +0300 Subject: [PATCH] MSP-625 reimplement authorize --- middleware/autorize.go | 27 +++++++++++ service/matcher/at_least_one_mather.go | 33 ------------- service/matcher/cacheable_matcher.go | 32 ------------- service/matcher/matcher.go | 31 ------------ service/matcher/matcher_test.go | 65 -------------------------- service/matcher/runtime_matcher.go | 24 ---------- service/veritification/error.go | 9 ---- 7 files changed, 27 insertions(+), 194 deletions(-) create mode 100644 middleware/autorize.go delete mode 100644 service/matcher/at_least_one_mather.go delete mode 100644 service/matcher/cacheable_matcher.go delete mode 100644 service/matcher/matcher.go delete mode 100644 service/matcher/matcher_test.go delete mode 100644 service/matcher/runtime_matcher.go delete mode 100644 service/veritification/error.go diff --git a/middleware/autorize.go b/middleware/autorize.go new file mode 100644 index 0000000..c9e3467 --- /dev/null +++ b/middleware/autorize.go @@ -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) + }) + } +} diff --git a/service/matcher/at_least_one_mather.go b/service/matcher/at_least_one_mather.go deleted file mode 100644 index e781b92..0000000 --- a/service/matcher/at_least_one_mather.go +++ /dev/null @@ -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 -} diff --git a/service/matcher/cacheable_matcher.go b/service/matcher/cacheable_matcher.go deleted file mode 100644 index 7767938..0000000 --- a/service/matcher/cacheable_matcher.go +++ /dev/null @@ -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 -} diff --git a/service/matcher/matcher.go b/service/matcher/matcher.go deleted file mode 100644 index dcedaf1..0000000 --- a/service/matcher/matcher.go +++ /dev/null @@ -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}, - } -} diff --git a/service/matcher/matcher_test.go b/service/matcher/matcher_test.go deleted file mode 100644 index b4651c3..0000000 --- a/service/matcher/matcher_test.go +++ /dev/null @@ -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) - } - } -} diff --git a/service/matcher/runtime_matcher.go b/service/matcher/runtime_matcher.go deleted file mode 100644 index 3189411..0000000 --- a/service/matcher/runtime_matcher.go +++ /dev/null @@ -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 -} diff --git a/service/veritification/error.go b/service/veritification/error.go deleted file mode 100644 index b5cc9d4..0000000 --- a/service/veritification/error.go +++ /dev/null @@ -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") -)