2020-07-22 14:50:08 +00:00
|
|
|
// 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 (
|
2020-11-26 09:48:29 +00:00
|
|
|
"net/http"
|
2020-12-12 13:28:58 +00:00
|
|
|
|
|
|
|
"github.com/casbin/casbin"
|
|
|
|
|
|
|
|
beego "github.com/astaxie/beego/adapter"
|
|
|
|
"github.com/astaxie/beego/adapter/context"
|
|
|
|
beecontext "github.com/astaxie/beego/server/web/context"
|
|
|
|
"github.com/astaxie/beego/server/web/filter/authz"
|
2020-07-22 14:50:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewAuthorizer returns the authorizer.
|
|
|
|
// Use a casbin enforcer as input
|
2020-11-26 09:48:29 +00:00
|
|
|
func NewAuthorizer(e *casbin.Enforcer) beego.FilterFunc {
|
2020-12-12 13:28:58 +00:00
|
|
|
f := authz.NewAuthorizer(e)
|
|
|
|
return func(context *context.Context) {
|
|
|
|
f((*beecontext.Context)(context))
|
2020-07-22 14:50:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BasicAuthorizer stores the casbin handler
|
2020-12-12 13:28:58 +00:00
|
|
|
type BasicAuthorizer authz.BasicAuthorizer
|
2020-07-22 14:50:08 +00:00
|
|
|
|
|
|
|
// GetUserName gets the user name from the request.
|
|
|
|
// Currently, only HTTP basic authentication is supported
|
|
|
|
func (a *BasicAuthorizer) GetUserName(r *http.Request) string {
|
2020-12-12 13:28:58 +00:00
|
|
|
return (*authz.BasicAuthorizer)(a).GetUserName(r)
|
2020-07-22 14:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2020-12-12 13:28:58 +00:00
|
|
|
return (*authz.BasicAuthorizer)(a).CheckPermission(r)
|
2020-07-22 14:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RequirePermission returns the 403 Forbidden to the client
|
|
|
|
func (a *BasicAuthorizer) RequirePermission(w http.ResponseWriter) {
|
2020-12-12 13:28:58 +00:00
|
|
|
(*authz.BasicAuthorizer)(a).RequirePermission(w)
|
2020-07-22 14:50:08 +00:00
|
|
|
}
|