-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorizer.go
50 lines (43 loc) · 945 Bytes
/
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
47
48
49
50
package auth
import (
"fmt"
"github.com/casbin/casbin"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Authorizer is a wrapper for a casbin enforcer.
type Authorizer struct {
enforcer *casbin.Enforcer
}
// New instantiates a new authorizer using the model
// and policy specified in the files stored in the
// given model and policy file paths.
func New(model, policy string) *Authorizer {
enforcer := casbin.NewEnforcer(model, policy)
return &Authorizer{
enforcer: enforcer,
}
}
// Authorize verifies if a given subject is
// permitted to run a given action on a given
// object.
func (a *Authorizer) Authorize(
subject string,
object string,
action string,
) error {
if !a.enforcer.Enforce(
subject,
object,
action) {
msg := fmt.Sprintf(
"%s not permitted to %s to %s",
subject,
action,
object,
)
st := status.New(codes.PermissionDenied, msg)
return st.Err()
}
return nil
}