Authz is an authorization middleware for Tango
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Yang Luo be858f7101 Update casbin's repo. 4 years ago
examples Add keyMatch model testing. 4 years ago
.gitignore Ignore IDEA files. 4 years ago
.travis.yml Add coverage badge. 4 years ago
LICENSE Initial commit 4 years ago
README.md Update casbin's repo. 4 years ago
authz.go Update casbin's repo. 4 years ago
authz_test.go Update casbin's repo. 4 years ago

README.md

authz Build Status Coverage Status GoDoc

authz is an authorization middleware for Tango, it's based on https://github.com/casbin/casbin.

Installation

go get github.com/tango-contrib/authz

Simple Example

package main

import (
	"github.com/casbin/casbin"
	"github.com/lunny/tango"
	"github.com/tango-contrib/authz"
	"github.com/tango-contrib/session"
)

func main() {
	tg := tango.Classic()
	sessions := session.New()

	tg.Use(tango.HandlerFunc(func(ctx *tango.Context) {
		sess := sessions.Session(ctx.Req(), ctx.ResponseWriter)
		sess.Set("casbin_user", "user's name")
		ctx.Next()
	}))

	// load the casbin model and policy from files, database is also supported.
	e := casbin.NewEnforcer("examples/basic_model.conf", "examples/basic_policy.csv")
	tg.Use(authz.Auth(&e, sessions))
	
	// define the routers
	// the access that is denied by authz will return "You have no permission to visit this page"
	tg.Any("*", func() string {
	    // the access is permitted when got here
		return "You have the correct permission"
	})

	tg.Run()
}

Getting Help

License

This project is under MIT License. See the LICENSE file for the full license text.