2022-02-04 17:06:04 +08:00
|
|
|
package server
|
|
|
|
|
2023-01-01 23:26:21 +08:00
|
|
|
import (
|
2023-01-07 10:51:34 +08:00
|
|
|
"net/http"
|
|
|
|
|
2023-01-01 23:26:21 +08:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/usememos/memos/api"
|
|
|
|
"github.com/usememos/memos/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
type response struct {
|
|
|
|
Data interface{} `json:"data"`
|
|
|
|
}
|
2022-02-04 17:06:04 +08:00
|
|
|
|
2023-01-01 23:26:21 +08:00
|
|
|
func composeResponse(data interface{}) response {
|
|
|
|
return response{
|
2022-02-04 17:06:04 +08:00
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
}
|
2023-01-01 23:26:21 +08:00
|
|
|
|
2023-01-07 10:51:34 +08:00
|
|
|
func DefaultGetRequestSkipper(c echo.Context) bool {
|
|
|
|
return c.Request().Method == http.MethodGet
|
|
|
|
}
|
|
|
|
|
2023-01-13 07:06:15 +08:00
|
|
|
func DefaultAPIRequestSkipper(c echo.Context) bool {
|
|
|
|
path := c.Path()
|
|
|
|
return common.HasPrefixes(path, "/api")
|
|
|
|
}
|
|
|
|
|
2023-01-01 23:26:21 +08:00
|
|
|
func (server *Server) DefaultAuthSkipper(c echo.Context) bool {
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
path := c.Path()
|
|
|
|
|
|
|
|
// Skip auth.
|
|
|
|
if common.HasPrefixes(path, "/api/auth") {
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
user, err := server.Store.FindUser(ctx, userFind)
|
|
|
|
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
|
|
|
|
}
|