Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the casbin middleware enforcer config customizable #58

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions casbin/casbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Advanced example:
package casbin

import (
"errors"
"net/http"

"github.com/casbin/casbin/v2"
Expand All @@ -60,7 +61,7 @@ type (

// Enforcer CasbinAuth main rule.
// Required.
Enforcer *casbin.Enforcer
Enforcer casbin.IEnforcer

// Method to get the username - defaults to using basic auth
UserGetter func(c echo.Context) (string, error)
Expand All @@ -82,7 +83,7 @@ var (
//
// For valid credentials it calls the next handler.
// For missing or invalid credentials, it sends "401 - Unauthorized" response.
func Middleware(ce *casbin.Enforcer) echo.MiddlewareFunc {
func Middleware(ce casbin.IEnforcer) echo.MiddlewareFunc {
c := DefaultConfig
c.Enforcer = ce
return MiddlewareWithConfig(c)
Expand Down Expand Up @@ -130,5 +131,9 @@ func (a *Config) CheckPermission(c echo.Context) (bool, error) {
}
method := c.Request().Method
path := c.Request().URL.Path
return a.Enforcer.Enforce(user, path, method)
if a.Enforcer != nil {
return a.Enforcer.Enforce(user, path, method)
} else {
return false, errors.New("casbin Enforcer is not set")
}
}
36 changes: 36 additions & 0 deletions casbin/casbin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/casbin/casbin/v2"
Expand Down Expand Up @@ -131,3 +132,38 @@ func TestUserGetterError(t *testing.T) {
})
testRequest(t, h, "cathy", "/dataset1/item", "GET", 403)
}

func TestSyncedEnforcer(t *testing.T) {
ce, _ := casbin.NewSyncedEnforcer("auth_model.conf", "auth_policy.csv")
h := Middleware(ce)(func(c echo.Context) error {
return c.String(http.StatusOK, "test")
})

testRequest(t, h, "alice", "/dataset1/resource1", echo.GET, 200)
testRequest(t, h, "alice", "/dataset1/resource1", echo.POST, 200)
testRequest(t, h, "alice", "/dataset1/resource2", echo.GET, 200)
testRequest(t, h, "alice", "/dataset1/resource2", echo.POST, 403)
}

func TestEnforcerNil(t *testing.T) {
cnf := Config{
Skipper: middleware.DefaultSkipper,
UserGetter: func(c echo.Context) (string, error) {
return "not_cathy_at_all", nil
},
}
h := MiddlewareWithConfig(cnf)(func(c echo.Context) error {
return c.String(http.StatusOK, "test")
})

e := echo.New()
req := httptest.NewRequest("GET", "/dataset1/item", nil)
req.SetBasicAuth("cathy", "secret")
res := httptest.NewRecorder()
c := e.NewContext(req, res)

if err := h(c); err == nil ||
!strings.Contains(err.Error(), "casbin Enforcer is not set") {
t.Error("expected the handler to fail with missing casbin Enforcer")
}
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ require (
github.com/uber/jaeger-client-go v2.25.0+incompatible
github.com/uber/jaeger-lib v2.4.0+incompatible // indirect
)