auth.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "net/http"
  10. "nginx-ui/server/config"
  11. "nginx-ui/server/models"
  12. "strings"
  13. )
  14. // 白名单,不需要登录即可访问
  15. var whitelist = map[string]bool{
  16. "/user/login": true,
  17. "/user/register": true,
  18. "/oauth2": true,
  19. "/oauth2/callback": true,
  20. "/ldap/login": true,
  21. "/ldap/server/active": true,
  22. "/user/resetPassword": true,
  23. "/wechat/webhook/gitlab": true,
  24. }
  25. var UnauthorizedResp = `{"code": 401, "msg":"未登录或者登录已过期!"}`
  26. func init() {
  27. beego.BConfig.WebConfig.Session.SessionAutoSetCookie = true
  28. }
  29. func checkThirdSession(ctx *context.Context, sess session.Store) {
  30. cfg := config.Config
  31. if !cfg.ThirdSession {
  32. return
  33. }
  34. cookie, err := ctx.Request.Cookie(cfg.ThirdSessionName)
  35. if err != nil {
  36. logs.Warn("no cookie", err)
  37. return
  38. }
  39. req := httplib.Get(cfg.ThirdSessionCheckUrl)
  40. req.SetEnableCookie(true)
  41. req.SetCookie(cookie)
  42. user := models.User{}
  43. err = req.ToJSON(&user)
  44. if err != nil {
  45. logs.Warn("check third session fail ", err)
  46. return
  47. }
  48. err = sess.Set("user", user)
  49. if err != nil {
  50. logs.Warn("set session data fail ", err)
  51. return
  52. }
  53. logs.Debug("check third session ok ", user)
  54. }
  55. func AuthFilter(ctx *context.Context) {
  56. path := ctx.Request.URL.Path
  57. path = strings.TrimSuffix(path, "/")
  58. path = strings.TrimPrefix(path, config.Config.BaseApi)
  59. if whitelist[path] {
  60. logs.Debug("in whitelist ,skip ", ctx.Request.RequestURI, path)
  61. return
  62. }
  63. logs.Info(fmt.Sprintf("auth: %s,%s", ctx.Request.RequestURI, path))
  64. sess := ctx.Input.CruSession
  65. if sess == nil {
  66. logs.Warn("no session found in request")
  67. return
  68. }
  69. defer sess.SessionRelease(ctx.ResponseWriter)
  70. data := sess.Get("user")
  71. if data == nil {
  72. checkThirdSession(ctx, sess)
  73. }
  74. data = sess.Get("user")
  75. if data == nil {
  76. WriteForbidden(ctx.ResponseWriter)
  77. return
  78. }
  79. user := data.(models.User)
  80. if len(user.Account) == 0 {
  81. WriteForbidden(ctx.ResponseWriter)
  82. return
  83. }
  84. logs.Info(fmt.Sprintf("request uri: %s, uid: %s", ctx.Request.RequestURI, user.Account))
  85. }
  86. func WriteForbidden(w http.ResponseWriter) {
  87. w.WriteHeader(401)
  88. w.Header().Set("Content-Type", "application/json")
  89. _, err := w.Write([]byte(UnauthorizedResp))
  90. if err != nil {
  91. logs.Warn("writeForbidden write error", err)
  92. return
  93. }
  94. }