2022-02-04 12:06:04 +03:00
|
|
|
package server
|
|
|
|
|
2023-01-01 18:26:21 +03:00
|
|
|
import (
|
2023-01-07 05:51:34 +03:00
|
|
|
"net/http"
|
|
|
|
|
2023-01-01 18:26:21 +03:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/usememos/memos/api"
|
|
|
|
"github.com/usememos/memos/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
type response struct {
|
2023-03-18 17:34:22 +03:00
|
|
|
Data any `json:"data"`
|
2023-01-01 18:26:21 +03:00
|
|
|
}
|
2022-02-04 12:06:04 +03:00
|
|
|
|
2023-03-18 17:34:22 +03:00
|
|
|
func composeResponse(data any) response {
|
2023-01-01 18:26:21 +03:00
|
|
|
return response{
|
2022-02-04 12:06:04 +03:00
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
}
|
2023-01-01 18:26:21 +03:00
|
|
|
|
2023-02-12 12:29:23 +03:00
|
|
|
func defaultGetRequestSkipper(c echo.Context) bool {
|
2023-01-07 05:51:34 +03:00
|
|
|
return c.Request().Method == http.MethodGet
|
|
|
|
}
|
|
|
|
|
2023-02-12 12:29:23 +03:00
|
|
|
func defaultAPIRequestSkipper(c echo.Context) bool {
|
2023-01-13 02:06:15 +03:00
|
|
|
path := c.Path()
|
|
|
|
return common.HasPrefixes(path, "/api")
|
|
|
|
}
|
|
|
|
|
2023-06-17 16:25:46 +03:00
|
|
|
func (s *Server) defaultAuthSkipper(c echo.Context) bool {
|
2023-01-01 18:26:21 +03:00
|
|
|
ctx := c.Request().Context()
|
|
|
|
path := c.Path()
|
|
|
|
|
|
|
|
// Skip auth.
|
2023-06-17 16:25:46 +03:00
|
|
|
if common.HasPrefixes(path, "/api/v1/auth") {
|
2023-01-01 18:26:21 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is openId in query string and related user is found, then skip auth.
|
|
|
|
openID := c.QueryParam("openId")
|
|
|
|
if openID != "" {
|
|
|
|
userFind := &api.UserFind{
|
|
|
|
OpenID: &openID,
|
|
|
|
}
|
2023-06-17 16:25:46 +03:00
|
|
|
user, err := s.Store.FindUser(ctx, userFind)
|
2023-01-01 18:26:21 +03:00
|
|
|
if err != nil && common.ErrorCode(err) != common.NotFound {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if user != nil {
|
|
|
|
// Stores userID into context.
|
|
|
|
c.Set(getUserIDContextKey(), user.ID)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|