Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
anonymous committed Oct 11, 2020
1 parent 920709e commit 34a4e3e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 11 deletions.
6 changes: 5 additions & 1 deletion adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import (
)

const (
RoleKey = "roles"
// RoleKey default
RoleKey = "roles"
// RoleAnonymous anonymous
RoleAnonymous = "anonymous"
)

// Adapter interface for implements GetRoleByToken
type Adapter interface {
GetRoleByToken(reqToken string) []string
}
Expand All @@ -31,6 +34,7 @@ func (r *roleAdapter) GetRoleByToken(reqToken string) []string {
return s
}

// NewRoleAdapter create adapter
func NewRoleAdapter(secret string) Adapter {
return &roleAdapter{
Secret: []byte(secret),
Expand Down
6 changes: 4 additions & 2 deletions common.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package fibercasbinrest

import (
"github.com/dgrijalva/jwt-go"
"log"

"github.com/dgrijalva/jwt-go"
)

// GetValue for get payload from JWT
func GetValue(reqToken string, key string, secretKey []byte) interface{} {
token, err := jwt.Parse(reqToken, func(token *jwt.Token) (interface{}, error) {
return secretKey, nil
Expand All @@ -14,7 +16,7 @@ func GetValue(reqToken string, key string, secretKey []byte) interface{} {
return ""
}
claims, ok := token.Claims.(jwt.MapClaims)
if ok && token.Valid {
if ok && token.Valid {
uid := claims[key]
return uid
}
Expand Down
3 changes: 2 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package main

import (
"log"

"github.com/casbin/casbin/v2"
mongodbadapter "github.com/casbin/mongodb-adapter/v3"
"github.com/gofiber/fiber/v2"
fibercasbinrest "github.com/prongbang/fiber-casbinrest"
"log"
)

func main() {
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ module github.com/prongbang/fiber-casbinrest
go 1.15

require (
github.com/casbin/casbin v1.9.1 // indirect
github.com/casbin/casbin/v2 v2.13.1
github.com/casbin/mongodb-adapter/v3 v3.0.0
github.com/casbin/redis-adapter/v2 v2.0.1
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/gofiber/fiber/v2 v2.0.6
github.com/stretchr/testify v1.5.1 // indirect
github.com/stretchr/testify v1.6.1 // indirect
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ github.com/casbin/redis-adapter v1.0.0/go.mod h1:/24TmweFhs/gtowqy3/EyDrhZ2SsldJ
github.com/casbin/redis-adapter/v2 v2.0.1 h1:rGJJm4HDCH9J7bnqoBEJs34d47mBMauPIBPkFn7OITo=
github.com/casbin/redis-adapter/v2 v2.0.1/go.mod h1:rzLGLfOG1HQ+UmmuS2NR1fzTyug7hOd1M2I1SkfYAUo=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
Expand Down Expand Up @@ -88,6 +89,7 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
Expand All @@ -98,11 +100,14 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
Expand Down Expand Up @@ -163,3 +168,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
15 changes: 11 additions & 4 deletions middleware.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
package fibercasbinrest

import (
"github.com/casbin/casbin/v2"
"github.com/gofiber/fiber/v2"
"log"
"strings"

"github.com/casbin/casbin/v2"
"github.com/gofiber/fiber/v2"
)

type (
// Skipper middleware function
Skipper func(*fiber.Ctx) bool
Config struct {
// Config middleware model
Config struct {
Skipper Skipper
Enforcer *casbin.Enforcer
Adapter Adapter
}
)

var (
// DefaultConfig initial default config
DefaultConfig = Config{
Skipper: DefaultSkipper,
}
)

// DefaultSkipper create default skipper
func DefaultSkipper(*fiber.Ctx) bool {
return false
}

// New create middleware
func New(ce *casbin.Enforcer, adt Adapter) fiber.Handler {
c := DefaultConfig
c.Enforcer = ce
c.Adapter = adt
return middlewareWithConfig(c)
}

// NewDefault create middleware
func NewDefault(ce *casbin.Enforcer, secret string) fiber.Handler {
c := DefaultConfig
c.Enforcer = ce
Expand Down Expand Up @@ -68,7 +75,7 @@ func (a *Config) CheckPermissions(c *fiber.Ctx) bool {
allowed := false
for _, role := range roles {
result, err := a.Enforcer.Enforce(strings.ToLower(role), c.Path(), c.Method())
if result && err == nil {
if result && err == nil {
allowed = true
} else {
log.Println(err)
Expand Down

0 comments on commit 34a4e3e

Please sign in to comment.