auth.go 2.4 KB

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