1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-23 21:07:03 +00:00

Simplify the basicauth middleware

Former-commit-id: 8d184a434c992a884c5565bc22767ef295a1575a
This commit is contained in:
kataras
2017-06-10 15:28:09 +03:00
parent 6ab00aa02f
commit 5fa9789c35
9 changed files with 136 additions and 88 deletions

View File

@@ -6,26 +6,15 @@ import (
"github.com/kataras/iris/middleware/basicauth"
)
func buildApp() *iris.Application {
func newApp() *iris.Application {
app := iris.New()
authConfig := basicauth.Config{
Users: map[string]string{"myusername": "mypassword", "mySecondusername": "mySecondpassword"},
Realm: "Authorization Required", // defaults to "Authorization Required"
ContextKey: "user", // defaults to "user"
Users: map[string]string{"myusername": "mypassword"},
}
authentication := basicauth.New(authConfig)
// to global app.Use(authentication) (or app.UseGlobal before the .Run)
// to routes
/*
app.Get("/mysecret", authentication, func(ctx context.Context) {
username := ctx.Values().GetString("user") // the Contextkey from the authConfig
ctx.Writef("Hello authenticated user: %s ", username)
})
*/
app.Get("/", func(ctx context.Context) { ctx.Redirect("/admin") })
// to party
@@ -33,27 +22,26 @@ func buildApp() *iris.Application {
needAuth := app.Party("/admin", authentication)
{
//http://localhost:8080/admin
needAuth.Get("/", func(ctx context.Context) {
username := ctx.Values().GetString("user") // the Contextkey from the authConfig
ctx.Writef("Hello authenticated user: %s from: %s", username, ctx.Path())
})
needAuth.Get("/", h)
// http://localhost:8080/admin/profile
needAuth.Get("/profile", func(ctx context.Context) {
username := ctx.Values().GetString("user") // the Contextkey from the authConfig
ctx.Writef("Hello authenticated user: %s from: %s", username, ctx.Path())
})
needAuth.Get("/profile", h)
// http://localhost:8080/admin/settings
needAuth.Get("/settings", func(ctx context.Context) {
username := authConfig.User(ctx) // shortcut for ctx.Values().GetString("user")
ctx.Writef("Hello authenticated user: %s from: %s", username, ctx.Path())
})
needAuth.Get("/settings", h)
}
return app
}
func h(ctx context.Context) {
username, password, _ := ctx.Request().BasicAuth()
// third parameter it will be always true because the middleware
// makes sure for that, otherwise this handler will not be executed.
ctx.Writef("%s %s:%s", ctx.Path(), username, password)
}
func main() {
app := buildApp()
app := newApp()
app.Run(iris.Addr(":8080"))
}

View File

@@ -7,10 +7,10 @@ import (
"github.com/kataras/iris/httptest"
)
// $ cd _example
// $ cd $GOPATH/src/github.com/kataras/iris/_examples/intermediate/httptest
// $ go test -v
func TestNewApp(t *testing.T) {
app := buildApp()
app := newApp()
e := httptest.New(app, t)
// redirects to /admin without basic auth
@@ -20,11 +20,11 @@ func TestNewApp(t *testing.T) {
// with valid basic auth
e.GET("/admin").WithBasicAuth("myusername", "mypassword").Expect().
Status(iris.StatusOK).Body().Equal("Hello authenticated user: myusername from: /admin")
Status(iris.StatusOK).Body().Equal("/admin myusername:mypassword")
e.GET("/admin/profile").WithBasicAuth("myusername", "mypassword").Expect().
Status(iris.StatusOK).Body().Equal("Hello authenticated user: myusername from: /admin/profile")
Status(iris.StatusOK).Body().Equal("/admin/profile myusername:mypassword")
e.GET("/admin/settings").WithBasicAuth("myusername", "mypassword").Expect().
Status(iris.StatusOK).Body().Equal("Hello authenticated user: myusername from: /admin/settings")
Status(iris.StatusOK).Body().Equal("/admin/settings myusername:mypassword")
// with invalid basic auth
e.GET("/admin/settings").WithBasicAuth("invalidusername", "invalidpassword").