memos/server/basic_auth.go

98 lines
2.5 KiB
Go
Raw Normal View History

2022-02-03 10:32:03 +03:00
package server
import (
"fmt"
"memos/api"
"memos/common"
"net/http"
"strconv"
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
)
var (
2022-05-02 21:05:43 +03:00
userIDContextKey = "user-id"
2022-02-03 10:32:03 +03:00
)
2022-05-02 21:05:43 +03:00
func getUserIDContextKey() string {
return userIDContextKey
2022-02-03 10:32:03 +03:00
}
2022-02-03 19:56:44 +03:00
func setUserSession(c echo.Context, user *api.User) error {
2022-02-05 12:04:23 +03:00
sess, _ := session.Get("session", c)
2022-02-03 10:32:03 +03:00
sess.Options = &sessions.Options{
Path: "/",
MaxAge: 1000 * 3600 * 24 * 30,
HttpOnly: true,
}
2022-05-02 21:05:43 +03:00
sess.Values[userIDContextKey] = user.ID
2022-02-05 12:04:23 +03:00
err := sess.Save(c.Request(), c.Response())
2022-02-03 19:56:44 +03:00
if err != nil {
2022-02-04 13:54:24 +03:00
return fmt.Errorf("failed to set session, err: %w", err)
2022-02-03 19:56:44 +03:00
}
return nil
2022-02-03 10:32:03 +03:00
}
2022-02-03 19:56:44 +03:00
func removeUserSession(c echo.Context) error {
2022-02-05 12:04:23 +03:00
sess, _ := session.Get("session", c)
2022-02-03 10:32:03 +03:00
sess.Options = &sessions.Options{
Path: "/",
MaxAge: 0,
HttpOnly: true,
}
2022-05-02 21:05:43 +03:00
sess.Values[userIDContextKey] = nil
2022-02-05 12:04:23 +03:00
err := sess.Save(c.Request(), c.Response())
2022-02-03 19:56:44 +03:00
if err != nil {
2022-02-04 13:54:24 +03:00
return fmt.Errorf("failed to set session, err: %w", err)
2022-02-03 19:56:44 +03:00
}
return nil
2022-02-03 10:32:03 +03:00
}
2022-02-05 12:04:23 +03:00
// Use session to store user.id.
2022-05-16 02:37:23 +03:00
func BasicAuthMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc {
2022-02-03 10:32:03 +03:00
return func(c echo.Context) error {
2022-02-03 19:56:44 +03:00
// Skips auth
if common.HasPrefixes(c.Path(), "/api/auth", "/api/ping", "/api/status") {
2022-02-03 10:32:03 +03:00
return next(c)
}
sess, err := session.Get("session", c)
if err != nil {
2022-02-04 13:54:24 +03:00
return echo.NewHTTPError(http.StatusUnauthorized, "Missing session").SetInternal(err)
2022-02-03 10:32:03 +03:00
}
2022-02-03 19:56:44 +03:00
2022-05-02 21:05:43 +03:00
userIDValue := sess.Values[userIDContextKey]
if userIDValue == nil {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing userID in session")
2022-02-03 19:56:44 +03:00
}
2022-05-02 21:05:43 +03:00
userID, err := strconv.Atoi(fmt.Sprintf("%v", userIDValue))
2022-02-03 10:32:03 +03:00
if err != nil {
2022-02-05 19:25:41 +03:00
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to malformatted user id in the session.").SetInternal(err)
2022-02-03 10:32:03 +03:00
}
// Even if there is no error, we still need to make sure the user still exists.
2022-02-05 19:25:41 +03:00
userFind := &api.UserFind{
2022-05-02 21:05:43 +03:00
ID: &userID,
2022-02-03 10:32:03 +03:00
}
2022-05-16 02:37:23 +03:00
user, err := s.Store.FindUser(userFind)
2022-02-03 10:32:03 +03:00
if err != nil {
2022-05-02 21:05:43 +03:00
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user by ID: %d", userID)).SetInternal(err)
2022-02-03 10:32:03 +03:00
}
if user == nil {
2022-05-02 21:05:43 +03:00
return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Not found user ID: %d", userID))
2022-05-19 13:32:04 +03:00
} else if user.RowStatus == api.Archived {
return echo.NewHTTPError(http.StatusForbidden, fmt.Sprintf("User has been archived with email %s", user.Email))
2022-02-03 10:32:03 +03:00
}
2022-05-02 21:05:43 +03:00
// Stores userID into context.
c.Set(getUserIDContextKey(), userID)
2022-02-05 19:25:41 +03:00
2022-02-03 10:32:03 +03:00
return next(c)
}
}