example_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package auth_test
  2. import (
  3. "fmt"
  4. bm "go-common/library/net/http/blademaster"
  5. "go-common/library/net/http/blademaster/middleware/auth"
  6. "go-common/library/net/metadata"
  7. "go-common/library/net/rpc/warden"
  8. )
  9. // This example create a identify middleware instance and attach to several path,
  10. // it will validate request by specified policy and put extra information into context. e.g., `mid`.
  11. // It provides additional handler functions to provide the identification for your business handler.
  12. func Example() {
  13. authn := auth.New(&auth.Config{
  14. Identify: &warden.ClientConfig{},
  15. DisableCSRF: false,
  16. })
  17. e := bm.DefaultServer(nil)
  18. // mark `/user` path as User policy
  19. e.GET("/user", authn.User, func(ctx *bm.Context) {
  20. mid := metadata.Int64(ctx, metadata.Mid)
  21. ctx.JSON(fmt.Sprintf("%d", mid), nil)
  22. })
  23. // mark `/mobile` path as UserMobile policy
  24. e.GET("/mobile", authn.UserMobile, func(ctx *bm.Context) {
  25. mid := metadata.Int64(ctx, metadata.Mid)
  26. ctx.JSON(fmt.Sprintf("%d", mid), nil)
  27. })
  28. // mark `/web` path as UserWeb policy
  29. e.GET("/web", authn.UserWeb, func(ctx *bm.Context) {
  30. mid := metadata.Int64(ctx, metadata.Mid)
  31. ctx.JSON(fmt.Sprintf("%d", mid), nil)
  32. })
  33. // mark `/guest` path as Guest policy
  34. e.GET("/guest", authn.Guest, func(ctx *bm.Context) {
  35. mid := metadata.Int64(ctx, metadata.Mid)
  36. ctx.JSON(fmt.Sprintf("%d", mid), nil)
  37. })
  38. e.Run(":18080")
  39. }