Skip to content

Commit bcde4c7

Browse files
committed
Added session middleware, updated casbin middlware
Signed-off-by: Vishal Rana <[email protected]>
1 parent 998bf51 commit bcde4c7

File tree

6 files changed

+107
-25
lines changed

6 files changed

+107
-25
lines changed

Gopkg.lock

Lines changed: 25 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
## Packages
66

7-
- Casbin (https://github.com/casbin/casbin) by [@hsluoyz](https://github.com/hsluoyz)
7+
- `casbin` (https://github.com/casbin/casbin) by [@hsluoyz](https://github.com/hsluoyz)
8+
- `session` (Session middleware backed by https://github.com/gorilla/sessions) by [@vishr](https://github.com/vishr)

casbin/auth.go renamed to casbin/casbin.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Simple example:
1414
e := echo.New()
1515
1616
// Mediate the access for every request
17-
e.Use(casbin-mw.Auth(casbin.NewEnforcer("auth_model.conf", "auth_policy.csv")))
17+
e.Use(casbin-mw.Middleware(casbin.NewEnforcer("auth_model.conf", "auth_policy.csv")))
1818
1919
e.Logger.Fatal(e.Start(":1323"))
2020
}
@@ -36,7 +36,7 @@ Advanced example:
3636
3737
e := echo.New()
3838
39-
echo.Use(casbin-mw.Auth(ce))
39+
echo.Use(casbin-mw.Middleware(ce))
4040
4141
e.Logger.Fatal(e.Start(":1323"))
4242
}
@@ -51,39 +51,40 @@ import (
5151
)
5252

5353
type (
54-
// AuthConfig defines the config for CasbinAuth middleware.
55-
AuthConfig struct {
54+
// Config defines the config for CasbinAuth middleware.
55+
Config struct {
5656
// Skipper defines a function to skip middleware.
5757
Skipper middleware.Skipper
58+
5859
// Enforcer CasbinAuth main rule.
5960
// Required.
6061
Enforcer *casbin.Enforcer
6162
}
6263
)
6364

6465
var (
65-
// DefaultAuthConfig is the default CasbinAuth middleware config.
66-
DefaultAuthConfig = AuthConfig{
66+
// DefaultConfig is the default CasbinAuth middleware config.
67+
DefaultConfig = Config{
6768
Skipper: middleware.DefaultSkipper,
6869
}
6970
)
7071

71-
// Auth returns an Auth middleware.
72+
// Middleware returns a CasbinAuth middleware.
7273
//
7374
// For valid credentials it calls the next handler.
7475
// For missing or invalid credentials, it sends "401 - Unauthorized" response.
75-
func Auth(ce *casbin.Enforcer) echo.MiddlewareFunc {
76-
c := DefaultAuthConfig
76+
func Middleware(ce *casbin.Enforcer) echo.MiddlewareFunc {
77+
c := DefaultConfig
7778
c.Enforcer = ce
78-
return AuthWithConfig(c)
79+
return MiddlewareWithConfig(c)
7980
}
8081

81-
// AuthWithConfig returns a CasbinAuth middleware with config.
82-
// See `Auth()`.
83-
func AuthWithConfig(config AuthConfig) echo.MiddlewareFunc {
82+
// MiddlewareWithConfig returns a CasbinAuth middleware with config.
83+
// See `Middleware()`.
84+
func MiddlewareWithConfig(config Config) echo.MiddlewareFunc {
8485
// Defaults
8586
if config.Skipper == nil {
86-
config.Skipper = DefaultAuthConfig.Skipper
87+
config.Skipper = DefaultConfig.Skipper
8788
}
8889

8990
return func(next echo.HandlerFunc) echo.HandlerFunc {
@@ -99,14 +100,14 @@ func AuthWithConfig(config AuthConfig) echo.MiddlewareFunc {
99100

100101
// GetUserName gets the user name from the request.
101102
// Currently, only HTTP basic authentication is supported
102-
func (a *AuthConfig) GetUserName(c echo.Context) string {
103+
func (a *Config) GetUserName(c echo.Context) string {
103104
username, _, _ := c.Request().BasicAuth()
104105
return username
105106
}
106107

107108
// CheckPermission checks the user/method/path combination from the request.
108109
// Returns true (permission granted) or false (permission forbidden)
109-
func (a *AuthConfig) CheckPermission(c echo.Context) bool {
110+
func (a *Config) CheckPermission(c echo.Context) bool {
110111
user := a.GetUserName(c)
111112
method := c.Request().Method
112113
path := c.Request().URL.Path

casbin/auth_test.go renamed to casbin/casbin_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func testRequest(t *testing.T, ce *casbin.Enforcer, user string, path string, me
1515
req.SetBasicAuth(user, "secret")
1616
res := httptest.NewRecorder()
1717
c := e.NewContext(req, res)
18-
h := Auth(ce)(func(c echo.Context) error {
18+
h := Middleware(ce)(func(c echo.Context) error {
1919
return c.String(http.StatusOK, "test")
2020
})
2121

session/session.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package session
2+
3+
import (
4+
"github.com/gorilla/sessions"
5+
"github.com/labstack/echo"
6+
"github.com/labstack/echo/middleware"
7+
)
8+
9+
type (
10+
// Config defines the config for CasbinAuth middleware.
11+
Config struct {
12+
// Skipper defines a function to skip middleware.
13+
Skipper middleware.Skipper
14+
15+
// Session store.
16+
// Required.
17+
Store sessions.Store
18+
}
19+
)
20+
21+
var (
22+
// DefaultConfig is the default Session middleware config.
23+
DefaultConfig = Config{
24+
Skipper: middleware.DefaultSkipper,
25+
}
26+
)
27+
28+
// Get returns a named session.
29+
func Get(name string, c echo.Context) (*sessions.Session, error) {
30+
store := c.Get("_session_store").(sessions.Store)
31+
return store.Get(c.Request(), name)
32+
}
33+
34+
// Middleware returns a Session middleware.
35+
func Middleware(store sessions.Store) echo.MiddlewareFunc {
36+
c := DefaultConfig
37+
c.Store = store
38+
return MiddlewareWithConfig(c)
39+
}
40+
41+
// MiddlewareWithConfig returns a Sessions middleware with config.
42+
// See `Middleware()`.
43+
func MiddlewareWithConfig(config Config) echo.MiddlewareFunc {
44+
// Defaults
45+
if config.Skipper == nil {
46+
config.Skipper = DefaultConfig.Skipper
47+
}
48+
if config.Store == nil {
49+
panic("echo: session middleware requires store")
50+
}
51+
52+
return func(next echo.HandlerFunc) echo.HandlerFunc {
53+
return func(c echo.Context) error {
54+
if config.Skipper(c) {
55+
return next(c)
56+
}
57+
c.Set("_session_store", config.Store)
58+
return next(c)
59+
}
60+
}
61+
}

session/session_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package session

0 commit comments

Comments
 (0)