Support customization of action and object getter#65
Support customization of action and object getter#65suzaku wants to merge 1 commit intolabstack:masterfrom
Conversation
|
I have created #66 instead of this solution. It will be more generic as it allows to choose enforcers. Actions/Methods can be changed before ce, _ := casbin.NewEnforcer("auth_model.conf", "auth_policy.csv")
cnf := Config{
EnforceHandler: func(c echo.Context, user string) (bool, error) {
method := c.Request().Method
if strings.HasPrefix(c.Request().URL.Path, "/user/bob") {
method += "_SELF"
}
return ce.Enforce(user, c.Request().URL.Path, method)
},
} |
Cool, very flexible. I have 2 questions though.
|
compare ActionGetter: func(c echo.Context) string {
method := c.Request().Method
if strings.HasPrefix(c.Request().URL.Path, "/user/bob") {
method += "_SELF"
}
return method
},with EnforceHandler: func(c echo.Context, user string) (bool, error) {
method := c.Request().Method
if strings.HasPrefix(c.Request().URL.Path, "/user/bob") {
method += "_SELF"
}
return ce.Enforce(user, c.Request().URL.Path, method)
},pretty much the same but latter allows things like #58 need If you need completely custom implementation. Then this would be bare minimum e := echo.New()
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
ce, _ := casbin.NewEnforcer("auth_model.conf", "auth_policy.csv")
return func(c echo.Context) error {
username, _, _ := c.Request().BasicAuth()
ok, err := ce.Enforce(username, c.Request().URL.Path, c.Request().Method)
if err != nil {
return err
}
if !ok {
return echo.ErrForbidden
}
return next(c)
}
}) |
|
Yes, the real problem is that this middleware is indeed very simple, so instead of making it customizable, maybe one should just implement it in whatever way suitable in their own project. Thanks for your clarification. |
Sometimes the Casbin policies don't match perfectly with the HTTP request paths and methods, this middleware could be more useful if it allows the customization of these two entities.