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"
|
|
|
|
"net/http"
|
2023-02-19 09:50:30 +08:00
|
|
|
"regexp"
|
2022-02-03 15:32:03 +08:00
|
|
|
|
2023-01-01 23:55:02 +08:00
|
|
|
"github.com/pkg/errors"
|
2022-06-27 22:09:06 +08:00
|
|
|
"github.com/usememos/memos/api"
|
|
|
|
"github.com/usememos/memos/common"
|
2023-02-19 09:50:30 +08:00
|
|
|
"github.com/usememos/memos/plugin/idp"
|
|
|
|
"github.com/usememos/memos/plugin/idp/oauth2"
|
|
|
|
"github.com/usememos/memos/store"
|
2022-06-27 22:09:06 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2023-04-02 09:28:02 +08:00
|
|
|
func (s *Server) registerAuthRoutes(g *echo.Group, secret string) {
|
2022-07-07 23:11:20 +08:00
|
|
|
g.POST("/auth/signin", func(c echo.Context) error {
|
2022-08-07 09:23:46 +08:00
|
|
|
ctx := c.Request().Context()
|
2023-01-01 23:26:21 +08:00
|
|
|
signin := &api.SignIn{}
|
2022-07-07 23:11:20 +08:00
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(signin); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted signin request").SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
userFind := &api.UserFind{
|
2022-11-23 22:27:21 +08:00
|
|
|
Username: &signin.Username,
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-08-07 09:23:46 +08:00
|
|
|
user, err := s.Store.FindUser(ctx, userFind)
|
2022-09-09 20:00:04 +08:00
|
|
|
if err != nil && common.ErrorCode(err) != common.NotFound {
|
2023-03-01 22:33:43 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Incorrect login credentials, please try again")
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
if user == nil {
|
2023-03-01 22:33:43 +08:00
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Incorrect login credentials, please try again")
|
2022-05-19 18:32:04 +08:00
|
|
|
} else if user.RowStatus == api.Archived {
|
2022-11-23 22:27:21 +08:00
|
|
|
return echo.NewHTTPError(http.StatusForbidden, fmt.Sprintf("User has been archived with username %s", signin.Username))
|
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.
|
2022-07-07 23:11:20 +08:00
|
|
|
if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(signin.Password)); err != nil {
|
2022-02-06 16:19:20 +08:00
|
|
|
// If the two passwords don't match, return a 401 status.
|
2023-03-01 22:33:43 +08:00
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Incorrect login credentials, please try again")
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
2023-04-07 08:52:13 +08:00
|
|
|
if err := GenerateTokensAndSetCookies(c, user, secret); err != nil {
|
2023-04-02 09:28:02 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate tokens").SetInternal(err)
|
2022-02-04 00:56:44 +08:00
|
|
|
}
|
2023-01-01 23:55:02 +08:00
|
|
|
if err := s.createUserAuthSignInActivity(c, user); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err)
|
|
|
|
}
|
2023-02-17 23:55:56 +08:00
|
|
|
return c.JSON(http.StatusOK, composeResponse(user))
|
2022-02-03 15:32:03 +08:00
|
|
|
})
|
2022-02-18 22:21:10 +08:00
|
|
|
|
2023-02-19 09:50:30 +08:00
|
|
|
g.POST("/auth/signin/sso", func(c echo.Context) error {
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
signin := &api.SSOSignIn{}
|
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(signin); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted signin request").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
identityProviderMessage, err := s.Store.GetIdentityProvider(ctx, &store.FindIdentityProviderMessage{
|
|
|
|
ID: &signin.IdentityProviderID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find identity provider").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var userInfo *idp.IdentityProviderUserInfo
|
|
|
|
if identityProviderMessage.Type == store.IdentityProviderOAuth2 {
|
|
|
|
oauth2IdentityProvider, err := oauth2.NewIdentityProvider(identityProviderMessage.Config.OAuth2Config)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create identity provider instance").SetInternal(err)
|
|
|
|
}
|
|
|
|
token, err := oauth2IdentityProvider.ExchangeToken(ctx, signin.RedirectURI, signin.Code)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to exchange token").SetInternal(err)
|
|
|
|
}
|
|
|
|
userInfo, err = oauth2IdentityProvider.UserInfo(token)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get user info").SetInternal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
identifierFilter := identityProviderMessage.IdentifierFilter
|
|
|
|
if identifierFilter != "" {
|
|
|
|
identifierFilterRegex, err := regexp.Compile(identifierFilter)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compile identifier filter").SetInternal(err)
|
|
|
|
}
|
|
|
|
if !identifierFilterRegex.MatchString(userInfo.Identifier) {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Access denied, identifier does not match the filter.").SetInternal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := s.Store.FindUser(ctx, &api.UserFind{
|
|
|
|
Username: &userInfo.Identifier,
|
|
|
|
})
|
|
|
|
if err != nil && common.ErrorCode(err) != common.NotFound {
|
2023-03-01 22:33:43 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Incorrect login credentials, please try again")
|
2023-02-19 09:50:30 +08:00
|
|
|
}
|
|
|
|
if user == nil {
|
2023-05-02 08:45:03 +08:00
|
|
|
allowSignUpSetting, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{
|
|
|
|
Name: api.SystemSettingAllowSignUpName,
|
|
|
|
})
|
|
|
|
if err != nil && common.ErrorCode(err) != common.NotFound {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find system setting").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
allowSignUpSettingValue := false
|
|
|
|
if allowSignUpSetting != nil {
|
|
|
|
err = json.Unmarshal([]byte(allowSignUpSetting.Value), &allowSignUpSettingValue)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal system setting allow signup").SetInternal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !allowSignUpSettingValue {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "signup is disabled").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2023-02-19 09:50:30 +08:00
|
|
|
userCreate := &api.UserCreate{
|
|
|
|
Username: userInfo.Identifier,
|
|
|
|
// The new signup user should be normal user by default.
|
|
|
|
Role: api.NormalUser,
|
|
|
|
Nickname: userInfo.DisplayName,
|
|
|
|
Email: userInfo.Email,
|
|
|
|
Password: userInfo.Email,
|
|
|
|
OpenID: common.GenUUID(),
|
|
|
|
}
|
|
|
|
password, err := common.RandomString(20)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate random password").SetInternal(err)
|
|
|
|
}
|
|
|
|
passwordHash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate password hash").SetInternal(err)
|
|
|
|
}
|
|
|
|
userCreate.PasswordHash = string(passwordHash)
|
|
|
|
user, err = s.Store.CreateUser(ctx, userCreate)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create user").SetInternal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if user.RowStatus == api.Archived {
|
|
|
|
return echo.NewHTTPError(http.StatusForbidden, fmt.Sprintf("User has been archived with username %s", userInfo.Identifier))
|
|
|
|
}
|
|
|
|
|
2023-04-07 08:52:13 +08:00
|
|
|
if err := GenerateTokensAndSetCookies(c, user, secret); err != nil {
|
2023-04-02 09:28:02 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate tokens").SetInternal(err)
|
2023-02-19 09:50:30 +08:00
|
|
|
}
|
|
|
|
if err := s.createUserAuthSignInActivity(c, user); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err)
|
|
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, composeResponse(user))
|
|
|
|
})
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
g.POST("/auth/signup", func(c echo.Context) error {
|
2022-08-07 09:23:46 +08:00
|
|
|
ctx := c.Request().Context()
|
2023-01-01 23:26:21 +08:00
|
|
|
signup := &api.SignUp{}
|
2022-11-03 21:47:36 +08:00
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(signup); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted signup request").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2023-02-11 15:15:56 +08:00
|
|
|
userCreate := &api.UserCreate{
|
|
|
|
Username: signup.Username,
|
|
|
|
// The new signup user should be normal user by default.
|
|
|
|
Role: api.NormalUser,
|
|
|
|
Nickname: signup.Username,
|
|
|
|
Password: signup.Password,
|
|
|
|
OpenID: common.GenUUID(),
|
|
|
|
}
|
2022-07-08 22:16:18 +08:00
|
|
|
hostUserType := api.Host
|
2023-02-11 15:15:56 +08:00
|
|
|
existedHostUsers, err := s.Store.FindUserList(ctx, &api.UserFind{
|
2022-07-08 22:16:18 +08:00
|
|
|
Role: &hostUserType,
|
2022-11-03 21:47:36 +08:00
|
|
|
})
|
2023-02-11 15:15:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Failed to find users").SetInternal(err)
|
|
|
|
}
|
|
|
|
if len(existedHostUsers) == 0 {
|
|
|
|
// Change the default role to host if there is no host user.
|
|
|
|
userCreate.Role = api.Host
|
|
|
|
} else {
|
|
|
|
allowSignUpSetting, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{
|
2023-02-15 22:54:46 +08:00
|
|
|
Name: api.SystemSettingAllowSignUpName,
|
2023-02-11 15:15:56 +08:00
|
|
|
})
|
|
|
|
if err != nil && common.ErrorCode(err) != common.NotFound {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find system setting").SetInternal(err)
|
|
|
|
}
|
2022-11-03 21:47:36 +08:00
|
|
|
|
2023-02-11 15:15:56 +08:00
|
|
|
allowSignUpSettingValue := false
|
|
|
|
if allowSignUpSetting != nil {
|
|
|
|
err = json.Unmarshal([]byte(allowSignUpSetting.Value), &allowSignUpSettingValue)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal system setting allow signup").SetInternal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !allowSignUpSettingValue {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "signup is disabled").SetInternal(err)
|
2022-11-03 21:47:36 +08:00
|
|
|
}
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
2022-08-20 21:03:15 +08:00
|
|
|
if err := userCreate.Validate(); err != nil {
|
2022-12-28 20:22:52 +08:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid user create format").SetInternal(err)
|
2022-02-06 16:19:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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-08-20 21:03:15 +08:00
|
|
|
userCreate.PasswordHash = string(passwordHash)
|
2022-08-07 09:23:46 +08:00
|
|
|
user, err := s.Store.CreateUser(ctx, userCreate)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create user").SetInternal(err)
|
|
|
|
}
|
2023-04-07 08:52:13 +08:00
|
|
|
if err := GenerateTokensAndSetCookies(c, user, secret); err != nil {
|
2023-04-02 09:28:02 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate tokens").SetInternal(err)
|
|
|
|
}
|
2023-01-02 23:18:12 +08:00
|
|
|
if err := s.createUserAuthSignUpActivity(c, user); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err)
|
|
|
|
}
|
2022-02-03 15:32:03 +08:00
|
|
|
|
2023-02-17 23:55:56 +08:00
|
|
|
return c.JSON(http.StatusOK, composeResponse(user))
|
2022-02-03 15:32:03 +08:00
|
|
|
})
|
2022-12-28 20:58:59 +08:00
|
|
|
|
2023-01-01 23:26:21 +08:00
|
|
|
g.POST("/auth/signout", func(c echo.Context) error {
|
2023-04-02 09:28:02 +08:00
|
|
|
RemoveTokensAndCookies(c)
|
2022-12-28 20:58:59 +08:00
|
|
|
return c.JSON(http.StatusOK, true)
|
|
|
|
})
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2023-01-01 23:55:02 +08:00
|
|
|
|
|
|
|
func (s *Server) createUserAuthSignInActivity(c echo.Context, user *api.User) error {
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
payload := api.ActivityUserAuthSignInPayload{
|
|
|
|
UserID: user.ID,
|
|
|
|
IP: echo.ExtractIPFromRealIPHeader()(c.Request()),
|
|
|
|
}
|
2023-02-17 23:55:56 +08:00
|
|
|
payloadBytes, err := json.Marshal(payload)
|
2023-01-01 23:55:02 +08:00
|
|
|
if err != nil {
|
2023-01-02 23:18:12 +08:00
|
|
|
return errors.Wrap(err, "failed to marshal activity payload")
|
2023-01-01 23:55:02 +08:00
|
|
|
}
|
2023-01-05 20:56:50 +08:00
|
|
|
activity, err := s.Store.CreateActivity(ctx, &api.ActivityCreate{
|
2023-01-01 23:55:02 +08:00
|
|
|
CreatorID: user.ID,
|
|
|
|
Type: api.ActivityUserAuthSignIn,
|
|
|
|
Level: api.ActivityInfo,
|
2023-02-17 23:55:56 +08:00
|
|
|
Payload: string(payloadBytes),
|
2023-01-01 23:55:02 +08:00
|
|
|
})
|
2023-01-07 11:49:58 +08:00
|
|
|
if err != nil || activity == nil {
|
|
|
|
return errors.Wrap(err, "failed to create activity")
|
|
|
|
}
|
2023-01-01 23:55:02 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-01-02 23:18:12 +08:00
|
|
|
|
|
|
|
func (s *Server) createUserAuthSignUpActivity(c echo.Context, user *api.User) error {
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
payload := api.ActivityUserAuthSignUpPayload{
|
|
|
|
Username: user.Username,
|
|
|
|
IP: echo.ExtractIPFromRealIPHeader()(c.Request()),
|
|
|
|
}
|
2023-02-17 23:55:56 +08:00
|
|
|
payloadBytes, err := json.Marshal(payload)
|
2023-01-02 23:18:12 +08:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to marshal activity payload")
|
|
|
|
}
|
2023-01-05 20:56:50 +08:00
|
|
|
activity, err := s.Store.CreateActivity(ctx, &api.ActivityCreate{
|
2023-01-02 23:18:12 +08:00
|
|
|
CreatorID: user.ID,
|
|
|
|
Type: api.ActivityUserAuthSignUp,
|
|
|
|
Level: api.ActivityInfo,
|
2023-02-17 23:55:56 +08:00
|
|
|
Payload: string(payloadBytes),
|
2023-01-02 23:18:12 +08:00
|
|
|
})
|
2023-01-07 11:49:58 +08:00
|
|
|
if err != nil || activity == nil {
|
|
|
|
return errors.Wrap(err, "failed to create activity")
|
|
|
|
}
|
2023-01-02 23:18:12 +08:00
|
|
|
return err
|
|
|
|
}
|