Skip to content

Commit 6d0a4d7

Browse files
committed
Update go modules for Iris v12.2.0-alpha2
1 parent 07e8bce commit 6d0a4d7

File tree

13 files changed

+129
-93
lines changed

13 files changed

+129
-93
lines changed

casbin/_examples/middleware/main.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,33 @@ import (
44
"github.com/kataras/iris/v12"
55
"github.com/kataras/iris/v12/middleware/basicauth"
66

7-
"github.com/casbin/casbin/v2"
8-
cm "github.com/iris-contrib/middleware/casbin"
7+
"github.com/iris-contrib/middleware/casbin"
98
)
109

11-
// $ go get github.com/casbin/casbin/v2@v2.13.1
10+
// $ go get github.com/casbin/casbin/v2@v2.17.0
1211
// $ go run main.go
1312

14-
// Enforcer maps the model and the policy for the casbin service, we use this variable on the main_test too.
15-
var Enforcer, _ = casbin.NewEnforcer("casbinmodel.conf", "casbinpolicy.csv")
16-
1713
func newApp() *iris.Application {
1814
app := iris.New()
1915

20-
casbinMiddleware := cm.New(Enforcer)
21-
/* Casbin requires an authenticated user name,
22-
You have three ways to set that username:
23-
1. casbinMiddleware.UsernameExtractor = func(ctx iris.Context) string {
24-
// [...custom logic]
25-
return "bob"
16+
casbinMiddleware, err := casbin.NewEnforcer("casbinmodel.conf", "casbinpolicy.csv")
17+
if err != nil {
18+
panic(err)
2619
}
27-
2. by SetUsername package-level function:
28-
func auth(ctx iris.Context) {
29-
cm.SetUsername(ctx, "bob")
30-
ctx.Next()
20+
/* The Casbin authorization determines a request based on `{subject, object, action}`.
21+
Please refer to: https://github.com/casbin/casbin to understand how it works first.
22+
The object is the current request's path and the action is the current request's method.
23+
The subject is extracted by the current request's ctx.User().GetUsername(),
24+
you can customize it by:
25+
1. casbinMiddleware.SubjectExtractor = func(ctx iris.Context) string {
26+
// [...custom logic]
27+
return "bob"
3128
}
32-
3. By registering an auth middleware that fills the Context.User()
33-
^ recommended way, and that's what it's used on that example.
29+
2. by SetSubject package-level function:
30+
func auth(ctx iris.Context) {
31+
casbin.SetSubject(ctx, "bob")
32+
ctx.Next()
33+
}
3434
*/
3535
app.Use(basicauth.Default(map[string]string{
3636
"bob": "bobpass",
@@ -58,9 +58,9 @@ func main() {
5858
}
5959

6060
func hi(ctx iris.Context) {
61-
// Note that, by default, the username is extracted by ctx.Request().BasicAuth
62-
// to change that, use the `cm.SetUsername` before the casbin middleware's execution.
63-
ctx.Writef("Hello %s", cm.Username(ctx))
61+
ctx.Writef("Hello %s", casbin.Subject(ctx))
62+
// Note that, by default, the username is extracted by ctx.User().GetUsername()
63+
// to change that behavior modify the `casbin.SubjectExtractor` or
64+
// use the `casbin.SetSubject` to set a custom subject for the current request
65+
// before the casbin middleware's execution.
6466
}
65-
66-
// You can modify the username that casbin uses by:

casbin/_examples/router/main.go

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,41 @@ import (
44
"github.com/kataras/iris/v12"
55
"github.com/kataras/iris/v12/middleware/basicauth"
66

7-
"github.com/casbin/casbin/v2"
8-
cm "github.com/iris-contrib/middleware/casbin"
7+
"github.com/iris-contrib/middleware/casbin"
98
)
109

11-
// $ go get github.com/casbin/casbin/v2@v2.13.1
10+
// $ go get github.com/casbin/casbin/v2@v2.17.0
1211
// $ go run main.go
1312

14-
// Enforcer maps the model and the policy for the casbin service, we use this variable on the main_test too.
15-
var Enforcer, _ = casbin.NewEnforcer("casbinmodel.conf", "casbinpolicy.csv")
16-
1713
func newApp() *iris.Application {
1814
app := iris.New()
1915

20-
casbinMiddleware := cm.New(Enforcer)
21-
/* Casbin requires an authenticated user name,
22-
You have three ways to set that username:
23-
1. casbinMiddleware.UsernameExtractor = func(ctx iris.Context) string {
24-
// [...custom logic]
25-
return "bob"
16+
casbinMiddleware, err := casbin.NewEnforcer("casbinmodel.conf", "casbinpolicy.csv")
17+
if err != nil {
18+
panic(err)
2619
}
27-
2. by SetUsername package-level function:
28-
func auth(ctx iris.Context) {
29-
cm.SetUsername(ctx, "bob")
30-
ctx.Next()
20+
/* The Casbin authorization determines a request based on `{subject, object, action}`.
21+
Please refer to: https://github.com/casbin/casbin to understand how it works first.
22+
The object is the current request's path and the action is the current request's method.
23+
The subject is extracted by the current request's ctx.User().GetUsername(),
24+
you can customize it by:
25+
1. casbinMiddleware.SubjectExtractor = func(ctx iris.Context) string {
26+
// [...custom logic]
27+
return "bob"
3128
}
32-
3. By registering an auth middleware that fills the Context.User()
33-
^ recommended way, and that's what it's used on that example.
29+
2. by SetSubject package-level function:
30+
func auth(ctx iris.Context) {
31+
casbin.SetSubject(ctx, "bob")
32+
ctx.Next()
33+
}
3434
*/
3535
app.UseRouter(basicauth.Default(map[string]string{
3636
"bob": "bobpass",
3737
"alice": "alicepass",
3838
}))
39-
// Note that by registering with UseRouter,
40-
// and becauese the middleware stops the execution with 403 (Forbidden)
39+
40+
// Note that by registering with UseRouter instead of Use,
41+
// and becauese the middleware stops the execution with 403 (Forbidden) by default,
4142
// if the authentication and roles match failed,
4243
// unregistered route paths will fire 403 instead of 404 (Not Found).
4344
app.UseRouter(casbinMiddleware.ServeHTTP)
@@ -62,7 +63,9 @@ func main() {
6263
}
6364

6465
func hi(ctx iris.Context) {
65-
ctx.Writef("Hello %s", cm.Username(ctx))
66-
// Note that, by default, the username is extracted by ctx.Request().BasicAuth
67-
// to change that, use the `cm.SetUsername` before the casbin middleware's execution.
66+
ctx.Writef("Hello %s", casbin.Subject(ctx))
67+
// Note that, by default, the username is extracted by ctx.User().GetUsername()
68+
// to change that behavior modify the `casbin.SubjectExtractor` or
69+
// use the `casbin.SetSubject` to set a custom subject for the current request
70+
// before the casbin middleware's execution.
6871
}

casbin/casbin.go

Lines changed: 70 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,67 @@ func init() {
1111
context.SetHandlerName("github.com/iris-contrib/middleware/casbin.*", "iris-contrib.casbin")
1212
}
1313

14-
// Casbin is the auth services which contains the casbin enforcer.
14+
// Casbin is the auth service which contains the casbin enforcer.
1515
type Casbin struct {
1616
enforcer *casbin.Enforcer
17-
// Can be used to customize the username passed to the casbin enforcer.
18-
UsernameExtractor func(iris.Context) string
17+
// SubjectExtractor is used to extract the
18+
// current request's subject for the casbin role enforcer.
19+
// Defaults to the `Subject` package-level function which
20+
// extracts the subject from a prior registered authorization middleware's
21+
// username (e.g. basicauth or JWT).
22+
SubjectExtractor func(iris.Context) string
23+
24+
// UnauthorizedHandler sets a custom handler to be executed
25+
// when the role checks fail.
26+
// Defaults to a handler which sends a status forbidden (403) status code.
27+
UnauthorizedHandler iris.Handler
1928
}
2029

21-
// New returns the auth service which receives a casbin enforcer.
22-
// The username that casbin requires is extracted by:
23-
// - UsernameExtractor
24-
// - casbin.Username
25-
// | set with casbin.SetUsername
30+
// New returns the Casbin middleware based on the given casbin.Enforcer instance.
31+
// The authorization determines a request based on `{subject, object, action}`.
32+
// Please refer to: https://github.com/casbin/casbin to understand how it works first.
33+
//
34+
// The object is the current request's path and the action is the current request's method.
35+
// The subject that casbin requires is extracted by:
36+
// - SubjectExtractor
37+
// - casbin.Subject
38+
// | set with casbin.SetSubject
2639
// - Context.User().GetUsername()
27-
// | by a prior auth middleware through Context.SetUser
40+
// | by a prior auth middleware through Context.SetUser.
2841
func New(e *casbin.Enforcer) *Casbin {
29-
return &Casbin{enforcer: e}
42+
return &Casbin{
43+
enforcer: e,
44+
SubjectExtractor: func(ctx iris.Context) string {
45+
return Subject(ctx)
46+
},
47+
UnauthorizedHandler: func(ctx iris.Context) {
48+
ctx.StopWithStatus(iris.StatusForbidden)
49+
},
50+
}
51+
}
52+
53+
// NewEnforcer returns the Casbin middleware based on the given model and policy file paths.
54+
//
55+
// Read `New` package-level function for more information.
56+
func NewEnforcer(modelFile, policyFile string) (*Casbin, error) {
57+
e, err := casbin.NewEnforcer(modelFile, policyFile)
58+
if err != nil {
59+
return nil, err
60+
}
61+
62+
return New(e), nil
3063
}
3164

3265
// ServeHTTP is the iris compatible casbin handler which should be passed to specific routes or parties.
66+
// Responds with Status Forbidden on unauthorized clients.
3367
// Usage:
68+
// - app.Use(authMiddleware)
69+
// - app.Use(casbinMiddleware.ServeHTTP) OR
70+
// - app.UseRouter(casbinMiddleware.ServeHTTP) OR per route:
3471
// - app.Get("/dataset1/resource1", casbinMiddleware.ServeHTTP, myHandler)
35-
// - app.Use(casbinMiddleware.ServeHTTP)
36-
// - app.UseRouter(casbinMiddleware.ServeHTTP)
3772
func (c *Casbin) ServeHTTP(ctx iris.Context) {
3873
if !c.Check(ctx) {
39-
ctx.StopWithStatus(iris.StatusForbidden)
74+
c.UnauthorizedHandler(ctx)
4075
return
4176
}
4277

@@ -51,35 +86,33 @@ func (c *Casbin) ServeHTTP(ctx iris.Context) {
5186
// - inside a handler
5287
// - using the iris.NewConditionalHandler
5388
func (c *Casbin) Check(ctx iris.Context) bool {
54-
var username string
55-
if c.UsernameExtractor != nil {
56-
username = c.UsernameExtractor(ctx)
57-
} else {
58-
username = Username(ctx)
59-
}
60-
61-
ok, _ := c.enforcer.Enforce(username, ctx.Path(), ctx.Method())
89+
ok, _ := c.Enforce(ctx, c.SubjectExtractor(ctx))
6290
return ok
6391
}
6492

65-
const usernameContextKey = "iris.contrib.casbin.username"
93+
// Enforce accepts the Context's path and method and a subject/role/username
94+
// and reports whether the specific "subject" has access to the current request.
95+
func (c *Casbin) Enforce(ctx iris.Context, subject string) (bool, error) {
96+
return c.enforcer.Enforce(subject, ctx.Path(), ctx.Method())
97+
}
6698

67-
// Username gets the username from the basicauth
68-
// or the given (by a prior middleware) username.
69-
// See `SetUsername` package-level function too.
70-
func Username(ctx iris.Context) string {
71-
username := ctx.Values().GetString(usernameContextKey)
72-
if username == "" {
73-
if u := ctx.User(); u != nil {
74-
username, _ = u.GetUsername()
75-
}
99+
const subjectContextKey = "iris.contrib.casbin.subject"
76100

77-
}
78-
return username
101+
// SetSubject sets a custom subject for the current request for the casbin middleware.
102+
// See `Subject` package-level function too.
103+
func SetSubject(ctx iris.Context, subject string) {
104+
ctx.Values().Set(subjectContextKey, subject)
79105
}
80106

81-
// SetUsername sets a custom username for the casbin middleware.
82-
// See `Username` package-level function too.
83-
func SetUsername(ctx iris.Context, username string) {
84-
ctx.Values().Set(usernameContextKey, username)
107+
// Subject gets the subject from an authorization middleware's username, e.g. basicauth or JWT.
108+
// If there is no registered middleware to fetch the subject then
109+
// it tries to extract it from the context's values (see SetSubject package-level function to set it).
110+
func Subject(ctx iris.Context) string {
111+
subject := ctx.Values().GetString(subjectContextKey)
112+
if subject == "" {
113+
if u := ctx.User(); u != nil {
114+
subject, _ = u.GetUsername()
115+
}
116+
}
117+
return subject
85118
}

casbin/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ go 1.14
44

55
require (
66
github.com/casbin/casbin/v2 v2.17.0
7-
github.com/kataras/iris/v12 v12.2.0-alpha.0.20201113181155-4d09475c290d
7+
github.com/kataras/iris/v12 v12.2.0-alpha2
88
)

cloudwatch/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module github.com/iris-contrib/middleware/cloudwatch
22

33
go 1.14
44

5-
require github.com/kataras/iris/v12 v12.2.0-alpha.0.20201113181155-4d09475c290d
5+
require github.com/kataras/iris/v12 v12.2.0-alpha2

cors/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module github.com/iris-contrib/middleware/cors
22

33
go 1.14
44

5-
require github.com/kataras/iris/v12 v12.2.0-alpha.0.20201113181155-4d09475c290d
5+
require github.com/kataras/iris/v12 v12.2.0-alpha2

csrf/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ go 1.15
44

55
require (
66
github.com/gorilla/securecookie v1.1.1
7-
github.com/kataras/iris/v12 v12.2.0-alpha.0.20201113181155-4d09475c290d
7+
github.com/kataras/iris/v12 v12.2.0-alpha2
88
)

jwt/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ go 1.14
44

55
require (
66
github.com/dgrijalva/jwt-go v3.2.0+incompatible
7-
github.com/kataras/iris/v12 v12.2.0-alpha.0.20201113181155-4d09475c290d
7+
github.com/kataras/iris/v12 v12.2.0-alpha2
88
)

newrelic/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module github.com/iris-contrib/middleware/newrelic
33
go 1.14
44

55
require (
6-
github.com/kataras/iris/v12 v12.2.0-alpha.0.20201113181155-4d09475c290d
6+
github.com/kataras/iris/v12 v12.2.0-alpha2
77
github.com/newrelic/go-agent/v3 v3.8.1
88
)

prometheus/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module github.com/iris-contrib/middleware/prometheus
33
go 1.14
44

55
require (
6-
github.com/kataras/iris/v12 v12.2.0-alpha.0.20201113181155-4d09475c290d
6+
github.com/kataras/iris/v12 v12.2.0-alpha2
77
github.com/prometheus/client_golang v1.2.1 // indirect
88
)

secure/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ module github.com/iris-contrib/middleware/secure
33
go 1.14
44

55
require (
6-
github.com/kataras/iris/v12 v12.2.0-alpha.0.20201113181155-4d09475c290d
6+
github.com/kataras/iris/v12 v12.2.0-alpha2
77
)

throttler/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module github.com/iris-contrib/middleware/throttler
33
go 1.14
44

55
require (
6-
github.com/kataras/iris/v12 v12.2.0-alpha.0.20201113181155-4d09475c290d
6+
github.com/kataras/iris/v12 v12.2.0-alpha2
77
github.com/throttled/throttled/v2 v2.6.0
88
)

tollboothic/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ go 1.14
44

55
require (
66
github.com/didip/tollbooth/v6 v6.0.1
7-
github.com/kataras/iris/v12 v12.2.0-alpha.0.20201113181155-4d09475c290d
7+
github.com/kataras/iris/v12 v12.2.0-alpha2
88
)

0 commit comments

Comments
 (0)