Skip to content
Casbin RESTful adapter for Casbin https://github.com/casbin/casbin
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example
.travis.yml
README.md
casbinrest.go
casbinrest_test.go
go.mod first commit Jun 23, 2019
go.sum

README.md

Casbin RESTful Adapter on Echo Web Framework

Casbin RESTful adapter for Casbin https://github.com/casbin/casbin

Build Status Codecov Go Report Card

Installation:

go get github.com/prongbang/casbinrest

Usage:

package main

import (
	"fmt"

	"github.com/casbin/casbin"
	"github.com/labstack/echo"
	"github.com/prongbang/casbinrest"
)

type redisDataSource struct {
}

func NewRedisDataSource() casbinrest.DataSource {
	return &redisDataSource{}
}

const mockAdminToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"

func (r *redisDataSource) GetRoleByToken(reqToken string) string {
	role := "anonymous"
	if reqToken == mockAdminToken {
		role = "admin"
	}
	return role
}

func main() {
    redisSource := NewRedisDataSource()
    ce := casbin.NewEnforcer("example/auth_model.conf", "example/policy.csv")

	e := echo.New()
    e.Use(casbinrest.Middleware(ce, redisSource))
    
	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(":1323"))
}
You can’t perform that action at this time.