-
Notifications
You must be signed in to change notification settings - Fork 2
/
authz_test.go
103 lines (84 loc) · 4.04 KB
/
authz_test.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package authz
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/casbin/casbin"
"gopkg.in/macaron.v1"
)
func testAuthzRequest(t *testing.T, m *macaron.Macaron, user string, path string, method string, code int) {
r, _ := http.NewRequest(method, path, nil)
r.SetBasicAuth(user, "123")
w := httptest.NewRecorder()
m.ServeHTTP(w, r)
if w.Code != code {
t.Errorf("%s, %s, %s: %d, supposed to be %d", user, path, method, w.Code, code)
}
}
func TestBasic(t *testing.T) {
m := macaron.New()
e := casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")
m.Use(Authorizer(e))
// Here we use HTTP basic authentication as the way to get the logged-in user name
// For simplicity, the credential is not verified, you should implement and use your own
// authentication before the authorization.
// In this example, we assume "alice:123" is a legal user.
m.Use(func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("Access allowed. "))
})
testAuthzRequest(t, m, "alice", "/dataset1/resource1", "GET", 200)
testAuthzRequest(t, m, "alice", "/dataset1/resource1", "POST", 200)
testAuthzRequest(t, m, "alice", "/dataset1/resource2", "GET", 200)
testAuthzRequest(t, m, "alice", "/dataset1/resource2", "POST", 403)
}
func TestPathWildcard(t *testing.T) {
m := macaron.New()
e := casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")
m.Use(Authorizer(e))
// Here we use HTTP basic authentication as the way to get the logged-in user name
// For simplicity, the credential is not verified, you should implement and use your own
// authentication before the authorization.
// In this example, we assume "alice:123" is a legal user.
m.Use(func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("Access allowed. "))
})
testAuthzRequest(t, m, "bob", "/dataset2/resource1", "GET", 200)
testAuthzRequest(t, m, "bob", "/dataset2/resource1", "POST", 200)
testAuthzRequest(t, m, "bob", "/dataset2/resource1", "DELETE", 200)
testAuthzRequest(t, m, "bob", "/dataset2/resource2", "GET", 200)
testAuthzRequest(t, m, "bob", "/dataset2/resource2", "POST", 403)
testAuthzRequest(t, m, "bob", "/dataset2/resource2", "DELETE", 403)
testAuthzRequest(t, m, "bob", "/dataset2/folder1/item1", "GET", 403)
testAuthzRequest(t, m, "bob", "/dataset2/folder1/item1", "POST", 200)
testAuthzRequest(t, m, "bob", "/dataset2/folder1/item1", "DELETE", 403)
testAuthzRequest(t, m, "bob", "/dataset2/folder1/item2", "GET", 403)
testAuthzRequest(t, m, "bob", "/dataset2/folder1/item2", "POST", 200)
testAuthzRequest(t, m, "bob", "/dataset2/folder1/item2", "DELETE", 403)
}
func TestRBAC(t *testing.T) {
m := macaron.New()
e := casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")
m.Use(Authorizer(e))
// Here we use HTTP basic authentication as the way to get the logged-in user name
// For simplicity, the credential is not verified, you should implement and use your own
// authentication before the authorization.
// In this example, we assume "alice:123" is a legal user.
m.Use(func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("Access allowed. "))
})
// cathy can access all /dataset1/* resources via all methods because it has the dataset1_admin role.
testAuthzRequest(t, m, "cathy", "/dataset1/item", "GET", 200)
testAuthzRequest(t, m, "cathy", "/dataset1/item", "POST", 200)
testAuthzRequest(t, m, "cathy", "/dataset1/item", "DELETE", 200)
testAuthzRequest(t, m, "cathy", "/dataset2/item", "GET", 403)
testAuthzRequest(t, m, "cathy", "/dataset2/item", "POST", 403)
testAuthzRequest(t, m, "cathy", "/dataset2/item", "DELETE", 403)
// delete all roles on user cathy, so cathy cannot access any resources now.
e.DeleteRolesForUser("cathy")
testAuthzRequest(t, m, "cathy", "/dataset1/item", "GET", 403)
testAuthzRequest(t, m, "cathy", "/dataset1/item", "POST", 403)
testAuthzRequest(t, m, "cathy", "/dataset1/item", "DELETE", 403)
testAuthzRequest(t, m, "cathy", "/dataset2/item", "GET", 403)
testAuthzRequest(t, m, "cathy", "/dataset2/item", "POST", 403)
testAuthzRequest(t, m, "cathy", "/dataset2/item", "DELETE", 403)
}