casbinmw

package module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 11, 2020 License: MIT Imports: 5 Imported by: 0

README

echo-middleware-casbin

An Echo middleware to authenticate a user through casbin.

Install
go get -u github.com/reedom/echo-middleware-casbin
Usage
package main

import (
	"net/http"

	"github.com/casbin/casbin"
	"github.com/labstack/echo"
	casbinmw "github.com/reedom/echo-middleware-casbin"
)

// casbinDataSource is a datasource for the middleware.
type casbinDataSource struct {
}

// GetSubject gets a subject from echo.Context.
// In this sample, it expects other middleware has set a user name at "user".
func (r *casbinDataSource) GetSubject(c echo.Context) string {
	return c.Get("user").(string)
}

// Introduce another middleware which extracts the accessor's user name and set
// it to "user" in echo.Context.
func setUserMiddleware() echo.MiddlewareFunc {
	return func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) error {
			user := // ... extract the user name somehow.
			c.Set("user", user)
			return next(c)
		}
	}
}

func main() {
	e := echo.New()

	ce, err := casbin.NewEnforcer("auth_model.conf", "auth_policy.conf")
	if err != nil {
		e.Logger.Fatal(err)
	}

	e.Use(setUserMiddleware())
	e.Use(casbinmw.Middleware(ce, &casbinDataSource{}))

	e.GET("/", func(c echo.Context) error {
		return c.JSON(http.StatusOK, "OK")
	})

	e.GET("/login", func(c echo.Context) error {
		return c.JSON(http.StatusOK, "OK")
	})

	e.Logger.Fatal(e.Start(":8080"))
}
License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultConfig = Config{
		Skipper: middleware.DefaultSkipper,
	}
)

Functions

func Middleware

func Middleware(ce *casbin.Enforcer, ds DataSource) echo.MiddlewareFunc

Middleware returns a Echo middleware.

func MiddlewareWithConfig

func MiddlewareWithConfig(config Config) echo.MiddlewareFunc

MiddlewareWithConfig returns an Echo middleware with config.

Types

type Config

type Config struct {
	// Skipper defines a function to skip middleware.
	Skipper middleware.Skipper
	// BeforeFunc defines a function which is executed just before the middleware.
	BeforeFunc middleware.BeforeFunc
	// GetURLPathFunc defines a function which return the requested URL.
	GetURLPathFunc func(ctx echo.Context) string
	// SuccessHandler defines a function which is executed for a granted access.
	SuccessHandler func(echo.Context)
	// ErrorHandler defines a function which is executed for a rejected access.
	// It may be used to define a custom error.
	ErrorHandler func(error, echo.Context) error
	// Enforcer instance.
	Enforcer *casbin.Enforcer
	// DataSource is the interface that extract a subject from echo.Context.
	DataSource DataSource
}

Config defines the config for this middleware.

func (*Config) GetSubject

func (a *Config) GetSubject(c echo.Context) string

GetSubject extract a subject from the request.

func (*Config) HasPermission

func (a *Config) HasPermission(c echo.Context, urlPath string) (bool, error)

HasPermission checks a resource access permission against casbin with the subject/method/path combination from the request. Returns true (permission granted) or false (permission forbidden).

type DataSource

type DataSource interface {
	GetSubject(c echo.Context) string
}

DataSource is the interface that extract a subject from echo.Context.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL