memos/server/auth.go

119 lines
4.3 KiB
Go
Raw Normal View History

2022-02-03 10:32:03 +03:00
package server
import (
"encoding/json"
2022-02-03 10:32:03 +03:00
"fmt"
"net/http"
2022-06-27 17:09:06 +03:00
"github.com/usememos/memos/api"
"github.com/usememos/memos/common"
2022-02-03 10:32:03 +03:00
"github.com/labstack/echo/v4"
2022-02-06 11:19:20 +03:00
"golang.org/x/crypto/bcrypt"
2022-02-03 10:32:03 +03:00
)
func (s *Server) registerAuthRoutes(g *echo.Group) {
g.POST("/auth/login", func(c echo.Context) error {
login := &api.Login{}
if err := json.NewDecoder(c.Request().Body).Decode(login); err != nil {
2022-02-03 10:32:03 +03:00
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted login request").SetInternal(err)
}
userFind := &api.UserFind{
Email: &login.Email,
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 {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user by email %s", login.Email)).SetInternal(err)
2022-02-03 10:32:03 +03:00
}
if user == nil {
return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("User not found with email %s", login.Email))
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", login.Email))
2022-02-03 10:32:03 +03:00
}
2022-02-06 11:19:20 +03:00
// Compare the stored hashed password, with the hashed version of the password that was received.
if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(login.Password)); err != nil {
// If the two passwords don't match, return a 401 status.
return echo.NewHTTPError(http.StatusUnauthorized, "Incorrect password").SetInternal(err)
2022-02-03 10:32:03 +03:00
}
2022-03-28 19:01:34 +03:00
if err = setUserSession(c, user); err != nil {
2022-02-03 19:56:44 +03:00
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to set login session").SetInternal(err)
}
2022-02-03 10:32:03 +03:00
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
2022-02-04 12:06:04 +03:00
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil {
2022-02-05 06:43:25 +03:00
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user response").SetInternal(err)
2022-02-03 10:32:03 +03:00
}
return nil
})
2022-02-18 17:21:10 +03:00
2022-02-03 10:32:03 +03:00
g.POST("/auth/logout", func(c echo.Context) error {
2022-02-03 19:56:44 +03:00
err := removeUserSession(c)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to set logout session").SetInternal(err)
}
2022-02-03 10:32:03 +03:00
c.Response().WriteHeader(http.StatusOK)
return nil
})
2022-02-18 17:21:10 +03:00
2022-02-03 10:32:03 +03:00
g.POST("/auth/signup", func(c echo.Context) error {
// Don't allow to signup by this api if site owner existed.
ownerUserType := api.Owner
ownerUserFind := api.UserFind{
Role: &ownerUserType,
}
2022-05-16 02:37:23 +03:00
ownerUser, err := s.Store.FindUser(&ownerUserFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find owner user").SetInternal(err)
}
if ownerUser != nil {
return echo.NewHTTPError(http.StatusUnauthorized, "Site Owner existed, please contact the site owner to signin account firstly.").SetInternal(err)
}
2022-02-03 10:32:03 +03:00
signup := &api.Signup{}
if err := json.NewDecoder(c.Request().Body).Decode(signup); err != nil {
2022-02-03 19:56:44 +03:00
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted signup request").SetInternal(err)
2022-02-03 10:32:03 +03:00
}
2022-03-28 19:01:34 +03:00
// Validate signup form.
// We can do stricter checks later.
if len(signup.Email) < 6 {
return echo.NewHTTPError(http.StatusBadRequest, "Email is too short, minimum length is 6.")
}
2022-03-28 19:01:34 +03:00
if len(signup.Password) < 6 {
2022-02-06 11:19:20 +03:00
return echo.NewHTTPError(http.StatusBadRequest, "Password is too short, minimum length is 6.")
}
passwordHash, err := bcrypt.GenerateFromPassword([]byte(signup.Password), bcrypt.DefaultCost)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate password hash").SetInternal(err)
}
2022-02-03 10:32:03 +03:00
userCreate := &api.UserCreate{
Email: signup.Email,
Role: api.Role(signup.Role),
2022-02-06 11:19:20 +03:00
Name: signup.Name,
PasswordHash: string(passwordHash),
2022-05-02 21:05:43 +03:00
OpenID: common.GenUUID(),
2022-02-03 10:32:03 +03:00
}
user, err := s.Store.CreateUser(userCreate)
2022-02-03 10:32:03 +03:00
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create user").SetInternal(err)
}
2022-02-03 19:56:44 +03:00
err = setUserSession(c, user)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to set signup session").SetInternal(err)
}
2022-02-03 10:32:03 +03:00
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
2022-02-04 12:06:04 +03:00
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil {
2022-02-05 06:43:25 +03:00
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode created user response").SetInternal(err)
2022-02-03 10:32:03 +03:00
}
return nil
})
}