move example flash-messages to sessions example folder and change httptest example with basicauth

Former-commit-id: 3c5f6c97629a2a6ae44e62f2900edd32c0329b50
This commit is contained in:
kataras 2017-06-10 05:00:18 +03:00
parent c4788ee4e8
commit dd26fbf26d
7 changed files with 76 additions and 47 deletions

View File

@ -8,8 +8,9 @@ go_import_path: github.com/kataras/iris
install: install:
- go get ./... - go get ./...
# install test dependencies # install test dependencies
- go get golang.org/x/tools/cmd/cover # - go get golang.org/x/tools/cmd/cover
- go get -v github.com/axw/gocov # - go get -v github.com/axw/gocov
- go install github.com/axw/gocov/gocov # - go install github.com/axw/gocov/gocov
script: script:
- gocov test | gocov report # - 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 ./...

View File

@ -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) * [Overview](intermediate/sessions/overview/main.go)
* [Encoding & Decoding the Session ID: Secure Cookie](intermediate/sessions/securecookie/main.go) * [Encoding & Decoding the Session ID: Secure Cookie](intermediate/sessions/securecookie/main.go)
* [Standalone](intermediate/sessions/standalone/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) * [With A Back-End Database](intermediate/sessions/database/main.go)
* [Password Hashing](intermediate/sessions/password-hashing/main.go) * [Password Hashing](intermediate/sessions/password-hashing/main.go)
* [Flash Messages](intermediate/flash-messages/main.go)
* [Websockets](intermediate/websockets) * [Websockets](intermediate/websockets)
* [Ridiculous Simple](intermediate/websockets/ridiculous-simple/main.go) * [Ridiculous Simple](intermediate/websockets/ridiculous-simple/main.go)
* [Overview](intermediate/websockets/overview/main.go) * [Overview](intermediate/websockets/overview/main.go)

View File

@ -37,19 +37,19 @@ func main() {
{ {
//http://localhost:8080/admin //http://localhost:8080/admin
needAuth.Get("/", func(ctx context.Context) { needAuth.Get("/", func(ctx context.Context) {
username := ctx.Values().GetString("mycustomkey") // the Contextkey from the authConfig username := ctx.Values().GetString("user") // the Contextkey from the authConfig
ctx.Writef("Hello authenticated user: %s from: %s ", username, ctx.Path()) ctx.Writef("Hello authenticated user: %s from: %s", username, ctx.Path())
}) })
// http://localhost:8080/admin/profile // http://localhost:8080/admin/profile
needAuth.Get("/profile", func(ctx context.Context) { needAuth.Get("/profile", func(ctx context.Context) {
username := ctx.Values().GetString("mycustomkey") // the Contextkey from the authConfig username := ctx.Values().GetString("user") // the Contextkey from the authConfig
ctx.Writef("Hello authenticated user: %s from: %s ", username, ctx.Path()) ctx.Writef("Hello authenticated user: %s from: % ", username, ctx.Path())
}) })
// http://localhost:8080/admin/settings // http://localhost:8080/admin/settings
needAuth.Get("/settings", func(ctx context.Context) { needAuth.Get("/settings", func(ctx context.Context) {
username := authConfig.User(ctx) // shortcut for ctx.Values().GetString("mycustomkey") username := authConfig.User(ctx) // shortcut for ctx.Values().GetString("user")
ctx.Writef("Hello authenticated user: %s from: %s ", username, ctx.Path()) ctx.Writef("Hello authenticated user: %s from: %s", username, ctx.Path())
}) })
} }

View File

@ -3,33 +3,57 @@ package main
import ( import (
"github.com/kataras/iris" "github.com/kataras/iris"
"github.com/kataras/iris/context" "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 := 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) { authentication := basicauth.New(authConfig)
sess := ctx.Session()
if !sess.HasFlash() {
ctx.HTML("<h1> Unauthorized Page! </h1>")
return
}
ctx.JSON(context.Map{ // to global app.Use(authentication) (or app.UseGlobal before the .Run)
"Message": "Hello", // to routes
"From": sess.GetFlash("name"), /*
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) { app.Get("/", func(ctx context.Context) { ctx.Redirect("/admin") })
sess := ctx.Session()
if !sess.HasFlash() {
sess.SetFlash("name", ctx.FormValue("name"))
}
}) // 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")) app.Run(iris.Addr(":8080"))
} }

View File

@ -3,27 +3,31 @@ package main
import ( import (
"testing" "testing"
"github.com/kataras/iris"
"github.com/kataras/iris/httptest" "github.com/kataras/iris/httptest"
) )
// $ cd _example // $ cd _example
// $ go test -v // $ go test -v
func TestNewApp(t *testing.T) { func TestNewApp(t *testing.T) {
app := newApp() app := buildApp()
e := httptest.New(app, t) e := httptest.New(app, t)
// test nauthorized // redirects to /admin without basic auth
e.GET("/hello").Expect().Status(401).Body().Equal("<h1> Unauthorized Page! </h1>") e.GET("/").Expect().Status(iris.StatusUnauthorized)
// test our login flash message // without basic auth
name := "myname" e.GET("/admin").Expect().Status(iris.StatusUnauthorized)
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) // with valid basic auth
// setted on /login previously. e.GET("/admin").WithBasicAuth("myusername", "mypassword").Expect().
expectedResponse := map[string]interface{}{ Status(iris.StatusOK).Body().Equal("Hello authenticated user: myusername from: /admin")
"Message": "Hello", e.GET("/admin/profile").WithBasicAuth("myusername", "mypassword").Expect().
"From": name, Status(iris.StatusOK).Body().Equal("Hello authenticated user: myusername from: /admin/profile")
} e.GET("/admin/settings").WithBasicAuth("myusername", "mypassword").Expect().
e.GET("/hello").Expect().Status(200).JSON().Equal(expectedResponse) Status(iris.StatusOK).Body().Equal("Hello authenticated user: myusername from: /admin/settings")
// test /hello nauthorized again, it should be return 401 now (flash should be removed)
e.GET("/hello").Expect().Status(401).Body().Equal("<h1> Unauthorized Page! </h1>") // with invalid basic auth
e.GET("/admin/settings").WithBasicAuth("invalidusername", "invalidpassword").
Expect().Status(iris.StatusUnauthorized)
} }

View File

@ -66,9 +66,9 @@ func (s *session) runFlashGC() {
s.mu.Unlock() 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 { 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 // GetFlash returns a flash message which removed on the next request