memos/server/acl.go

96 lines
2.4 KiB
Go
Raw Normal View History

2022-02-03 15:32:03 +08:00
package server
import (
"fmt"
"net/http"
"strconv"
2022-06-27 22:09:06 +08:00
"github.com/usememos/memos/api"
"github.com/usememos/memos/common"
2022-02-03 15:32:03 +08:00
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
)
var (
2022-05-03 02:05:43 +08:00
userIDContextKey = "user-id"
sessionName = "memos_session"
2022-02-03 15:32:03 +08:00
)
2022-05-03 02:05:43 +08:00
func getUserIDContextKey() string {
return userIDContextKey
2022-02-03 15:32:03 +08:00
}
func setUserSession(ctx echo.Context, user *api.User) error {
sess, _ := session.Get(sessionName, ctx)
2022-02-03 15:32:03 +08:00
sess.Options = &sessions.Options{
Path: "/",
2022-11-01 21:03:33 +08:00
MaxAge: 3600 * 24 * 30,
2022-02-03 15:32:03 +08:00
HttpOnly: true,
2022-12-30 00:17:19 +08:00
SameSite: http.SameSiteStrictMode,
2022-02-03 15:32:03 +08:00
}
2022-05-03 02:05:43 +08:00
sess.Values[userIDContextKey] = user.ID
err := sess.Save(ctx.Request(), ctx.Response())
2022-02-04 00:56:44 +08:00
if err != nil {
2022-02-04 18:54:24 +08:00
return fmt.Errorf("failed to set session, err: %w", err)
2022-02-04 00:56:44 +08:00
}
return nil
2022-02-03 15:32:03 +08:00
}
func removeUserSession(ctx echo.Context) error {
sess, _ := session.Get(sessionName, ctx)
2022-02-03 15:32:03 +08:00
sess.Options = &sessions.Options{
Path: "/",
MaxAge: 0,
HttpOnly: true,
}
2022-05-03 02:05:43 +08:00
sess.Values[userIDContextKey] = nil
err := sess.Save(ctx.Request(), ctx.Response())
2022-02-04 00:56:44 +08:00
if err != nil {
2022-02-04 18:54:24 +08:00
return fmt.Errorf("failed to set session, err: %w", err)
2022-02-04 00:56:44 +08:00
}
return nil
2022-02-03 15:32:03 +08:00
}
2022-07-27 19:45:37 +08:00
func aclMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc {
2022-08-07 09:23:46 +08:00
return func(c echo.Context) error {
ctx := c.Request().Context()
path := c.Path()
2023-02-12 17:29:23 +08:00
if s.defaultAuthSkipper(c) {
2022-08-07 09:23:46 +08:00
return next(c)
2022-07-28 20:09:25 +08:00
}
sess, _ := session.Get(sessionName, c)
userIDValue := sess.Values[userIDContextKey]
if userIDValue != nil {
userID, _ := strconv.Atoi(fmt.Sprintf("%v", userIDValue))
userFind := &api.UserFind{
ID: &userID,
}
user, err := s.Store.FindUser(ctx, userFind)
if err != nil && common.ErrorCode(err) != common.NotFound {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user by ID: %d", userID)).SetInternal(err)
}
if user != nil {
if user.RowStatus == api.Archived {
return echo.NewHTTPError(http.StatusForbidden, fmt.Sprintf("User has been archived with username %s", user.Username))
2022-07-27 19:45:37 +08:00
}
c.Set(getUserIDContextKey(), userID)
}
2022-07-27 19:45:37 +08:00
}
2022-02-04 00:56:44 +08:00
if common.HasPrefixes(path, "/api/ping", "/api/status", "/api/idp", "/api/user/:id", "/api/memo") && c.Request().Method == http.MethodGet {
2022-09-05 21:14:17 +08:00
return next(c)
}
2022-08-07 09:23:46 +08:00
userID := c.Get(getUserIDContextKey())
2022-07-27 19:45:37 +08:00
if userID == nil {
2022-07-28 20:09:25 +08:00
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
2022-02-03 15:32:03 +08:00
}
2022-02-06 00:25:41 +08:00
2022-08-07 09:23:46 +08:00
return next(c)
2022-02-03 15:32:03 +08:00
}
}