auth.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package server
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "github.com/usememos/memos/api"
  7. "github.com/usememos/memos/common"
  8. metric "github.com/usememos/memos/plugin/metrics"
  9. "github.com/labstack/echo/v4"
  10. "golang.org/x/crypto/bcrypt"
  11. )
  12. func (s *Server) registerAuthRoutes(g *echo.Group) {
  13. g.POST("/auth/signin", func(c echo.Context) error {
  14. ctx := c.Request().Context()
  15. signin := &api.Signin{}
  16. if err := json.NewDecoder(c.Request().Body).Decode(signin); err != nil {
  17. return echo.NewHTTPError(http.StatusBadRequest, "Malformatted signin request").SetInternal(err)
  18. }
  19. userFind := &api.UserFind{
  20. Username: &signin.Username,
  21. }
  22. user, err := s.Store.FindUser(ctx, userFind)
  23. if err != nil && common.ErrorCode(err) != common.NotFound {
  24. return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user by username %s", signin.Username)).SetInternal(err)
  25. }
  26. if user == nil {
  27. return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("User not found with username %s", signin.Username))
  28. } else if user.RowStatus == api.Archived {
  29. return echo.NewHTTPError(http.StatusForbidden, fmt.Sprintf("User has been archived with username %s", signin.Username))
  30. }
  31. // Compare the stored hashed password, with the hashed version of the password that was received.
  32. if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(signin.Password)); err != nil {
  33. // If the two passwords don't match, return a 401 status.
  34. return echo.NewHTTPError(http.StatusUnauthorized, "Incorrect password").SetInternal(err)
  35. }
  36. if err = setUserSession(c, user); err != nil {
  37. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to set signin session").SetInternal(err)
  38. }
  39. s.Collector.Collect(ctx, &metric.Metric{
  40. Name: "user signed in",
  41. })
  42. c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
  43. if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil {
  44. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user response").SetInternal(err)
  45. }
  46. return nil
  47. })
  48. g.POST("/auth/signup", func(c echo.Context) error {
  49. ctx := c.Request().Context()
  50. signup := &api.Signup{}
  51. if err := json.NewDecoder(c.Request().Body).Decode(signup); err != nil {
  52. return echo.NewHTTPError(http.StatusBadRequest, "Malformatted signup request").SetInternal(err)
  53. }
  54. hostUserType := api.Host
  55. hostUserFind := api.UserFind{
  56. Role: &hostUserType,
  57. }
  58. hostUser, err := s.Store.FindUser(ctx, &hostUserFind)
  59. if err != nil && common.ErrorCode(err) != common.NotFound {
  60. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find host user").SetInternal(err)
  61. }
  62. if signup.Role == api.Host && hostUser != nil {
  63. return echo.NewHTTPError(http.StatusUnauthorized, "Site Host existed, please contact the site host to signin account firstly").SetInternal(err)
  64. }
  65. systemSettingAllowSignUpName := api.SystemSettingAllowSignUpName
  66. allowSignUpSetting, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{
  67. Name: &systemSettingAllowSignUpName,
  68. })
  69. if err != nil && common.ErrorCode(err) != common.NotFound {
  70. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find system setting").SetInternal(err)
  71. }
  72. allowSignUpSettingValue := false
  73. if allowSignUpSetting != nil {
  74. err = json.Unmarshal([]byte(allowSignUpSetting.Value), &allowSignUpSettingValue)
  75. if err != nil {
  76. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal system setting allow signup").SetInternal(err)
  77. }
  78. }
  79. if !allowSignUpSettingValue && hostUser != nil {
  80. return echo.NewHTTPError(http.StatusUnauthorized, "Site Host existed, please contact the site host to signin account firstly").SetInternal(err)
  81. }
  82. userCreate := &api.UserCreate{
  83. Username: signup.Username,
  84. Role: api.Role(signup.Role),
  85. Nickname: signup.Username,
  86. Password: signup.Password,
  87. OpenID: common.GenUUID(),
  88. }
  89. if err := userCreate.Validate(); err != nil {
  90. return echo.NewHTTPError(http.StatusBadRequest, "Invalid user create format").SetInternal(err)
  91. }
  92. passwordHash, err := bcrypt.GenerateFromPassword([]byte(signup.Password), bcrypt.DefaultCost)
  93. if err != nil {
  94. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate password hash").SetInternal(err)
  95. }
  96. userCreate.PasswordHash = string(passwordHash)
  97. user, err := s.Store.CreateUser(ctx, userCreate)
  98. if err != nil {
  99. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create user").SetInternal(err)
  100. }
  101. s.Collector.Collect(ctx, &metric.Metric{
  102. Name: "user signed up",
  103. })
  104. err = setUserSession(c, user)
  105. if err != nil {
  106. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to set signup session").SetInternal(err)
  107. }
  108. c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
  109. if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil {
  110. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode created user response").SetInternal(err)
  111. }
  112. return nil
  113. })
  114. g.POST("/auth/logout", func(c echo.Context) error {
  115. ctx := c.Request().Context()
  116. err := removeUserSession(c)
  117. if err != nil {
  118. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to set logout session").SetInternal(err)
  119. }
  120. s.Collector.Collect(ctx, &metric.Metric{
  121. Name: "user logout",
  122. })
  123. return c.JSON(http.StatusOK, true)
  124. })
  125. }