Skip to content

Commit 4db66e1

Browse files
rolandjitsuvishr
authored andcommitted
Upgrade to casbin v2 and close #10 (#11)
1 parent f5babd9 commit 4db66e1

File tree

5 files changed

+44
-19
lines changed

5 files changed

+44
-19
lines changed

casbin/broken_auth_model.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[request_definition]
2+
r = sub, obj, act
3+
4+
[policy_definition]
5+
p = sub, obj, act
6+
7+
[role_definition]
8+
g = _, _
9+
10+
[policy_effect]
11+
e = some(where (p.eft == allow))
12+
13+
[matchers]
14+
m = g(, p.sub) && keyMatch(r.obj, p.obj) && (r.act == p.act || p.act == "*")

casbin/casbin.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ Simple example:
55
package main
66
77
import (
8-
"github.com/casbin/casbin"
8+
"github.com/casbin/casbin/v2"
99
"github.com/labstack/echo/v4"
10-
"github.com/labstack/echo-contrib/casbin" casbin-mw
10+
casbin_mw "github.com/labstack/echo-contrib/casbin"
1111
)
1212
1313
func main() {
1414
e := echo.New()
1515
1616
// Mediate the access for every request
17-
e.Use(casbin-mw.Middleware(casbin.NewEnforcer("auth_model.conf", "auth_policy.csv")))
17+
e.Use(casbin_mw.Middleware(casbin.NewEnforcer("auth_model.conf", "auth_policy.csv")))
1818
1919
e.Logger.Fatal(e.Start(":1323"))
2020
}
@@ -24,19 +24,19 @@ Advanced example:
2424
package main
2525
2626
import (
27-
"github.com/casbin/casbin"
27+
"github.com/casbin/casbin/v2"
2828
"github.com/labstack/echo/v4"
29-
"github.com/labstack/echo-contrib/casbin" casbin-mw
29+
casbin_mw "github.com/labstack/echo-contrib/casbin"
3030
)
3131
3232
func main() {
33-
ce := casbin.NewEnforcer("auth_model.conf", "")
33+
ce, _ := casbin.NewEnforcer("auth_model.conf", "")
3434
ce.AddRoleForUser("alice", "admin")
3535
ce.AddPolicy(...)
3636
3737
e := echo.New()
3838
39-
echo.Use(casbin-mw.Middleware(ce))
39+
e.Use(casbin_mw.Middleware(ce))
4040
4141
e.Logger.Fatal(e.Start(":1323"))
4242
}
@@ -45,7 +45,9 @@ Advanced example:
4545
package casbin
4646

4747
import (
48-
"github.com/casbin/casbin"
48+
"net/http"
49+
50+
"github.com/casbin/casbin/v2"
4951
"github.com/labstack/echo/v4"
5052
"github.com/labstack/echo/v4/middleware"
5153
)
@@ -89,8 +91,14 @@ func MiddlewareWithConfig(config Config) echo.MiddlewareFunc {
8991

9092
return func(next echo.HandlerFunc) echo.HandlerFunc {
9193
return func(c echo.Context) error {
92-
if config.Skipper(c) || config.CheckPermission(c) {
94+
if config.Skipper(c) {
95+
return next(c)
96+
}
97+
98+
if pass, err := config.CheckPermission(c); err == nil && pass {
9399
return next(c)
100+
} else if err != nil {
101+
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
94102
}
95103

96104
return echo.ErrForbidden
@@ -107,7 +115,7 @@ func (a *Config) GetUserName(c echo.Context) string {
107115

108116
// CheckPermission checks the user/method/path combination from the request.
109117
// Returns true (permission granted) or false (permission forbidden)
110-
func (a *Config) CheckPermission(c echo.Context) bool {
118+
func (a *Config) CheckPermission(c echo.Context) (bool, error) {
111119
user := a.GetUserName(c)
112120
method := c.Request().Method
113121
path := c.Request().URL.Path

casbin/casbin_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"net/http/httptest"
66
"testing"
77

8-
"github.com/casbin/casbin"
8+
"github.com/casbin/casbin/v2"
99
"github.com/labstack/echo/v4"
1010
)
1111

@@ -37,7 +37,7 @@ func testRequest(t *testing.T, ce *casbin.Enforcer, user string, path string, me
3737
}
3838

3939
func TestAuth(t *testing.T) {
40-
ce := casbin.NewEnforcer("auth_model.conf", "auth_policy.csv")
40+
ce, _ := casbin.NewEnforcer("auth_model.conf", "auth_policy.csv")
4141

4242
testRequest(t, ce, "alice", "/dataset1/resource1", echo.GET, 200)
4343
testRequest(t, ce, "alice", "/dataset1/resource1", echo.POST, 200)
@@ -46,7 +46,7 @@ func TestAuth(t *testing.T) {
4646
}
4747

4848
func TestPathWildcard(t *testing.T) {
49-
ce := casbin.NewEnforcer("auth_model.conf", "auth_policy.csv")
49+
ce, _ := casbin.NewEnforcer("auth_model.conf", "auth_policy.csv")
5050

5151
testRequest(t, ce, "bob", "/dataset2/resource1", "GET", 200)
5252
testRequest(t, ce, "bob", "/dataset2/resource1", "POST", 200)
@@ -64,7 +64,7 @@ func TestPathWildcard(t *testing.T) {
6464
}
6565

6666
func TestRBAC(t *testing.T) {
67-
ce := casbin.NewEnforcer("auth_model.conf", "auth_policy.csv")
67+
ce, _ := casbin.NewEnforcer("auth_model.conf", "auth_policy.csv")
6868

6969
// cathy can access all /dataset1/* resources via all methods because it has the dataset1_admin role.
7070
testRequest(t, ce, "cathy", "/dataset1/item", "GET", 200)
@@ -84,3 +84,8 @@ func TestRBAC(t *testing.T) {
8484
testRequest(t, ce, "cathy", "/dataset2/item", "POST", 403)
8585
testRequest(t, ce, "cathy", "/dataset2/item", "DELETE", 403)
8686
}
87+
88+
func TestEnforceError(t *testing.T) {
89+
ce, _ := casbin.NewEnforcer("broken_auth_model.conf", "auth_policy.csv")
90+
testRequest(t, ce, "cathy", "/dataset1/item", "GET", 500)
91+
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
module github.com/labstack/echo-contrib
22

33
require (
4-
github.com/casbin/casbin v1.8.2
4+
github.com/casbin/casbin/v2 v2.0.0
5+
github.com/casbin/casbin/v2 v2.0.0
56
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
67
github.com/gorilla/context v1.1.1
78
github.com/gorilla/sessions v1.1.3

go.sum

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible h1:1G1pk05UrOh0NlF1oeaaix1x8XzrfjIDK47TY0Zehcw=
22
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
3-
github.com/casbin/casbin v1.8.2 h1:hJrnZxIXnsxyxQ8zvrUWYrR+MJK+J1X7sBeA5DuLh+o=
4-
github.com/casbin/casbin v1.8.2/go.mod h1:z8uPsfBJGUsnkagrt3G8QvjgTKFMBJ32UP8HpZllfog=
5-
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd h1:qMd81Ts1T2OTKmB4acZcyKaMtRnY5Y44NuXGX2GFJ1w=
3+
github.com/casbin/casbin/v2 v2.0.0/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
64
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
75
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
86
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -44,7 +42,6 @@ github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6Kllzaw
4442
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
4543
github.com/valyala/fasttemplate v1.0.1 h1:tY9CJiPnMXf1ERmG2EyK7gNUd+c6RKGD0IfU8WdUSz8=
4644
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
47-
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
4845
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
4946
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
5047
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5 h1:58fnuSXlxZmFdJyvtTFVmVhcMLU6v5fEb/ok4wyqtNU=

0 commit comments

Comments
 (0)