mirror of
https://github.com/kataras/iris.git
synced 2025-03-14 08:26:26 +01:00
move example flash-messages to sessions example folder and change httptest example with basicauth
Former-commit-id: 3c5f6c97629a2a6ae44e62f2900edd32c0329b50
This commit is contained in:
parent
c4788ee4e8
commit
dd26fbf26d
|
@ -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
|
||||
# - 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 ./...
|
|
@ -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)
|
||||
|
|
|
@ -37,18 +37,18 @@ func main() {
|
|||
{
|
||||
//http://localhost:8080/admin
|
||||
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())
|
||||
})
|
||||
// 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")
|
||||
username := authConfig.User(ctx) // shortcut for ctx.Values().GetString("user")
|
||||
ctx.Writef("Hello authenticated user: %s from: %s", username, ctx.Path())
|
||||
})
|
||||
}
|
||||
|
|
|
@ -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"}))
|
||||
|
||||
app.Get("/hello", func(ctx context.Context) {
|
||||
sess := ctx.Session()
|
||||
if !sess.HasFlash() {
|
||||
ctx.HTML("<h1> Unauthorized Page! </h1>")
|
||||
return
|
||||
authConfig := basicauth.Config{
|
||||
Users: map[string]string{"myusername": "mypassword", "mySecondusername": "mySecondpassword"},
|
||||
Realm: "Authorization Required", // defaults to "Authorization Required"
|
||||
ContextKey: "user", // defaults to "user"
|
||||
}
|
||||
|
||||
ctx.JSON(context.Map{
|
||||
"Message": "Hello",
|
||||
"From": sess.GetFlash("name"),
|
||||
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
|
||||
|
||||
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())
|
||||
})
|
||||
|
||||
app.Post("/login", func(ctx context.Context) {
|
||||
sess := ctx.Session()
|
||||
if !sess.HasFlash() {
|
||||
sess.SetFlash("name", ctx.FormValue("name"))
|
||||
// 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"))
|
||||
}
|
||||
|
|
|
@ -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("<h1> Unauthorized Page! </h1>")
|
||||
// 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("<h1> Unauthorized Page! </h1>")
|
||||
// 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)
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user