You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
847 B
Go
50 lines
847 B
Go
2 years ago
|
package endpointroles
|
||
|
|
||
|
import (
|
||
|
"jochum.dev/jo-micro/auth2"
|
||
|
"jochum.dev/jo-micro/auth2/internal/util"
|
||
|
)
|
||
|
|
||
|
// Add this on every Server that exposes RouterClientService
|
||
|
var RouterRule = NewRule(
|
||
|
Endpoint("RouterClientService.Routes"),
|
||
|
RolesAllow([]string{auth2.ROLE_SERVICE}),
|
||
|
)
|
||
|
|
||
|
type Rule struct {
|
||
|
Endpoint string
|
||
|
RolesAllow []string
|
||
|
RolesDeny []string
|
||
|
}
|
||
|
|
||
|
type RuleOption func(e *Rule)
|
||
|
|
||
|
func Endpoint(n interface{}) RuleOption {
|
||
|
return func(e *Rule) {
|
||
|
e.Endpoint = util.ReflectFunctionName(n)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func RolesAllow(n []string) RuleOption {
|
||
|
return func(e *Rule) {
|
||
|
e.RolesAllow = n
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func RolesDeny(n []string) RuleOption {
|
||
|
return func(e *Rule) {
|
||
|
e.RolesDeny = n
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func NewRule(opts ...RuleOption) Rule {
|
||
|
ep := Rule{
|
||
|
RolesAllow: []string{},
|
||
|
RolesDeny: []string{},
|
||
|
}
|
||
|
for _, o := range opts {
|
||
|
o(&ep)
|
||
|
}
|
||
|
return ep
|
||
|
}
|