fix+feat:withconfig but not set usersetter+domain rbac#61
Closed
Jarnpher553 wants to merge 1 commit intolabstack:masterfrom
Jarnpher553:master
Closed
fix+feat:withconfig but not set usersetter+domain rbac#61Jarnpher553 wants to merge 1 commit intolabstack:masterfrom Jarnpher553:master
Jarnpher553 wants to merge 1 commit intolabstack:masterfrom
Jarnpher553:master
Conversation
Contributor
|
If do not think that logic for matching domain is best choice because of I propose adding more generic field for custom Enforce handling that you could implement for your needs. type (
// Config defines the config for CasbinAuth middleware.
Config struct {
// Skipper defines a function to skip middleware.
Skipper middleware.Skipper
// Enforcer CasbinAuth main rule.
// One of Enforcer or EnforceHandler fields is required.
Enforcer *casbin.Enforcer
// EnforceHandler is custom callback to handle enforcing.
// One of Enforcer or EnforceHandler fields is required.
EnforceHandler func(c echo.Context, user string) (bool, error)
// Method to get the username - defaults to using basic auth
UserGetter func(c echo.Context) (string, error)
// Method to handle errors
ErrorHandler func(c echo.Context, internal error, proposedStatus int) error
}
)
var (
// DefaultConfig is the default CasbinAuth middleware config.
DefaultConfig = Config{
Skipper: middleware.DefaultSkipper,
UserGetter: func(c echo.Context) (string, error) {
username, _, _ := c.Request().BasicAuth()
return username, nil
},
ErrorHandler: func(c echo.Context, internal error, proposedStatus int) error {
err := echo.NewHTTPError(proposedStatus, internal.Error())
err.Internal = internal
return err
},
}
)
// Middleware returns a CasbinAuth middleware.
//
// For valid credentials it calls the next handler.
// For missing or invalid credentials, it sends "401 - Unauthorized" response.
func Middleware(ce *casbin.Enforcer) echo.MiddlewareFunc {
c := DefaultConfig
c.Enforcer = ce
return MiddlewareWithConfig(c)
}
// MiddlewareWithConfig returns a CasbinAuth middleware with config.
// See `Middleware()`.
func MiddlewareWithConfig(config Config) echo.MiddlewareFunc {
if config.Enforcer == nil && config.EnforceHandler == nil {
panic("one of casbin middleware Enforcer or EnforceHandler fields must be set")
}
if config.Skipper == nil {
config.Skipper = DefaultConfig.Skipper
}
if config.UserGetter == nil {
config.UserGetter = DefaultConfig.UserGetter
}
if config.ErrorHandler == nil {
config.ErrorHandler = DefaultConfig.ErrorHandler
}
if config.EnforceHandler == nil {
config.EnforceHandler = func(c echo.Context, user string) (bool, error) {
return config.Enforcer.Enforce(user, c.Request().URL.Path, c.Request().Method)
}
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if config.Skipper(c) {
return next(c)
}
user, err := config.UserGetter(c)
if err != nil {
return config.ErrorHandler(c, err, http.StatusForbidden)
}
pass, err := config.EnforceHandler(c, user)
if err != nil {
return config.ErrorHandler(c, err, http.StatusInternalServerError)
}
if !pass {
return config.ErrorHandler(c, errors.New("enforce did not pass"), http.StatusForbidden)
}
return next(c)
}
}
} |
Author
|
thank you for your's reply and example code |
Contributor
|
@Jarnpher553 this PR goes on in #66 where I add |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix bug and add feature.
if use this method, but can not set UserGetter, will get a panic at runtime
add some custom handler to make this middleware greater!!