This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
authj_test.go
173 lines (148 loc) · 6.32 KB
/
authj_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package authj
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/casbin/casbin/v2"
"github.com/gin-gonic/gin"
)
func testAuthjRequest(t *testing.T, router http.Handler, user, path, method string, code int) {
r, _ := http.NewRequestWithContext(context.TODO(), method, path, http.NoBody)
r.SetBasicAuth(user, "123")
w := httptest.NewRecorder()
router.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) {
router := gin.New()
e, _ := casbin.NewEnforcer("authj_model.conf", "authj_policy.csv")
router.Use(func(context *gin.Context) {
ContextWithSubject(context, "alice")
})
router.Use(Authorizer(e, WithSubject(Subject)))
router.Any("/*anypath", func(c *gin.Context) {
c.Status(200)
})
testAuthjRequest(t, router, "alice", "/dataset1/resource1", "GET", 200)
testAuthjRequest(t, router, "alice", "/dataset1/resource1", "POST", 200)
testAuthjRequest(t, router, "alice", "/dataset1/resource2", "GET", 200)
testAuthjRequest(t, router, "alice", "/dataset1/resource2", "POST", 403)
}
func TestPathWildcard(t *testing.T) {
router := gin.New()
e, _ := casbin.NewEnforcer("authj_model.conf", "authj_policy.csv")
router.Use(func(context *gin.Context) {
ContextWithSubject(context, "bob")
})
router.Use(Authorizer(e, WithSubject(Subject)))
router.Any("/*anypath", func(c *gin.Context) {
c.Status(200)
})
testAuthjRequest(t, router, "bob", "/dataset2/resource1", "GET", 200)
testAuthjRequest(t, router, "bob", "/dataset2/resource1", "POST", 200)
testAuthjRequest(t, router, "bob", "/dataset2/resource1", "DELETE", 200)
testAuthjRequest(t, router, "bob", "/dataset2/resource2", "GET", 200)
testAuthjRequest(t, router, "bob", "/dataset2/resource2", "POST", 403)
testAuthjRequest(t, router, "bob", "/dataset2/resource2", "DELETE", 403)
testAuthjRequest(t, router, "bob", "/dataset2/folder1/item1", "GET", 403)
testAuthjRequest(t, router, "bob", "/dataset2/folder1/item1", "POST", 200)
testAuthjRequest(t, router, "bob", "/dataset2/folder1/item1", "DELETE", 403)
testAuthjRequest(t, router, "bob", "/dataset2/folder1/item2", "GET", 403)
testAuthjRequest(t, router, "bob", "/dataset2/folder1/item2", "POST", 200)
testAuthjRequest(t, router, "bob", "/dataset2/folder1/item2", "DELETE", 403)
}
func TestRBAC(t *testing.T) {
router := gin.New()
e, _ := casbin.NewEnforcer("authj_model.conf", "authj_policy.csv")
router.Use(func(context *gin.Context) {
ContextWithSubject(context, "cathy")
})
router.Use(Authorizer(e,
WithSubject(Subject),
WithErrorFallback(func(c *gin.Context, err error) {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"code": http.StatusInternalServerError,
"msg": "Permission validation errors occur!",
})
}),
WithForbiddenFallback(func(c *gin.Context) {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
"code": http.StatusForbidden,
"msg": "Permission denied!",
})
})),
)
router.Any("/*anypath", func(c *gin.Context) {
c.Status(200)
})
// cathy can access all /dataset1/* resources via all methods because it has the dataset1_admin role.
testAuthjRequest(t, router, "cathy", "/dataset1/item", "GET", 200)
testAuthjRequest(t, router, "cathy", "/dataset1/item", "POST", 200)
testAuthjRequest(t, router, "cathy", "/dataset1/item", "DELETE", 200)
testAuthjRequest(t, router, "cathy", "/dataset2/item", "GET", 403)
testAuthjRequest(t, router, "cathy", "/dataset2/item", "POST", 403)
testAuthjRequest(t, router, "cathy", "/dataset2/item", "DELETE", 403)
// delete all roles on user cathy, so cathy cannot access any resources now.
_, _ = e.DeleteRolesForUser("cathy")
testAuthjRequest(t, router, "cathy", "/dataset1/item", "GET", 403)
testAuthjRequest(t, router, "cathy", "/dataset1/item", "POST", 403)
testAuthjRequest(t, router, "cathy", "/dataset1/item", "DELETE", 403)
testAuthjRequest(t, router, "cathy", "/dataset2/item", "GET", 403)
testAuthjRequest(t, router, "cathy", "/dataset2/item", "POST", 403)
testAuthjRequest(t, router, "cathy", "/dataset2/item", "DELETE", 403)
}
func TestSkipAuthentication(t *testing.T) {
router := gin.New()
e, _ := casbin.NewEnforcer("authj_model.conf", "authj_policy.csv")
router.Use(func(context *gin.Context) {
ContextWithSubject(context, "cathy")
})
router.Use(Authorizer(e,
WithSubject(Subject),
WithErrorFallback(func(c *gin.Context, err error) {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"code": http.StatusInternalServerError,
"msg": "Permission validation errors occur!",
})
}),
WithForbiddenFallback(func(c *gin.Context) {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
"code": http.StatusForbidden,
"msg": "Permission denied!",
})
}),
WithSkipAuthentication(func(c *gin.Context) bool {
if c.Request.Method == http.MethodGet && c.Request.URL.Path == "/skip/authentication" {
return true
}
return false
}),
))
router.Any("/*anypath", func(c *gin.Context) {
c.Status(200)
})
// skip authentication
testAuthjRequest(t, router, "cathy", "/skip/authentication", "GET", 200)
testAuthjRequest(t, router, "cathy", "/skip/authentication", "POST", 403)
// cathy can access all /dataset1/* resources via all methods because it has the dataset1_admin role.
testAuthjRequest(t, router, "cathy", "/dataset1/item", "GET", 200)
testAuthjRequest(t, router, "cathy", "/dataset1/item", "POST", 200)
testAuthjRequest(t, router, "cathy", "/dataset1/item", "DELETE", 200)
testAuthjRequest(t, router, "cathy", "/dataset2/item", "GET", 403)
testAuthjRequest(t, router, "cathy", "/dataset2/item", "POST", 403)
testAuthjRequest(t, router, "cathy", "/dataset2/item", "DELETE", 403)
// delete all roles on user cathy, so cathy cannot access any resources now.
_, _ = e.DeleteRolesForUser("cathy")
testAuthjRequest(t, router, "cathy", "/dataset1/item", "GET", 403)
testAuthjRequest(t, router, "cathy", "/dataset1/item", "POST", 403)
testAuthjRequest(t, router, "cathy", "/dataset1/item", "DELETE", 403)
testAuthjRequest(t, router, "cathy", "/dataset2/item", "GET", 403)
testAuthjRequest(t, router, "cathy", "/dataset2/item", "POST", 403)
testAuthjRequest(t, router, "cathy", "/dataset2/item", "DELETE", 403)
}