auth.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. "server/config"
  11. "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. }
  21. type ThirdSession struct {
  22. Enable bool
  23. CookieName string
  24. CheckUrl string
  25. }
  26. var thirdSession = ThirdSession{
  27. Enable: false,
  28. CookieName: "",
  29. CheckUrl: "",
  30. }
  31. func init() {
  32. beego.BConfig.WebConfig.Session.SessionAutoSetCookie = true
  33. thirdSession.Enable = beego.AppConfig.DefaultBool("thirdsessionenable", false)
  34. thirdSession.CookieName = beego.AppConfig.DefaultString("thirdsessionname", "")
  35. thirdSession.CheckUrl = beego.AppConfig.DefaultString("thirdsessioncheckurl", "")
  36. if thirdSession.Enable && (len(thirdSession.CookieName) == 0 || len(thirdSession.CheckUrl) == 0) {
  37. logs.Info("no thirdsessionname or thirdsessioncheckurl info,skip !")
  38. thirdSession.Enable = false
  39. }
  40. }
  41. func checkThirdSession(ctx *context.Context, sess session.Store) {
  42. if !thirdSession.Enable {
  43. return
  44. }
  45. cookie, err := ctx.Request.Cookie(thirdSession.CookieName)
  46. if err != nil {
  47. logs.Warn("no cookie", err)
  48. return
  49. }
  50. req := httplib.Get(thirdSession.CheckUrl)
  51. req.SetEnableCookie(true)
  52. req.SetCookie(cookie)
  53. user := models.User{}
  54. err = req.ToJSON(&user)
  55. if err != nil {
  56. logs.Warn("check third session fail ", err)
  57. return
  58. }
  59. err = sess.Set("user", user)
  60. if err != nil {
  61. logs.Warn("set session data fail ", err)
  62. return
  63. }
  64. logs.Debug("check third session ok ", user)
  65. }
  66. func AuthFilter(ctx *context.Context) {
  67. path := ctx.Request.RequestURI
  68. path = strings.TrimSuffix(path, "/")
  69. path = strings.TrimPrefix(path, config.Config.BaseApi)
  70. if whitelist[path] {
  71. logs.Debug("in whitelist ,skip ", ctx.Request.RequestURI, path)
  72. return
  73. }
  74. logs.Info(fmt.Sprintf("auth: %s,%s", ctx.Request.RequestURI, path))
  75. sess := ctx.Input.CruSession
  76. defer sess.SessionRelease(ctx.ResponseWriter)
  77. data := sess.Get("user")
  78. if data == nil {
  79. checkThirdSession(ctx, sess)
  80. }
  81. data = sess.Get("user")
  82. if data == nil {
  83. WriteForbidden(ctx.ResponseWriter)
  84. return
  85. }
  86. user := data.(models.User)
  87. if len(user.Account) == 0 {
  88. WriteForbidden(ctx.ResponseWriter)
  89. return
  90. }
  91. logs.Info(fmt.Sprintf("request uri: %s, uid: %s", ctx.Request.RequestURI, user.Account))
  92. }
  93. func WriteForbidden(w http.ResponseWriter) {
  94. w.WriteHeader(401)
  95. _, err := w.Write([]byte("401 Unauthorized\n"))
  96. if err != nil {
  97. logs.Warn("writeForbidden write error", err)
  98. return
  99. }
  100. }