-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathauthorizer.go
46 lines (39 loc) · 1.13 KB
/
authorizer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package authorizer
import (
"github.com/openinfradev/tks-api/internal/repository"
"net/http"
)
type Interface interface {
WithAuthorization(handler http.Handler) http.Handler
}
type filterFunc func(http.Handler, repository.Repository) http.Handler
type defaultAuthorization struct {
repo repository.Repository
filters []filterFunc
}
func NewDefaultAuthorization(repo repository.Repository) *defaultAuthorization {
d := &defaultAuthorization{
repo: repo,
}
d.addFilters(ABACFilter)
d.addFilters(PasswordFilter)
//d.addFilters(RBACFilter)
//d.addFilters(RBACFilterWithEndpoint)
d.addFilters(AdminApiFilter)
return d
}
func (a *defaultAuthorization) WithAuthorization(handler http.Handler) http.Handler {
compositeFilter := combineFilters(a.filters...)
return compositeFilter(handler, a.repo)
}
func (a *defaultAuthorization) addFilters(filters ...filterFunc) {
a.filters = append(a.filters, filters...)
}
func combineFilters(filters ...filterFunc) filterFunc {
return func(handler http.Handler, repo repository.Repository) http.Handler {
for _, filter := range filters {
handler = filter(handler, repo)
}
return handler
}
}