diff --git a/.travis.yml b/.travis.yml index a73f0300..d385a064 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,8 +8,9 @@ go_import_path: github.com/kataras/iris install: - go get ./... # install test dependencies - - go get golang.org/x/tools/cmd/cover - - go get -v github.com/axw/gocov - - go install github.com/axw/gocov/gocov +# - go get golang.org/x/tools/cmd/cover +# - go get -v github.com/axw/gocov +# - go install github.com/axw/gocov/gocov script: - - gocov test | gocov report \ No newline at end of file + # - gocov test | gocov report # the result is invalid because it tests the vendor too, which are removed to reduce the dl size. + - go test -v ./... \ No newline at end of file diff --git a/_examples/README.md b/_examples/README.md index 23668ffb..9faef83d 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -78,9 +78,9 @@ It doesn't contains "best ways" neither explains all its features. It's just a s * [Overview](intermediate/sessions/overview/main.go) * [Encoding & Decoding the Session ID: Secure Cookie](intermediate/sessions/securecookie/main.go) * [Standalone](intermediate/sessions/standalone/main.go) + * [Flash Messages](intermediate/sessions/flash-messages/main.go) * [With A Back-End Database](intermediate/sessions/database/main.go) * [Password Hashing](intermediate/sessions/password-hashing/main.go) - * [Flash Messages](intermediate/flash-messages/main.go) * [Websockets](intermediate/websockets) * [Ridiculous Simple](intermediate/websockets/ridiculous-simple/main.go) * [Overview](intermediate/websockets/overview/main.go) diff --git a/_examples/beginner/basicauth/main.go b/_examples/beginner/basicauth/main.go index b7e37a26..fdbeae65 100644 --- a/_examples/beginner/basicauth/main.go +++ b/_examples/beginner/basicauth/main.go @@ -37,19 +37,19 @@ func main() { { //http://localhost:8080/admin needAuth.Get("/", func(ctx context.Context) { - username := ctx.Values().GetString("mycustomkey") // the Contextkey from the authConfig - ctx.Writef("Hello authenticated user: %s from: %s ", username, ctx.Path()) + username := ctx.Values().GetString("user") // the Contextkey from the authConfig + ctx.Writef("Hello authenticated user: %s from: %s", username, ctx.Path()) }) // http://localhost:8080/admin/profile needAuth.Get("/profile", func(ctx context.Context) { - username := ctx.Values().GetString("mycustomkey") // the Contextkey from the authConfig - ctx.Writef("Hello authenticated user: %s from: %s ", username, ctx.Path()) + username := ctx.Values().GetString("user") // the Contextkey from the authConfig + ctx.Writef("Hello authenticated user: %s from: % ", username, ctx.Path()) }) // http://localhost:8080/admin/settings needAuth.Get("/settings", func(ctx context.Context) { - username := authConfig.User(ctx) // shortcut for ctx.Values().GetString("mycustomkey") - ctx.Writef("Hello authenticated user: %s from: %s ", username, ctx.Path()) + username := authConfig.User(ctx) // shortcut for ctx.Values().GetString("user") + ctx.Writef("Hello authenticated user: %s from: %s", username, ctx.Path()) }) } diff --git a/_examples/intermediate/httptest/main.go b/_examples/intermediate/httptest/main.go index 46766f83..c8d3ecc7 100644 --- a/_examples/intermediate/httptest/main.go +++ b/_examples/intermediate/httptest/main.go @@ -3,33 +3,57 @@ package main import ( "github.com/kataras/iris" "github.com/kataras/iris/context" - "github.com/kataras/iris/sessions" + "github.com/kataras/iris/middleware/basicauth" ) -func main() { +func buildApp() *iris.Application { app := iris.New() - app.AttachSessionManager(sessions.New(sessions.Config{Cookie: "mysessionid"})) + authConfig := basicauth.Config{ + Users: map[string]string{"myusername": "mypassword", "mySecondusername": "mySecondpassword"}, + Realm: "Authorization Required", // defaults to "Authorization Required" + ContextKey: "user", // defaults to "user" + } - app.Get("/hello", func(ctx context.Context) { - sess := ctx.Session() - if !sess.HasFlash() { - ctx.HTML("

Unauthorized Page!

") - return - } + authentication := basicauth.New(authConfig) - ctx.JSON(context.Map{ - "Message": "Hello", - "From": sess.GetFlash("name"), + // 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.Post("/login", func(ctx context.Context) { - sess := ctx.Session() - if !sess.HasFlash() { - sess.SetFlash("name", ctx.FormValue("name")) - } + app.Get("/", func(ctx context.Context) { ctx.Redirect("/admin") }) - }) + // to party + + 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()) + }) + // 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()) + }) + + // 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()) + }) + } + + return app +} + +func main() { + app := buildApp() app.Run(iris.Addr(":8080")) } diff --git a/_examples/intermediate/httptest/main_test.go b/_examples/intermediate/httptest/main_test.go index 6a6a8794..0f74dd97 100644 --- a/_examples/intermediate/httptest/main_test.go +++ b/_examples/intermediate/httptest/main_test.go @@ -3,27 +3,31 @@ package main import ( "testing" + "github.com/kataras/iris" "github.com/kataras/iris/httptest" ) // $ cd _example // $ go test -v func TestNewApp(t *testing.T) { - app := newApp() + app := buildApp() e := httptest.New(app, t) - // test nauthorized - e.GET("/hello").Expect().Status(401).Body().Equal("

Unauthorized Page!

") - // test our login flash message - name := "myname" - e.POST("/login").WithFormField("name", name).Expect().Status(200) - // test the /hello again with the flash (a message which deletes itself after it has been shown to the user) - // setted on /login previously. - expectedResponse := map[string]interface{}{ - "Message": "Hello", - "From": name, - } - e.GET("/hello").Expect().Status(200).JSON().Equal(expectedResponse) - // test /hello nauthorized again, it should be return 401 now (flash should be removed) - e.GET("/hello").Expect().Status(401).Body().Equal("

Unauthorized Page!

") + // redirects to /admin without basic auth + e.GET("/").Expect().Status(iris.StatusUnauthorized) + // without basic auth + e.GET("/admin").Expect().Status(iris.StatusUnauthorized) + + // with valid basic auth + e.GET("/admin").WithBasicAuth("myusername", "mypassword").Expect(). + Status(iris.StatusOK).Body().Equal("Hello authenticated user: myusername from: /admin") + e.GET("/admin/profile").WithBasicAuth("myusername", "mypassword").Expect(). + Status(iris.StatusOK).Body().Equal("Hello authenticated user: myusername from: /admin/profile") + e.GET("/admin/settings").WithBasicAuth("myusername", "mypassword").Expect(). + Status(iris.StatusOK).Body().Equal("Hello authenticated user: myusername from: /admin/settings") + + // with invalid basic auth + e.GET("/admin/settings").WithBasicAuth("invalidusername", "invalidpassword"). + Expect().Status(iris.StatusUnauthorized) + } diff --git a/_examples/intermediate/flash-messages/main.go b/_examples/intermediate/sessions/flash-messages/main.go similarity index 100% rename from _examples/intermediate/flash-messages/main.go rename to _examples/intermediate/sessions/flash-messages/main.go diff --git a/sessions/session.go b/sessions/session.go index 4e0659c2..8292c0cb 100644 --- a/sessions/session.go +++ b/sessions/session.go @@ -66,9 +66,9 @@ func (s *session) runFlashGC() { s.mu.Unlock() } -// HasFlash returns true if this request has available flash messages +// HasFlash returns true if this session has available flash messages. func (s *session) HasFlash() bool { - return s.flashes != nil && len(s.flashes) > 0 + return len(s.flashes) > 0 } // GetFlash returns a flash message which removed on the next request