basic auth

HTTP basic auth中间件,提供简单的认证方式,建议只用于内部管理系统。

Example

  1. package main
  2. import (
  3. "bytes"
  4. "github.com/vicanso/elton"
  5. "github.com/vicanso/elton/middleware"
  6. "github.com/vicanso/hes"
  7. )
  8. func main() {
  9. e := elton.New()
  10. e.Use(middleware.NewBasicAuth(middleware.BasicAuthConfig{
  11. Validate: func(account, pwd string, c *elton.Context) (bool, error) {
  12. if account == "tree.xie" && pwd == "password" {
  13. return true, nil
  14. }
  15. if account == "n" {
  16. return false, hes.New("account is invalid")
  17. }
  18. return false, nil
  19. },
  20. }))
  21. e.GET("/", func(c *elton.Context) (err error) {
  22. c.BodyBuffer = bytes.NewBufferString("hello world")
  23. return
  24. })
  25. err := e.ListenAndServe(":3000")
  26. if err != nil {
  27. panic(err)
  28. }
  29. }