|
|
|
@ -21,30 +21,30 @@ func getUserIDContextKey() string {
|
|
|
|
|
return userIDContextKey
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func setUserSession(c echo.Context, user *api.User) error {
|
|
|
|
|
sess, _ := session.Get("session", c)
|
|
|
|
|
func setUserSession(ctx echo.Context, user *api.User) error {
|
|
|
|
|
sess, _ := session.Get("session", ctx)
|
|
|
|
|
sess.Options = &sessions.Options{
|
|
|
|
|
Path: "/",
|
|
|
|
|
MaxAge: 1000 * 3600 * 24 * 30,
|
|
|
|
|
HttpOnly: true,
|
|
|
|
|
}
|
|
|
|
|
sess.Values[userIDContextKey] = user.ID
|
|
|
|
|
err := sess.Save(c.Request(), c.Response())
|
|
|
|
|
err := sess.Save(ctx.Request(), ctx.Response())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to set session, err: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func removeUserSession(c echo.Context) error {
|
|
|
|
|
sess, _ := session.Get("session", c)
|
|
|
|
|
func removeUserSession(ctx echo.Context) error {
|
|
|
|
|
sess, _ := session.Get("session", ctx)
|
|
|
|
|
sess.Options = &sessions.Options{
|
|
|
|
|
Path: "/",
|
|
|
|
|
MaxAge: 0,
|
|
|
|
|
HttpOnly: true,
|
|
|
|
|
}
|
|
|
|
|
sess.Values[userIDContextKey] = nil
|
|
|
|
|
err := sess.Save(c.Request(), c.Response())
|
|
|
|
|
err := sess.Save(ctx.Request(), ctx.Response())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to set session, err: %w", err)
|
|
|
|
|
}
|
|
|
|
@ -53,14 +53,14 @@ func removeUserSession(c echo.Context) error {
|
|
|
|
|
|
|
|
|
|
// Use session to store user.id.
|
|
|
|
|
func BasicAuthMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
|
return func(ctx echo.Context) error {
|
|
|
|
|
// Skip auth for some paths.
|
|
|
|
|
if common.HasPrefixes(c.Path(), "/api/auth", "/api/ping", "/api/status", "/api/user/:userId") {
|
|
|
|
|
return next(c)
|
|
|
|
|
if common.HasPrefixes(ctx.Path(), "/api/auth", "/api/ping", "/api/status", "/api/user/:userId") {
|
|
|
|
|
return next(ctx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If there is openId in query string and related user is found, then skip auth.
|
|
|
|
|
openID := c.QueryParam("openId")
|
|
|
|
|
openID := ctx.QueryParam("openId")
|
|
|
|
|
if openID != "" {
|
|
|
|
|
userFind := &api.UserFind{
|
|
|
|
|
OpenID: &openID,
|
|
|
|
@ -71,49 +71,49 @@ func BasicAuthMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
|
}
|
|
|
|
|
if user != nil {
|
|
|
|
|
// Stores userID into context.
|
|
|
|
|
c.Set(getUserIDContextKey(), user.ID)
|
|
|
|
|
return next(c)
|
|
|
|
|
ctx.Set(getUserIDContextKey(), user.ID)
|
|
|
|
|
return next(ctx)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if common.HasPrefixes(c.Path(), "/api/memo", "/api/tag", "/api/shortcut") && c.Request().Method == http.MethodGet {
|
|
|
|
|
if _, err := strconv.Atoi(c.QueryParam("creatorId")); err == nil {
|
|
|
|
|
return next(c)
|
|
|
|
|
needAuth := true
|
|
|
|
|
if common.HasPrefixes(ctx.Path(), "/api/memo", "/api/tag", "/api/shortcut") && ctx.Request().Method == http.MethodGet {
|
|
|
|
|
if _, err := strconv.Atoi(ctx.QueryParam("creatorId")); err == nil {
|
|
|
|
|
needAuth = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sess, err := session.Get("session", c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing session").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
sess, _ := session.Get("session", ctx)
|
|
|
|
|
userIDValue := sess.Values[userIDContextKey]
|
|
|
|
|
if userIDValue == nil && needAuth {
|
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing userID in session")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userIDValue := sess.Values[userIDContextKey]
|
|
|
|
|
if userIDValue == nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing userID in session")
|
|
|
|
|
}
|
|
|
|
|
userID, err := strconv.Atoi(fmt.Sprintf("%v", userIDValue))
|
|
|
|
|
if err != nil && needAuth {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to malformatted user id in the session.").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userID, err := strconv.Atoi(fmt.Sprintf("%v", userIDValue))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to malformatted user id in the session.").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
userFind := &api.UserFind{
|
|
|
|
|
ID: &userID,
|
|
|
|
|
}
|
|
|
|
|
user, err := s.Store.FindUser(userFind)
|
|
|
|
|
if err != nil && needAuth {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user by ID: %d", userID)).SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
if needAuth {
|
|
|
|
|
if user == nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Not found user ID: %d", userID))
|
|
|
|
|
} else if user.RowStatus == api.Archived {
|
|
|
|
|
return echo.NewHTTPError(http.StatusForbidden, fmt.Sprintf("User has been archived with email %s", user.Email))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Even if there is no error, we still need to make sure the user still exists.
|
|
|
|
|
userFind := &api.UserFind{
|
|
|
|
|
ID: &userID,
|
|
|
|
|
// Save userID into context.
|
|
|
|
|
ctx.Set(getUserIDContextKey(), userID)
|
|
|
|
|
}
|
|
|
|
|
user, err := s.Store.FindUser(userFind)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user by ID: %d", userID)).SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
if user == nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Not found user ID: %d", userID))
|
|
|
|
|
} else if user.RowStatus == api.Archived {
|
|
|
|
|
return echo.NewHTTPError(http.StatusForbidden, fmt.Sprintf("User has been archived with email %s", user.Email))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stores userID into context.
|
|
|
|
|
c.Set(getUserIDContextKey(), userID)
|
|
|
|
|
|
|
|
|
|
return next(c)
|
|
|
|
|
return next(ctx)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|