Skip to content

Commit

Permalink
chore: update user store names (usememos#1877)
Browse files Browse the repository at this point in the history
* chore: update user store names

* chore: update
  • Loading branch information
boojack authored and lincolnthalles committed Oct 21, 2023
1 parent 6848742 commit 9fe3ba7
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 275 deletions.
14 changes: 7 additions & 7 deletions api/v1/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *APIV1Service) registerAuthRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted signin request").SetInternal(err)
}

user, err := s.Store.GetUser(ctx, &store.FindUserMessage{
user, err := s.Store.GetUser(ctx, &store.FindUser{
Username: &signin.Username,
})
if err != nil && common.ErrorCode(err) != common.NotFound {
Expand Down Expand Up @@ -111,14 +111,14 @@ func (s *APIV1Service) registerAuthRoutes(g *echo.Group) {
}
}

user, err := s.Store.GetUser(ctx, &store.FindUserMessage{
user, err := s.Store.GetUser(ctx, &store.FindUser{
Username: &userInfo.Identifier,
})
if err != nil && common.ErrorCode(err) != common.NotFound {
return echo.NewHTTPError(http.StatusInternalServerError, "Incorrect login credentials, please try again")
}
if user == nil {
userCreate := &store.UserMessage{
userCreate := &store.User{
Username: userInfo.Identifier,
// The new signup user should be normal user by default.
Role: store.NormalUser,
Expand Down Expand Up @@ -161,14 +161,14 @@ func (s *APIV1Service) registerAuthRoutes(g *echo.Group) {
}

hostUserType := store.Host
existedHostUsers, err := s.Store.ListUsers(ctx, &store.FindUserMessage{
existedHostUsers, err := s.Store.ListUsers(ctx, &store.FindUser{
Role: &hostUserType,
})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Failed to find users").SetInternal(err)
}

userCreate := &store.UserMessage{
userCreate := &store.User{
Username: signup.Username,
// The new signup user should be normal user by default.
Role: store.NormalUser,
Expand Down Expand Up @@ -224,7 +224,7 @@ func (s *APIV1Service) registerAuthRoutes(g *echo.Group) {
})
}

func (s *APIV1Service) createAuthSignInActivity(c echo.Context, user *store.UserMessage) error {
func (s *APIV1Service) createAuthSignInActivity(c echo.Context, user *store.User) error {
ctx := c.Request().Context()
payload := ActivityUserAuthSignInPayload{
UserID: user.ID,
Expand All @@ -246,7 +246,7 @@ func (s *APIV1Service) createAuthSignInActivity(c echo.Context, user *store.User
return err
}

func (s *APIV1Service) createAuthSignUpActivity(c echo.Context, user *store.UserMessage) error {
func (s *APIV1Service) createAuthSignUpActivity(c echo.Context, user *store.User) error {
ctx := c.Request().Context()
payload := ActivityUserAuthSignUpPayload{
Username: user.Username,
Expand Down
10 changes: 5 additions & 5 deletions api/v1/idp.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (s *APIV1Service) registerIdentityProviderRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
}

user, err := s.Store.GetUser(ctx, &store.FindUserMessage{
user, err := s.Store.GetUser(ctx, &store.FindUser{
ID: &userID,
})
if err != nil {
Expand Down Expand Up @@ -102,7 +102,7 @@ func (s *APIV1Service) registerIdentityProviderRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
}

user, err := s.Store.GetUser(ctx, &store.FindUserMessage{
user, err := s.Store.GetUser(ctx, &store.FindUser{
ID: &userID,
})
if err != nil {
Expand Down Expand Up @@ -147,7 +147,7 @@ func (s *APIV1Service) registerIdentityProviderRoutes(g *echo.Group) {
userID, ok := c.Get(getUserIDContextKey()).(int)
isHostUser := false
if ok {
user, err := s.Store.GetUser(ctx, &store.FindUserMessage{
user, err := s.Store.GetUser(ctx, &store.FindUser{
ID: &userID,
})
if err != nil {
Expand Down Expand Up @@ -177,7 +177,7 @@ func (s *APIV1Service) registerIdentityProviderRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
}

user, err := s.Store.GetUser(ctx, &store.FindUserMessage{
user, err := s.Store.GetUser(ctx, &store.FindUser{
ID: &userID,
})
if err != nil {
Expand Down Expand Up @@ -211,7 +211,7 @@ func (s *APIV1Service) registerIdentityProviderRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
}

user, err := s.Store.GetUser(ctx, &store.FindUserMessage{
user, err := s.Store.GetUser(ctx, &store.FindUser{
ID: &userID,
})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions api/v1/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func JWTMiddleware(server *APIV1Service, next echo.HandlerFunc, secret string) e
}

// Even if there is no error, we still need to make sure the user still exists.
user, err := server.Store.GetUser(ctx, &store.FindUserMessage{
user, err := server.Store.GetUser(ctx, &store.FindUser{
ID: &userID,
})
if err != nil {
Expand Down Expand Up @@ -222,7 +222,7 @@ func (s *APIV1Service) defaultAuthSkipper(c echo.Context) bool {
// If there is openId in query string and related user is found, then skip auth.
openID := c.QueryParam("openId")
if openID != "" {
user, err := s.Store.GetUser(ctx, &store.FindUserMessage{
user, err := s.Store.GetUser(ctx, &store.FindUser{
OpenID: &openID,
})
if err != nil && common.ErrorCode(err) != common.NotFound {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ require (
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ github.com/spf13/viper v1.15.0 h1:js3yy885G8xwJa6iOISGFwd+qlUo5AvyXb7CiihdtiU=
github.com/spf13/viper v1.15.0/go.mod h1:fFcTBJxvhhzSJiZy8n+PeW6t8l+KeT/uTARa0jHOQLA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
2 changes: 1 addition & 1 deletion server/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func GenerateRefreshToken(userName string, userID int, secret string) (string, e
}

// GenerateTokensAndSetCookies generates jwt token and saves it to the http-only cookie.
func GenerateTokensAndSetCookies(c echo.Context, user *store.UserMessage, secret string) error {
func GenerateTokensAndSetCookies(c echo.Context, user *store.User, secret string) error {
accessToken, err := GenerateAccessToken(user.Username, user.ID, secret)
if err != nil {
return errors.Wrap(err, "failed to generate access token")
Expand Down
2 changes: 1 addition & 1 deletion server/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func JWTMiddleware(server *Server, next echo.HandlerFunc, secret string) echo.Ha
}

// Even if there is no error, we still need to make sure the user still exists.
user, err := server.Store.GetUser(ctx, &store.FindUserMessage{
user, err := server.Store.GetUser(ctx, &store.FindUser{
ID: &userID,
})
if err != nil {
Expand Down
59 changes: 32 additions & 27 deletions setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,19 @@ import (

"github.com/usememos/memos/api"
"github.com/usememos/memos/common"
"github.com/usememos/memos/store"
)

func Execute(
ctx context.Context,
store store,
hostUsername, hostPassword string,
) error {
func Execute(ctx context.Context, store *store.Store, hostUsername, hostPassword string) error {
s := setupService{store: store}
return s.Setup(ctx, hostUsername, hostPassword)
}

type store interface {
FindUserList(ctx context.Context, find *api.UserFind) ([]*api.User, error)
CreateUser(ctx context.Context, create *api.UserCreate) (*api.User, error)
}

type setupService struct {
store store
store *store.Store
}

func (s setupService) Setup(
ctx context.Context,
hostUsername, hostPassword string,
) error {
func (s setupService) Setup(ctx context.Context, hostUsername, hostPassword string) error {
if err := s.makeSureHostUserNotExists(ctx); err != nil {
return err
}
Expand All @@ -59,31 +48,47 @@ func (s setupService) makeSureHostUserNotExists(ctx context.Context) error {
return nil
}

func (s setupService) createUser(
ctx context.Context,
hostUsername, hostPassword string,
) error {
userCreate := &api.UserCreate{
func (s setupService) createUser(ctx context.Context, hostUsername, hostPassword string) error {
userCreate := &store.User{
Username: hostUsername,
// The new signup user should be normal user by default.
Role: api.Host,
Role: store.Host,
Nickname: hostUsername,
Password: hostPassword,
OpenID: common.GenUUID(),
}

if err := userCreate.Validate(); err != nil {
return fmt.Errorf("validate: %w", err)
if len(userCreate.Username) < 3 {
return fmt.Errorf("username is too short, minimum length is 3")
}
if len(userCreate.Username) > 32 {
return fmt.Errorf("username is too long, maximum length is 32")
}
if len(hostPassword) < 3 {
return fmt.Errorf("password is too short, minimum length is 3")
}
if len(hostPassword) > 512 {
return fmt.Errorf("password is too long, maximum length is 512")
}
if len(userCreate.Nickname) > 64 {
return fmt.Errorf("nickname is too long, maximum length is 64")
}
if userCreate.Email != "" {
if len(userCreate.Email) > 256 {
return fmt.Errorf("email is too long, maximum length is 256")
}
if !common.ValidateEmail(userCreate.Email) {
return fmt.Errorf("invalid email format")
}
}

passwordHash, err := bcrypt.GenerateFromPassword([]byte(hostPassword), bcrypt.DefaultCost)
if err != nil {
return fmt.Errorf("hash password: %w", err)
return fmt.Errorf("failed to hash password: %w", err)
}

userCreate.PasswordHash = string(passwordHash)
if _, err := s.store.CreateUser(ctx, userCreate); err != nil {
return fmt.Errorf("create user: %w", err)
if _, err := s.store.CreateUserV1(ctx, userCreate); err != nil {
return fmt.Errorf("failed to create user: %w", err)
}

return nil
Expand Down
Loading

0 comments on commit 9fe3ba7

Please sign in to comment.