Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: update email format with lower case (#4814)

boojack67160be1e936
diff --git a/backend/api/v1/auth_service.go b/backend/api/v1/auth_service.go
index 8cc51edd3a..23b5d13734 100644
--- a/backend/api/v1/auth_service.go
+++ b/backend/api/v1/auth_service.go
@@ -3,8 +3,10 @@ package v1
import (
"context"
"encoding/json"
+ "errors"
"fmt"
"net/mail"
+ "strings"
"golang.org/x/crypto/bcrypt"
"google.golang.org/grpc"
@@ -92,9 +94,6 @@ func (s *AuthService) CreateUser(ctx context.Context, request *v1pb.CreateUserRe
if request.User.Email == "" {
return nil, status.Errorf(codes.InvalidArgument, "email must be set")
}
- if _, err := mail.ParseAddress(request.User.Email); err != nil {
- return nil, status.Errorf(codes.InvalidArgument, "invalid email %q address", request.User.Email)
- }
if request.User.Title == "" {
return nil, status.Errorf(codes.InvalidArgument, "user title must be set")
}
@@ -121,6 +120,9 @@ func (s *AuthService) CreateUser(ctx context.Context, request *v1pb.CreateUserRe
}
}
+ if err := validateEmail(request.User.Email); err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, "invalid email %q format: %v", request.User.Email, err)
+ }
existingUser, err := s.store.GetUser(ctx, &store.FindUserMessage{
Email: &request.User.Email,
ShowDeleted: true,
@@ -217,8 +219,8 @@ func (s *AuthService) UpdateUser(ctx context.Context, request *v1pb.UpdateUserRe
for _, path := range request.UpdateMask.Paths {
switch path {
case "user.email":
- if _, err := mail.ParseAddress(request.User.Email); err != nil {
- return nil, status.Errorf(codes.InvalidArgument, "invalid email address %q", request.User.Email)
+ if err := validateEmail(request.User.Email); err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, "invalid email %q format: %v", request.User.Email, err)
}
users, err := s.store.ListUsers(ctx, &store.FindUserMessage{
Email: &request.User.Email,
@@ -520,18 +522,19 @@ func (s *AuthService) getUserWithLoginRequestOfIdentityProvider(ctx context.Cont
return nil, status.Errorf(codes.NotFound, "identity provider user info not found")
}
- email := userInfo.Email
- if email == "" {
+ // The userinfo's email comes from identity provider, we cannot ensure it's lowercase.
+ formatedEmail := strings.ToLower(userInfo.Email)
+ if formatedEmail == "" {
// If the email is empty, we should concatenate the identifier and
// the IdP's domain as the user's email.
- email = fmt.Sprintf("%s@%s", userInfo.Identifier, identityProvider.Domain)
+ formatedEmail = strings.ToLower(fmt.Sprintf("%s@%s", userInfo.Identifier, identityProvider.Domain))
}
users, err := s.store.ListUsers(ctx, &store.FindUserMessage{
- Email: &email,
+ Email: &formatedEmail,
ShowDeleted: true,
})
if err != nil {
- return nil, status.Errorf(codes.Internal, "failed to list users by email %s: %v", email, err)
+ return nil, status.Errorf(codes.Internal, "failed to list users by email %s: %v", formatedEmail, err)
}
var user *store.UserMessage
@@ -547,7 +550,7 @@ func (s *AuthService) getUserWithLoginRequestOfIdentityProvider(ctx context.Cont
}
newUser, err := s.store.CreateUser(ctx, &store.UserMessage{
Name: userInfo.DisplayName,
- Email: email,
+ Email: formatedEmail,
Type: api.EndUser,
PasswordHash: string(passwordHash),
}, api.SystemBotID)
@@ -564,3 +567,14 @@ func (s *AuthService) getUserWithLoginRequestOfIdentityProvider(ctx context.Cont
return user, nil
}
+
+func validateEmail(email string) error {
+ formatedEmail := strings.ToLower(email)
+ if email != formatedEmail {
+ return errors.New("email should be lowercase")
+ }
+ if _, err := mail.ParseAddress(email); err != nil {
+ return err
+ }
+ return nil
+}
diff --git a/backend/server/auth.go b/backend/server/auth.go
index ad90378b93..452287203b 100644
--- a/backend/server/auth.go
+++ b/backend/server/auth.go
@@ -5,9 +5,12 @@ import (
"encoding/json"
"fmt"
"net/http"
+ "net/mail"
+ "strings"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4"
+ "github.com/pkg/errors"
"golang.org/x/crypto/bcrypt"
"github.com/bytebase/bytebase/backend/common"
@@ -201,6 +204,9 @@ func trySignUp(ctx context.Context, s *Server, signUp *api.SignUp, creatorID int
return nil, echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate password hash").SetInternal(err)
}
+ if err := validateEmail(signUp.Email); err != nil {
+ return nil, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("invalid email %q format: %v", signUp.Email, err))
+ }
users, err := s.store.ListUsers(ctx, &store.FindUserMessage{
Email: &signUp.Email,
ShowDeleted: true,
@@ -246,3 +252,14 @@ func trySignUp(ctx context.Context, s *Server, signUp *api.SignUp, creatorID int
return user, nil
}
+
+func validateEmail(email string) error {
+ formatedEmail := strings.ToLower(email)
+ if email != formatedEmail {
+ return errors.New("email should be lowercase")
+ }
+ if _, err := mail.ParseAddress(email); err != nil {
+ return err
+ }
+ return nil
+}
diff --git a/backend/server/principal.go b/backend/server/principal.go
index 8c630047a8..afa0072f9f 100644
--- a/backend/server/principal.go
+++ b/backend/server/principal.go
@@ -33,8 +33,11 @@ func (s *Server) registerPrincipalRoutes(g *echo.Group) {
if err := jsonapi.UnmarshalPayload(c.Request().Body, principalCreate); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformed create principal request").SetInternal(err)
}
- creatorID := c.Get(getPrincipalIDContextKey()).(int)
+ if err := validateEmail(principalCreate.Email); err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("invalid email %q format: %v", principalCreate.Email, err))
+ }
+ creatorID := c.Get(getPrincipalIDContextKey()).(int)
password := principalCreate.Password
if principalCreate.Type == api.ServiceAccount {
pwd, err := common.RandomString(20)
@@ -153,6 +156,9 @@ func (s *Server) registerPrincipalRoutes(g *echo.Group) {
}
if principalPatch.Email != nil {
+ if err := validateEmail(*principalPatch.Email); err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("invalid email %q format: %v", *principalPatch.Email, err))
+ }
currentUser, err := s.store.GetUserByID(ctx, id)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to get user by id %d", id)).SetInternal(err)
@@ -168,6 +174,8 @@ func (s *Server) registerPrincipalRoutes(g *echo.Group) {
if len(users) != 0 {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Email %s is already existed", *principalPatch.Email))
}
+ } else {
+ principalPatch.Email = nil
}
}
diff --git a/frontend/src/store/modules/principal.ts b/frontend/src/store/modules/principal.ts
index cc0f6186da..347c60a07a 100644
--- a/frontend/src/store/modules/principal.ts
+++ b/frontend/src/store/modules/principal.ts
@@ -113,7 +113,7 @@ export const usePrincipalStore = defineStore("principal", {
type: "PrincipalCreate",
attributes: {
name: newPrincipal.name,
- email: newPrincipal.email,
+ email: newPrincipal.email.toLowerCase(),
password: randomString(),
type: newPrincipal.type,
},