memos/server/basic_auth.go

95 lines
2.3 KiB
Go
Raw Normal View History

2022-02-03 15:32:03 +08: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 (
userIdContextKey = "user-id"
)
func getUserIdContextKey() string {
return userIdContextKey
}
2022-02-04 00:56:44 +08:00
func setUserSession(c echo.Context, user *api.User) error {
2022-02-05 17:04:23 +08:00
sess, _ := session.Get("session", c)
2022-02-03 15:32:03 +08:00
sess.Options = &sessions.Options{
Path: "/",
MaxAge: 1000 * 3600 * 24 * 30,
HttpOnly: true,
}
2022-02-04 00:56:44 +08:00
sess.Values[userIdContextKey] = user.Id
2022-02-05 17:04:23 +08:00
err := sess.Save(c.Request(), c.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-02-04 00:56:44 +08:00
func removeUserSession(c echo.Context) error {
2022-02-05 17:04:23 +08:00
sess, _ := session.Get("session", c)
2022-02-03 15:32:03 +08:00
sess.Options = &sessions.Options{
Path: "/",
MaxAge: 0,
HttpOnly: true,
}
sess.Values[userIdContextKey] = nil
2022-02-05 17:04:23 +08:00
err := sess.Save(c.Request(), c.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-02-05 17:04:23 +08:00
// Use session to store user.id.
2022-02-04 18:54:24 +08:00
func BasicAuthMiddleware(us api.UserService, next echo.HandlerFunc) echo.HandlerFunc {
2022-02-03 15:32:03 +08:00
return func(c echo.Context) error {
2022-02-04 00:56:44 +08:00
// Skips auth
if common.HasPrefixes(c.Path(), "/api/auth") {
2022-02-03 15:32:03 +08:00
return next(c)
}
sess, err := session.Get("session", c)
if err != nil {
2022-02-04 18:54:24 +08:00
return echo.NewHTTPError(http.StatusUnauthorized, "Missing session").SetInternal(err)
2022-02-03 15:32:03 +08:00
}
2022-02-04 00:56:44 +08:00
userIdValue := sess.Values[userIdContextKey]
if userIdValue == nil {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing userId in session")
}
userId, err := strconv.Atoi(fmt.Sprintf("%v", userIdValue))
2022-02-03 15:32:03 +08:00
if err != nil {
return echo.NewHTTPError(http.StatusUnauthorized, "Failed to malformatted user id in the session.")
}
// Even if there is no error, we still need to make sure the user still exists.
principalFind := &api.UserFind{
Id: &userId,
}
user, err := us.FindUser(principalFind)
if err != nil {
2022-02-05 11:43:25 +08:00
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user by ID: %d", userId)).SetInternal(err)
2022-02-03 15:32:03 +08:00
}
if user == nil {
2022-02-05 11:43:25 +08:00
return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Not found user ID: %d", userId))
2022-02-03 15:32:03 +08:00
}
2022-02-05 11:43:25 +08:00
// Stores userId into context.
2022-02-03 15:32:03 +08:00
c.Set(getUserIdContextKey(), userId)
return next(c)
}
}