Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/prongbang/casbinrest
Casbin RESTful adapter for Casbin https://github.com/casbin/casbin
https://github.com/prongbang/casbinrest
adapter casbin echo-casbin golang role-api roles
Last synced: 3 months ago
JSON representation
Casbin RESTful adapter for Casbin https://github.com/casbin/casbin
- Host: GitHub
- URL: https://github.com/prongbang/casbinrest
- Owner: prongbang
- License: mit
- Created: 2019-06-23T05:06:43.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-01-12T10:21:48.000Z (about 3 years ago)
- Last Synced: 2024-10-10T19:11:09.198Z (3 months ago)
- Topics: adapter, casbin, echo-casbin, golang, role-api, roles
- Language: Go
- Homepage:
- Size: 15.6 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Casbin RESTful Adapter on Echo Web Framework
Casbin RESTful adapter for Casbin https://github.com/casbin/casbin
[![Build Status](http://img.shields.io/travis/prongbang/casbinrest.svg)](https://travis-ci.org/prongbang/casbinrest)
[![Codecov](https://img.shields.io/codecov/c/github/prongbang/casbinrest.svg)](https://codecov.io/gh/prongbang/casbinrest)
[![Go Report Card](https://goreportcard.com/badge/github.com/prongbang/casbinrest)](https://goreportcard.com/report/github.com/prongbang/casbinrest)## Installation:
```
go get github.com/prongbang/casbinrest
```## Usage:
```go
package mainimport (
"github.com/labstack/echo/v4"
"github.com/prongbang/casbinrest"
"net/http""github.com/casbin/casbin/v2"
)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"))
}
```## Request:
```
curl -i -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" http://localhost:1323/login
```