2023-06-17 16:25:46 +03:00
|
|
|
package v1
|
2022-02-03 10:32:03 +03:00
|
|
|
|
|
|
|
import (
|
2022-02-04 11:51:48 +03:00
|
|
|
"encoding/json"
|
2022-02-03 10:32:03 +03:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-02-19 04:50:30 +03:00
|
|
|
"regexp"
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2023-06-17 16:25:46 +03:00
|
|
|
"github.com/labstack/echo/v4"
|
2023-01-01 18:55:02 +03:00
|
|
|
"github.com/pkg/errors"
|
2022-06-27 17:09:06 +03:00
|
|
|
"github.com/usememos/memos/common"
|
2023-02-19 04:50:30 +03:00
|
|
|
"github.com/usememos/memos/plugin/idp"
|
|
|
|
"github.com/usememos/memos/plugin/idp/oauth2"
|
2023-06-17 16:25:46 +03:00
|
|
|
"github.com/usememos/memos/server/auth"
|
2023-02-19 04:50:30 +03:00
|
|
|
"github.com/usememos/memos/store"
|
2022-02-06 11:19:20 +03:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2022-02-03 10:32:03 +03:00
|
|
|
)
|
|
|
|
|
2023-06-17 16:25:46 +03:00
|
|
|
type SignIn struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type SSOSignIn struct {
|
|
|
|
IdentityProviderID int `json:"identityProviderId"`
|
|
|
|
Code string `json:"code"`
|
|
|
|
RedirectURI string `json:"redirectUri"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type SignUp struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *APIV1Service) registerAuthRoutes(g *echo.Group, secret string) {
|
2022-07-07 18:11:20 +03:00
|
|
|
g.POST("/auth/signin", func(c echo.Context) error {
|
2022-08-07 04:23:46 +03:00
|
|
|
ctx := c.Request().Context()
|
2023-06-17 16:25:46 +03:00
|
|
|
signin := &SignIn{}
|
2022-07-07 18:11:20 +03: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 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
2023-06-26 18:06:53 +03:00
|
|
|
user, err := s.Store.GetUser(ctx, &store.FindUserMessage{
|
2022-11-23 17:27:21 +03:00
|
|
|
Username: &signin.Username,
|
2023-06-26 18:06:53 +03:00
|
|
|
})
|
2022-09-09 15:00:04 +03:00
|
|
|
if err != nil && common.ErrorCode(err) != common.NotFound {
|
2023-03-01 17:33:43 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Incorrect login credentials, please try again")
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
if user == nil {
|
2023-03-01 17:33:43 +03:00
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Incorrect login credentials, please try again")
|
2023-06-26 18:06:53 +03:00
|
|
|
} else if user.RowStatus == store.Archived {
|
2022-11-23 17:27:21 +03:00
|
|
|
return echo.NewHTTPError(http.StatusForbidden, fmt.Sprintf("User has been archived with username %s", signin.Username))
|
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.
|
2022-07-07 18:11:20 +03:00
|
|
|
if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(signin.Password)); err != nil {
|
2022-02-06 11:19:20 +03:00
|
|
|
// If the two passwords don't match, return a 401 status.
|
2023-03-01 17:33:43 +03:00
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Incorrect login credentials, please try again")
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
2023-06-17 16:25:46 +03:00
|
|
|
if err := auth.GenerateTokensAndSetCookies(c, user, secret); err != nil {
|
2023-04-02 04:28:02 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate tokens").SetInternal(err)
|
2022-02-03 19:56:44 +03:00
|
|
|
}
|
2023-06-17 16:25:46 +03:00
|
|
|
if err := s.createAuthSignInActivity(c, user); err != nil {
|
2023-01-01 18:55:02 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err)
|
|
|
|
}
|
2023-06-17 16:25:46 +03:00
|
|
|
return c.JSON(http.StatusOK, user)
|
2022-02-03 10:32:03 +03:00
|
|
|
})
|
2022-02-18 17:21:10 +03:00
|
|
|
|
2023-02-19 04:50:30 +03:00
|
|
|
g.POST("/auth/signin/sso", func(c echo.Context) error {
|
|
|
|
ctx := c.Request().Context()
|
2023-06-17 16:25:46 +03:00
|
|
|
signin := &SSOSignIn{}
|
2023-02-19 04:50:30 +03:00
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(signin); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted signin request").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2023-06-26 18:46:01 +03:00
|
|
|
identityProvider, err := s.Store.GetIdentityProvider(ctx, &store.FindIdentityProvider{
|
2023-02-19 04:50:30 +03:00
|
|
|
ID: &signin.IdentityProviderID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find identity provider").SetInternal(err)
|
|
|
|
}
|
2023-06-26 18:46:01 +03:00
|
|
|
if identityProvider == nil {
|
|
|
|
return echo.NewHTTPError(http.StatusNotFound, "Identity provider not found")
|
|
|
|
}
|
2023-02-19 04:50:30 +03:00
|
|
|
|
|
|
|
var userInfo *idp.IdentityProviderUserInfo
|
2023-06-26 18:46:01 +03:00
|
|
|
if identityProvider.Type == store.IdentityProviderOAuth2 {
|
|
|
|
oauth2IdentityProvider, err := oauth2.NewIdentityProvider(identityProvider.Config.OAuth2Config)
|
2023-02-19 04:50:30 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-26 18:46:01 +03:00
|
|
|
identifierFilter := identityProvider.IdentifierFilter
|
2023-02-19 04:50:30 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-26 18:06:53 +03:00
|
|
|
user, err := s.Store.GetUser(ctx, &store.FindUserMessage{
|
2023-02-19 04:50:30 +03:00
|
|
|
Username: &userInfo.Identifier,
|
|
|
|
})
|
|
|
|
if err != nil && common.ErrorCode(err) != common.NotFound {
|
2023-03-01 17:33:43 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Incorrect login credentials, please try again")
|
2023-02-19 04:50:30 +03:00
|
|
|
}
|
|
|
|
if user == nil {
|
2023-06-26 18:06:53 +03:00
|
|
|
userCreate := &store.UserMessage{
|
2023-02-19 04:50:30 +03:00
|
|
|
Username: userInfo.Identifier,
|
|
|
|
// The new signup user should be normal user by default.
|
2023-06-26 18:06:53 +03:00
|
|
|
Role: store.NormalUser,
|
2023-02-19 04:50:30 +03:00
|
|
|
Nickname: userInfo.DisplayName,
|
|
|
|
Email: 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)
|
2023-06-26 18:06:53 +03:00
|
|
|
user, err = s.Store.CreateUserV1(ctx, userCreate)
|
2023-02-19 04:50:30 +03:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create user").SetInternal(err)
|
|
|
|
}
|
|
|
|
}
|
2023-06-26 18:06:53 +03:00
|
|
|
if user.RowStatus == store.Archived {
|
2023-02-19 04:50:30 +03:00
|
|
|
return echo.NewHTTPError(http.StatusForbidden, fmt.Sprintf("User has been archived with username %s", userInfo.Identifier))
|
|
|
|
}
|
|
|
|
|
2023-06-17 16:25:46 +03:00
|
|
|
if err := auth.GenerateTokensAndSetCookies(c, user, secret); err != nil {
|
2023-04-02 04:28:02 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate tokens").SetInternal(err)
|
2023-02-19 04:50:30 +03:00
|
|
|
}
|
2023-06-17 16:25:46 +03:00
|
|
|
if err := s.createAuthSignInActivity(c, user); err != nil {
|
2023-02-19 04:50:30 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err)
|
|
|
|
}
|
2023-06-17 16:25:46 +03:00
|
|
|
return c.JSON(http.StatusOK, user)
|
2023-02-19 04:50:30 +03:00
|
|
|
})
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
g.POST("/auth/signup", func(c echo.Context) error {
|
2022-08-07 04:23:46 +03:00
|
|
|
ctx := c.Request().Context()
|
2023-06-17 16:25:46 +03:00
|
|
|
signup := &SignUp{}
|
2022-11-03 16:47:36 +03:00
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(signup); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted signup request").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2023-06-26 18:06:53 +03:00
|
|
|
hostUserType := store.Host
|
|
|
|
existedHostUsers, err := s.Store.ListUsers(ctx, &store.FindUserMessage{
|
2022-07-08 17:16:18 +03:00
|
|
|
Role: &hostUserType,
|
2022-11-03 16:47:36 +03:00
|
|
|
})
|
2023-02-11 10:15:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Failed to find users").SetInternal(err)
|
|
|
|
}
|
2023-06-26 18:06:53 +03:00
|
|
|
|
|
|
|
userCreate := &store.UserMessage{
|
|
|
|
Username: signup.Username,
|
|
|
|
// The new signup user should be normal user by default.
|
|
|
|
Role: store.NormalUser,
|
|
|
|
Nickname: signup.Username,
|
|
|
|
OpenID: common.GenUUID(),
|
|
|
|
}
|
2023-02-11 10:15:56 +03:00
|
|
|
if len(existedHostUsers) == 0 {
|
|
|
|
// Change the default role to host if there is no host user.
|
2023-06-26 18:06:53 +03:00
|
|
|
userCreate.Role = store.Host
|
2023-02-11 10:15:56 +03:00
|
|
|
} else {
|
2023-06-26 18:06:53 +03:00
|
|
|
allowSignUpSetting, err := s.Store.GetSystemSetting(ctx, &store.FindSystemSettingMessage{
|
|
|
|
Name: SystemSettingAllowSignUpName.String(),
|
2023-02-11 10:15:56 +03:00
|
|
|
})
|
|
|
|
if err != nil && common.ErrorCode(err) != common.NotFound {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find system setting").SetInternal(err)
|
|
|
|
}
|
2022-11-03 16:47:36 +03:00
|
|
|
|
2023-02-11 10:15:56 +03: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 16:47:36 +03:00
|
|
|
}
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
2022-02-06 11:19:20 +03: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 16:03:15 +03:00
|
|
|
userCreate.PasswordHash = string(passwordHash)
|
2023-06-26 18:06:53 +03:00
|
|
|
user, err := s.Store.CreateUserV1(ctx, userCreate)
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create user").SetInternal(err)
|
|
|
|
}
|
2023-06-17 16:25:46 +03:00
|
|
|
if err := auth.GenerateTokensAndSetCookies(c, user, secret); err != nil {
|
2023-04-02 04:28:02 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate tokens").SetInternal(err)
|
|
|
|
}
|
2023-06-17 16:25:46 +03:00
|
|
|
if err := s.createAuthSignUpActivity(c, user); err != nil {
|
2023-01-02 18:18:12 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err)
|
|
|
|
}
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2023-06-17 16:25:46 +03:00
|
|
|
return c.JSON(http.StatusOK, user)
|
2022-02-03 10:32:03 +03:00
|
|
|
})
|
2022-12-28 15:58:59 +03:00
|
|
|
|
2023-01-01 18:26:21 +03:00
|
|
|
g.POST("/auth/signout", func(c echo.Context) error {
|
2023-06-17 16:25:46 +03:00
|
|
|
auth.RemoveTokensAndCookies(c)
|
2022-12-28 15:58:59 +03:00
|
|
|
return c.JSON(http.StatusOK, true)
|
|
|
|
})
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2023-01-01 18:55:02 +03:00
|
|
|
|
2023-06-26 18:06:53 +03:00
|
|
|
func (s *APIV1Service) createAuthSignInActivity(c echo.Context, user *store.UserMessage) error {
|
2023-01-01 18:55:02 +03:00
|
|
|
ctx := c.Request().Context()
|
2023-06-26 18:06:53 +03:00
|
|
|
payload := ActivityUserAuthSignInPayload{
|
2023-01-01 18:55:02 +03:00
|
|
|
UserID: user.ID,
|
|
|
|
IP: echo.ExtractIPFromRealIPHeader()(c.Request()),
|
|
|
|
}
|
2023-02-17 18:55:56 +03:00
|
|
|
payloadBytes, err := json.Marshal(payload)
|
2023-01-01 18:55:02 +03:00
|
|
|
if err != nil {
|
2023-01-02 18:18:12 +03:00
|
|
|
return errors.Wrap(err, "failed to marshal activity payload")
|
2023-01-01 18:55:02 +03:00
|
|
|
}
|
2023-06-26 18:06:53 +03:00
|
|
|
activity, err := s.Store.CreateActivityV1(ctx, &store.ActivityMessage{
|
2023-01-01 18:55:02 +03:00
|
|
|
CreatorID: user.ID,
|
2023-06-26 18:06:53 +03:00
|
|
|
Type: string(ActivityUserAuthSignIn),
|
|
|
|
Level: string(ActivityInfo),
|
2023-02-17 18:55:56 +03:00
|
|
|
Payload: string(payloadBytes),
|
2023-01-01 18:55:02 +03:00
|
|
|
})
|
2023-01-07 06:49:58 +03:00
|
|
|
if err != nil || activity == nil {
|
|
|
|
return errors.Wrap(err, "failed to create activity")
|
|
|
|
}
|
2023-01-01 18:55:02 +03:00
|
|
|
return err
|
|
|
|
}
|
2023-01-02 18:18:12 +03:00
|
|
|
|
2023-06-26 18:06:53 +03:00
|
|
|
func (s *APIV1Service) createAuthSignUpActivity(c echo.Context, user *store.UserMessage) error {
|
2023-01-02 18:18:12 +03:00
|
|
|
ctx := c.Request().Context()
|
2023-06-26 18:06:53 +03:00
|
|
|
payload := ActivityUserAuthSignUpPayload{
|
2023-01-02 18:18:12 +03:00
|
|
|
Username: user.Username,
|
|
|
|
IP: echo.ExtractIPFromRealIPHeader()(c.Request()),
|
|
|
|
}
|
2023-02-17 18:55:56 +03:00
|
|
|
payloadBytes, err := json.Marshal(payload)
|
2023-01-02 18:18:12 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to marshal activity payload")
|
|
|
|
}
|
2023-06-26 18:06:53 +03:00
|
|
|
activity, err := s.Store.CreateActivityV1(ctx, &store.ActivityMessage{
|
2023-01-02 18:18:12 +03:00
|
|
|
CreatorID: user.ID,
|
2023-06-26 18:06:53 +03:00
|
|
|
Type: string(ActivityUserAuthSignUp),
|
|
|
|
Level: string(ActivityInfo),
|
2023-02-17 18:55:56 +03:00
|
|
|
Payload: string(payloadBytes),
|
2023-01-02 18:18:12 +03:00
|
|
|
})
|
2023-01-07 06:49:58 +03:00
|
|
|
if err != nil || activity == nil {
|
|
|
|
return errors.Wrap(err, "failed to create activity")
|
|
|
|
}
|
2023-01-02 18:18:12 +03:00
|
|
|
return err
|
|
|
|
}
|