mirror of
https://github.com/astaxie/beego.git
synced 2025-06-25 23:20:19 +00:00
Revert "Merge pull request #4254 from astaxie/develop-2.0"
This reverts commite284b0ddae
, reversing changes made to8ef8fd2606
.
This commit is contained in:
.github/workflows
.gitignore.travis.ymlCONTRIBUTING.mdREADME.mdadapter
admin.goapp.gobeego.gobuild_info.go
admin.goadmin_test.goadminui.goapp.gobeego.gobuild_info.gocache
config.goconfig
context
controller.godoc.goerror.goflash.gofs.gograce
httplib
logs
migration
namespace.goorm
cmd.godb.godb_alias.gomodels.gomodels_boot.gomodels_fields.goorm.goorm_conds.goorm_log.goorm_queryset.goqb.goqb_mysql.goquery_setter_adapter.gotypes.goutils.go
plugins
policy.gorouter.gosession
couchbase
ledis
memcache
mysql
postgres
provider_adapter.goredis
redis_cluster
redis_sentinel
sess_cookie.gosess_file.gosess_file_test.gosess_mem.gosess_test.gosess_utils.gosession.gossdb
store_adapter.goswagger
template.gotemplatefunc.gotemplatefunc_test.gotesting
toolbox
tree.gotree_test.goutils
validation
cache
client
cache
httplib
orm
config
config_test.gocontext
acceptencoder.goacceptencoder_test.gocontext.gocontext_test.goinput.goinput_test.gooutput.go
controller.gocontroller_test.goparam
renderer.goresponse.gocore
bean
context.godoc.gofactory.gometadata.gotag_auto_wire_bean_factory.gotag_auto_wire_bean_factory_test.gotime_type_adapter.gotime_type_adapter_test.gotype_adapter.go
config
governor
logs
access_log_test.goconn_test.go
es
formatter.goformatter_test.gojianliao_test.golog_msg.golog_msg_test.golog_test.goslack.goutils
caller_test.godebug_test.gokv.gokv_test.gomail_test.go
pagination
rand_test.gosafemap_test.goslice_test.gotime.govalidation
grace
hooks.gohttplib
log.gologs
README.mdaccesslog.go
alils
alils.goconfig.golog.pb.golog_config.golog_project.golog_store.gomachine_group.gorequest.gosignature.go
conn.goconn_test.goconsole.goconsole_test.goes
file.gofile_test.gojianliao.golog.gologger.gologger_test.gomultifile.gomultifile_test.goslack.gosmtp.gosmtp_test.gometric
migration
mime.gonamespace.gonamespace_test.goorm
README.mdcmd.gocmd_utils.godb.godb_alias.godb_mysql.godb_oracle.godb_postgres.godb_sqlite.godb_tables.godb_tidb.godb_utils.gomodels.gomodels_boot.gomodels_fields.gomodels_info_f.gomodels_info_m.gomodels_test.gomodels_utils.goorm.goorm_conds.goorm_log.goorm_object.goorm_querym2m.goorm_queryset.goorm_raw.goorm_test.goqb.goqb_mysql.goqb_tidb.gotypes.goutils.goutils_test.go
parser.goplugins
apiauth
auth
authz
cors
scripts
server/web
LICENSEadmin.goadmin_controller.goadmin_test.gofilter_chain_test.goserver.goserver_test.gostatistics_test.go
captcha
context
doc.gofilter.gofilter
apiauth
authz
opentracing
prometheus
session
couchbase
ledis
redis
redis_cluster
redis_sentinel
sess_cookie_test.gosess_file_test.gosess_mem_test.gossdb
session
README.md
staticfile.gostaticfile_test.gocouchbase
ledis
memcache
mysql
postgres
redis
redis_cluster
redis_sentinel
sess_cookie.gosess_cookie_test.gosess_file.gosess_mem.gosess_mem_test.gosess_test.gosess_utils.gosession.gossdb
swagger
task
template.gotemplate_test.gotemplatefunc.gotemplatefunc_test.gotestdata
testing
toolbox
tree.gotree_test.gounregroute_test.goutils
caller.gocaller_test.go
captcha
debug.godebug_test.gofile.gofile_test.gomail.gomail_test.gopagination
rand.gorand_test.gosafemap.gosafemap_test.goslice.goslice_test.gotestdata
utils.goutils_test.govalidation
86
plugins/authz/authz.go
Normal file
86
plugins/authz/authz.go
Normal file
@ -0,0 +1,86 @@
|
||||
// Copyright 2014 beego Author. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package authz provides handlers to enable ACL, RBAC, ABAC authorization support.
|
||||
// Simple Usage:
|
||||
// import(
|
||||
// "github.com/astaxie/beego"
|
||||
// "github.com/astaxie/beego/plugins/authz"
|
||||
// "github.com/casbin/casbin"
|
||||
// )
|
||||
//
|
||||
// func main(){
|
||||
// // mediate the access for every request
|
||||
// beego.InsertFilter("*", beego.BeforeRouter, authz.NewAuthorizer(casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")))
|
||||
// beego.Run()
|
||||
// }
|
||||
//
|
||||
//
|
||||
// Advanced Usage:
|
||||
//
|
||||
// func main(){
|
||||
// e := casbin.NewEnforcer("authz_model.conf", "")
|
||||
// e.AddRoleForUser("alice", "admin")
|
||||
// e.AddPolicy(...)
|
||||
//
|
||||
// beego.InsertFilter("*", beego.BeforeRouter, authz.NewAuthorizer(e))
|
||||
// beego.Run()
|
||||
// }
|
||||
package authz
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/astaxie/beego/context"
|
||||
"github.com/casbin/casbin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// NewAuthorizer returns the authorizer.
|
||||
// Use a casbin enforcer as input
|
||||
func NewAuthorizer(e *casbin.Enforcer) beego.FilterFunc {
|
||||
return func(ctx *context.Context) {
|
||||
a := &BasicAuthorizer{enforcer: e}
|
||||
|
||||
if !a.CheckPermission(ctx.Request) {
|
||||
a.RequirePermission(ctx.ResponseWriter)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// BasicAuthorizer stores the casbin handler
|
||||
type BasicAuthorizer struct {
|
||||
enforcer *casbin.Enforcer
|
||||
}
|
||||
|
||||
// GetUserName gets the user name from the request.
|
||||
// Currently, only HTTP basic authentication is supported
|
||||
func (a *BasicAuthorizer) GetUserName(r *http.Request) string {
|
||||
username, _, _ := r.BasicAuth()
|
||||
return username
|
||||
}
|
||||
|
||||
// CheckPermission checks the user/method/path combination from the request.
|
||||
// Returns true (permission granted) or false (permission forbidden)
|
||||
func (a *BasicAuthorizer) CheckPermission(r *http.Request) bool {
|
||||
user := a.GetUserName(r)
|
||||
method := r.Method
|
||||
path := r.URL.Path
|
||||
return a.enforcer.Enforce(user, path, method)
|
||||
}
|
||||
|
||||
// RequirePermission returns the 403 Forbidden to the client
|
||||
func (a *BasicAuthorizer) RequirePermission(w http.ResponseWriter) {
|
||||
w.WriteHeader(403)
|
||||
w.Write([]byte("403 Forbidden\n"))
|
||||
}
|
14
plugins/authz/authz_model.conf
Normal file
14
plugins/authz/authz_model.conf
Normal file
@ -0,0 +1,14 @@
|
||||
[request_definition]
|
||||
r = sub, obj, act
|
||||
|
||||
[policy_definition]
|
||||
p = sub, obj, act
|
||||
|
||||
[role_definition]
|
||||
g = _, _
|
||||
|
||||
[policy_effect]
|
||||
e = some(where (p.eft == allow))
|
||||
|
||||
[matchers]
|
||||
m = g(r.sub, p.sub) && keyMatch(r.obj, p.obj) && (r.act == p.act || p.act == "*")
|
7
plugins/authz/authz_policy.csv
Normal file
7
plugins/authz/authz_policy.csv
Normal file
@ -0,0 +1,7 @@
|
||||
p, alice, /dataset1/*, GET
|
||||
p, alice, /dataset1/resource1, POST
|
||||
p, bob, /dataset2/resource1, *
|
||||
p, bob, /dataset2/resource2, GET
|
||||
p, bob, /dataset2/folder1/*, POST
|
||||
p, dataset1_admin, /dataset1/*, *
|
||||
g, cathy, dataset1_admin
|
|
107
plugins/authz/authz_test.go
Normal file
107
plugins/authz/authz_test.go
Normal file
@ -0,0 +1,107 @@
|
||||
// Copyright 2014 beego Author. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package authz
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/astaxie/beego/context"
|
||||
"github.com/astaxie/beego/plugins/auth"
|
||||
"github.com/casbin/casbin"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func testRequest(t *testing.T, handler *beego.ControllerRegister, user string, path string, method string, code int) {
|
||||
r, _ := http.NewRequest(method, path, nil)
|
||||
r.SetBasicAuth(user, "123")
|
||||
w := httptest.NewRecorder()
|
||||
handler.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) {
|
||||
handler := beego.NewControllerRegister()
|
||||
|
||||
handler.InsertFilter("*", beego.BeforeRouter, auth.Basic("alice", "123"))
|
||||
handler.InsertFilter("*", beego.BeforeRouter, NewAuthorizer(casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")))
|
||||
|
||||
handler.Any("*", func(ctx *context.Context) {
|
||||
ctx.Output.SetStatus(200)
|
||||
})
|
||||
|
||||
testRequest(t, handler, "alice", "/dataset1/resource1", "GET", 200)
|
||||
testRequest(t, handler, "alice", "/dataset1/resource1", "POST", 200)
|
||||
testRequest(t, handler, "alice", "/dataset1/resource2", "GET", 200)
|
||||
testRequest(t, handler, "alice", "/dataset1/resource2", "POST", 403)
|
||||
}
|
||||
|
||||
func TestPathWildcard(t *testing.T) {
|
||||
handler := beego.NewControllerRegister()
|
||||
|
||||
handler.InsertFilter("*", beego.BeforeRouter, auth.Basic("bob", "123"))
|
||||
handler.InsertFilter("*", beego.BeforeRouter, NewAuthorizer(casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")))
|
||||
|
||||
handler.Any("*", func(ctx *context.Context) {
|
||||
ctx.Output.SetStatus(200)
|
||||
})
|
||||
|
||||
testRequest(t, handler, "bob", "/dataset2/resource1", "GET", 200)
|
||||
testRequest(t, handler, "bob", "/dataset2/resource1", "POST", 200)
|
||||
testRequest(t, handler, "bob", "/dataset2/resource1", "DELETE", 200)
|
||||
testRequest(t, handler, "bob", "/dataset2/resource2", "GET", 200)
|
||||
testRequest(t, handler, "bob", "/dataset2/resource2", "POST", 403)
|
||||
testRequest(t, handler, "bob", "/dataset2/resource2", "DELETE", 403)
|
||||
|
||||
testRequest(t, handler, "bob", "/dataset2/folder1/item1", "GET", 403)
|
||||
testRequest(t, handler, "bob", "/dataset2/folder1/item1", "POST", 200)
|
||||
testRequest(t, handler, "bob", "/dataset2/folder1/item1", "DELETE", 403)
|
||||
testRequest(t, handler, "bob", "/dataset2/folder1/item2", "GET", 403)
|
||||
testRequest(t, handler, "bob", "/dataset2/folder1/item2", "POST", 200)
|
||||
testRequest(t, handler, "bob", "/dataset2/folder1/item2", "DELETE", 403)
|
||||
}
|
||||
|
||||
func TestRBAC(t *testing.T) {
|
||||
handler := beego.NewControllerRegister()
|
||||
|
||||
handler.InsertFilter("*", beego.BeforeRouter, auth.Basic("cathy", "123"))
|
||||
e := casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")
|
||||
handler.InsertFilter("*", beego.BeforeRouter, NewAuthorizer(e))
|
||||
|
||||
handler.Any("*", func(ctx *context.Context) {
|
||||
ctx.Output.SetStatus(200)
|
||||
})
|
||||
|
||||
// cathy can access all /dataset1/* resources via all methods because it has the dataset1_admin role.
|
||||
testRequest(t, handler, "cathy", "/dataset1/item", "GET", 200)
|
||||
testRequest(t, handler, "cathy", "/dataset1/item", "POST", 200)
|
||||
testRequest(t, handler, "cathy", "/dataset1/item", "DELETE", 200)
|
||||
testRequest(t, handler, "cathy", "/dataset2/item", "GET", 403)
|
||||
testRequest(t, handler, "cathy", "/dataset2/item", "POST", 403)
|
||||
testRequest(t, handler, "cathy", "/dataset2/item", "DELETE", 403)
|
||||
|
||||
// delete all roles on user cathy, so cathy cannot access any resources now.
|
||||
e.DeleteRolesForUser("cathy")
|
||||
|
||||
testRequest(t, handler, "cathy", "/dataset1/item", "GET", 403)
|
||||
testRequest(t, handler, "cathy", "/dataset1/item", "POST", 403)
|
||||
testRequest(t, handler, "cathy", "/dataset1/item", "DELETE", 403)
|
||||
testRequest(t, handler, "cathy", "/dataset2/item", "GET", 403)
|
||||
testRequest(t, handler, "cathy", "/dataset2/item", "POST", 403)
|
||||
testRequest(t, handler, "cathy", "/dataset2/item", "DELETE", 403)
|
||||
}
|
Reference in New Issue
Block a user