mirror of
https://github.com/kataras/iris.git
synced 2025-01-24 11:11:03 +01:00
dd26fbf26d
Former-commit-id: 3c5f6c97629a2a6ae44e62f2900edd32c0329b50
59 lines
1.8 KiB
Go
59 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/kataras/iris"
|
|
"github.com/kataras/iris/context"
|
|
"github.com/kataras/iris/middleware/basicauth"
|
|
)
|
|
|
|
func main() {
|
|
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"
|
|
Expires: time.Duration(30) * time.Minute,
|
|
}
|
|
|
|
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: % ", 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())
|
|
})
|
|
}
|
|
|
|
// open http://localhost:8080/admin
|
|
app.Run(iris.Addr(":8080"))
|
|
}
|