2022-02-03 10:32:03 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-02-04 11:51:48 +03:00
|
|
|
"encoding/json"
|
2022-02-03 10:32:03 +03:00
|
|
|
"fmt"
|
|
|
|
"memos/api"
|
|
|
|
"memos/common"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) registerAuthRoutes(g *echo.Group) {
|
|
|
|
g.POST("/auth/login", func(c echo.Context) error {
|
|
|
|
login := &api.Login{}
|
2022-02-04 11:51:48 +03:00
|
|
|
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{
|
|
|
|
Name: &login.Name,
|
|
|
|
}
|
|
|
|
user, err := s.UserService.FindUser(userFind)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to authenticate user").SetInternal(err)
|
|
|
|
}
|
|
|
|
if user == nil {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("User not found: %s", login.Name))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare the stored hashed password, with the hashed version of the password that was received.
|
|
|
|
if login.Password != user.Password {
|
|
|
|
// If the two passwords don't match, return a 401 status.
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Incorrect password").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 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-03 10:32:03 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal create user response").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
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
|
|
|
|
})
|
|
|
|
g.POST("/auth/signup", func(c echo.Context) error {
|
|
|
|
signup := &api.Signup{}
|
2022-02-04 11:51:48 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
userFind := &api.UserFind{
|
|
|
|
Name: &signup.Name,
|
|
|
|
}
|
|
|
|
user, err := s.UserService.FindUser(userFind)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to authenticate user").SetInternal(err)
|
|
|
|
}
|
|
|
|
if user != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Exist user found: %s", signup.Name))
|
|
|
|
}
|
|
|
|
|
|
|
|
userCreate := &api.UserCreate{
|
|
|
|
Name: signup.Name,
|
|
|
|
Password: signup.Password,
|
|
|
|
OpenId: common.GenUUID(),
|
|
|
|
}
|
|
|
|
user, err = s.UserService.CreateUser(userCreate)
|
|
|
|
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-03 10:32:03 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal create user response").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|