common.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package server
  2. import (
  3. "net/http"
  4. "github.com/labstack/echo/v4"
  5. "github.com/usememos/memos/api"
  6. "github.com/usememos/memos/common"
  7. )
  8. type response struct {
  9. Data interface{} `json:"data"`
  10. }
  11. func composeResponse(data interface{}) response {
  12. return response{
  13. Data: data,
  14. }
  15. }
  16. func defaultGetRequestSkipper(c echo.Context) bool {
  17. return c.Request().Method == http.MethodGet
  18. }
  19. func defaultAPIRequestSkipper(c echo.Context) bool {
  20. path := c.Path()
  21. return common.HasPrefixes(path, "/api")
  22. }
  23. func (server *Server) defaultAuthSkipper(c echo.Context) bool {
  24. ctx := c.Request().Context()
  25. path := c.Path()
  26. // Skip auth.
  27. if common.HasPrefixes(path, "/api/auth") {
  28. return true
  29. }
  30. // If there is openId in query string and related user is found, then skip auth.
  31. openID := c.QueryParam("openId")
  32. if openID != "" {
  33. userFind := &api.UserFind{
  34. OpenID: &openID,
  35. }
  36. user, err := server.Store.FindUser(ctx, userFind)
  37. if err != nil && common.ErrorCode(err) != common.NotFound {
  38. return false
  39. }
  40. if user != nil {
  41. // Stores userID into context.
  42. c.Set(getUserIDContextKey(), user.ID)
  43. return true
  44. }
  45. }
  46. return false
  47. }