auth.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package middleware
  2. import (
  3. "fmt"
  4. "github.com/astaxie/beego"
  5. "github.com/astaxie/beego/context"
  6. "github.com/astaxie/beego/logs"
  7. "github.com/astaxie/beego/session"
  8. "github.com/beego/beego/v2/client/httplib"
  9. "log"
  10. "nginx-ui/server/base"
  11. "nginx-ui/server/config"
  12. "nginx-ui/server/models"
  13. "nginx-ui/server/modules/auth_token"
  14. "strings"
  15. )
  16. // 白名单,不需要登录即可访问
  17. var whitelist = map[string]bool{
  18. "/user/login": true,
  19. "/user/register": true,
  20. "/oauth2": true,
  21. "/oauth2/callback": true,
  22. "/ldap/login": true,
  23. "/ldap/server/active": true,
  24. "/user/resetPassword": true,
  25. "/wechat/webhook/gitlab": true,
  26. }
  27. func init() {
  28. beego.BConfig.WebConfig.Session.SessionAutoSetCookie = true
  29. }
  30. func checkThirdSession(ctx *context.Context, sess session.Store) {
  31. cfg := config.Config
  32. if !cfg.ThirdSession {
  33. return
  34. }
  35. cookie, err := ctx.Request.Cookie(cfg.ThirdSessionName)
  36. if err != nil {
  37. logs.Warn("no cookie", err)
  38. return
  39. }
  40. req := httplib.Get(cfg.ThirdSessionCheckUrl)
  41. req.SetEnableCookie(true)
  42. req.SetCookie(cookie)
  43. user := models.User{}
  44. err = req.ToJSON(&user)
  45. if err != nil {
  46. logs.Warn("check third session fail ", err)
  47. return
  48. }
  49. err = sess.Set("user", user)
  50. if err != nil {
  51. logs.Warn("set session data fail ", err)
  52. return
  53. }
  54. logs.Debug("check third session ok ", user)
  55. }
  56. func checkAuthToken(ctx *context.Context, sess session.Store, path string) (bool, error) {
  57. c := sess.Get("client")
  58. if c != nil {
  59. _, ok := c.(models.AuthToken)
  60. if ok {
  61. return true, nil
  62. }
  63. }
  64. token := ctx.Request.Header.Get("Token")
  65. if token == "" {
  66. return false, nil
  67. }
  68. t, err := auth_token.VerifyToken(token, path, ctx.Input.IP())
  69. if err != nil {
  70. return false, err
  71. }
  72. log.Printf("auth token: %v", t)
  73. _ = sess.Set("client", t)
  74. return true, nil
  75. }
  76. func AuthFilter(ctx *context.Context) {
  77. path := ctx.Request.URL.Path
  78. path = strings.TrimSuffix(path, "/")
  79. path = strings.TrimPrefix(path, config.Config.BaseApi)
  80. if whitelist[path] {
  81. logs.Debug("in whitelist ,skip ", ctx.Request.RequestURI, path)
  82. return
  83. }
  84. logs.Info(fmt.Sprintf("auth: %s,%s", ctx.Request.RequestURI, path))
  85. sess := ctx.Input.CruSession
  86. if sess == nil {
  87. logs.Warn("no session found in request")
  88. return
  89. }
  90. defer sess.SessionRelease(ctx.ResponseWriter)
  91. data := sess.Get("user")
  92. if data == nil {
  93. checkThirdSession(ctx, sess)
  94. }
  95. data = sess.Get("user")
  96. if data == nil {
  97. ok, err := checkAuthToken(ctx, sess, path)
  98. if err != nil {
  99. base.WriteError(ctx.ResponseWriter, err)
  100. return
  101. }
  102. if ok {
  103. return
  104. }
  105. }
  106. if data == nil {
  107. base.WriteForbidden(ctx.ResponseWriter)
  108. return
  109. }
  110. user := data.(models.User)
  111. if len(user.Account) == 0 {
  112. base.WriteForbidden(ctx.ResponseWriter)
  113. return
  114. }
  115. logs.Info(fmt.Sprintf("request uri: %s, uid: %s", ctx.Request.RequestURI, user.Account))
  116. }