forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (49 loc) · 1.95 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"time"
"gopkg.in/kataras/iris.v6"
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
"gopkg.in/kataras/iris.v6/middleware/basicauth"
)
func main() {
app := iris.New()
app.Adapt(iris.DevLogger()) // adapt a simple internal logger to print any errors
app.Adapt(httprouter.New()) // adapt a router, you can use gorillamux too
authConfig := basicauth.Config{
Users: map[string]string{"myusername": "mypassword", "mySecondusername": "mySecondpassword"},
Realm: "Authorization Required", // defaults to "Authorization Required"
ContextKey: "mycustomkey", // defaults to "user"
Expires: time.Duration(30) * time.Minute,
}
authentication := basicauth.New(authConfig)
app.Get("/", func(ctx *iris.Context) { ctx.Redirect("/admin") })
// to global app.Use(authentication) (or app.UseGlobal before the .Listen)
// to routes
/*
app.Get("/mysecret", authentication, func(ctx *iris.Context) {
username := ctx.GetString("mycustomkey") // the Contextkey from the authConfig
ctx.Writef("Hello authenticated user: %s ", username)
})
*/
// to party
needAuth := app.Party("/admin", authentication)
{
//http://localhost:8080/admin
needAuth.Get("/", func(ctx *iris.Context) {
username := ctx.GetString("mycustomkey") // 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 *iris.Context) {
username := ctx.GetString("mycustomkey") // 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 *iris.Context) {
username := authConfig.User(ctx) // shortcut for ctx.GetString("mycustomkey")
ctx.Writef("Hello authenticated user: %s from: %s ", username, ctx.Path())
})
}
// open http://localhost:8080/admin
app.Listen(":8080")
}