2022-02-03 15:32:03 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-02-04 16:51:48 +08:00
|
|
|
"encoding/json"
|
2022-02-03 15:32:03 +08:00
|
|
|
"fmt"
|
|
|
|
"memos/api"
|
|
|
|
"memos/common"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
2022-02-06 16:19:20 +08:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2022-02-03 15:32:03 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) registerAuthRoutes(g *echo.Group) {
|
|
|
|
g.POST("/auth/login", func(c echo.Context) error {
|
|
|
|
login := &api.Login{}
|
2022-02-04 16:51:48 +08:00
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(login); err != nil {
|
2022-02-03 15:32:03 +08:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted login request").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
userFind := &api.UserFind{
|
2022-05-15 10:57:54 +08:00
|
|
|
Email: &login.Email,
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-05-16 07:37:23 +08:00
|
|
|
user, err := s.Store.FindUser(userFind)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
2022-05-15 10:57:54 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user by email %s", login.Email)).SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
if user == nil {
|
2022-05-15 10:57:54 +08:00
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("User not found with email %s", login.Email))
|
2022-05-19 18:32:04 +08: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 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
2022-02-06 16:19:20 +08: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 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
2022-03-29 00:01:34 +08:00
|
|
|
if err = setUserSession(c, user); err != nil {
|
2022-02-04 00:56:44 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to set login session").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-02-04 17:06:04 +08:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil {
|
2022-02-05 11:43:25 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user response").SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-18 22:21:10 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
g.POST("/auth/logout", func(c echo.Context) error {
|
2022-02-04 00:56:44 +08:00
|
|
|
err := removeUserSession(c)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to set logout session").SetInternal(err)
|
|
|
|
}
|
2022-02-03 15:32:03 +08:00
|
|
|
|
|
|
|
c.Response().WriteHeader(http.StatusOK)
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-18 22:21:10 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
g.POST("/auth/signup", func(c echo.Context) error {
|
2022-05-15 10:57:54 +08:00
|
|
|
// Don't allow to signup by this api if site owner existed.
|
|
|
|
ownerUserType := api.Owner
|
|
|
|
ownerUserFind := api.UserFind{
|
|
|
|
Role: &ownerUserType,
|
|
|
|
}
|
2022-05-16 07:37:23 +08:00
|
|
|
ownerUser, err := s.Store.FindUser(&ownerUserFind)
|
2022-05-15 10:57:54 +08:00
|
|
|
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 15:32:03 +08:00
|
|
|
signup := &api.Signup{}
|
2022-02-04 16:51:48 +08:00
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(signup); err != nil {
|
2022-02-04 00:56:44 +08:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted signup request").SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
2022-03-29 00:01:34 +08:00
|
|
|
// Validate signup form.
|
|
|
|
// We can do stricter checks later.
|
2022-05-15 10:57:54 +08:00
|
|
|
if len(signup.Email) < 6 {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Email is too short, minimum length is 6.")
|
|
|
|
}
|
2022-03-29 00:01:34 +08:00
|
|
|
if len(signup.Password) < 6 {
|
2022-02-06 16:19:20 +08: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 15:32:03 +08:00
|
|
|
userCreate := &api.UserCreate{
|
2022-05-15 10:57:54 +08:00
|
|
|
Email: signup.Email,
|
|
|
|
Role: api.Role(signup.Role),
|
2022-02-06 16:19:20 +08:00
|
|
|
Name: signup.Name,
|
|
|
|
PasswordHash: string(passwordHash),
|
2022-05-03 02:05:43 +08:00
|
|
|
OpenID: common.GenUUID(),
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-05-16 22:19:39 +08:00
|
|
|
user, err := s.Store.CreateUser(userCreate)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create user").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-02-04 00:56:44 +08:00
|
|
|
err = setUserSession(c, user)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to set signup session").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-02-04 17:06:04 +08:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil {
|
2022-02-05 11:43:25 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode created user response").SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|