From 78761c5abf54f4c1942c9f106087ea9410b9bfd7 Mon Sep 17 00:00:00 2001 From: dreth Date: Fri, 16 Aug 2024 01:12:49 +0200 Subject: [PATCH] fix length validation bug and refactor a little --- backend/auth/auth.go | 36 ++++++++++++++++++++++-------------- backend/helper/validation.go | 19 +++++++------------ 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/backend/auth/auth.go b/backend/auth/auth.go index 9a3c5d3..752ace4 100644 --- a/backend/auth/auth.go +++ b/backend/auth/auth.go @@ -68,6 +68,7 @@ func Register(c *gin.Context) { []int{150, 64, 50, 50, 60, 20}, []int{5, 1, 1, 1, 1, 1}, []int{0, 0, 0, 0, 0, 0}, + []bool{false, false, false, false, false, false}, ) // Loop over errors and concatenate the strings to return it all at once // first check if all errors are nil, if so, nothing to do, otherwise, loop over the errors and concatenate them @@ -200,6 +201,7 @@ func Login(c *gin.Context) { []int{150, 64}, []int{5, 1}, []int{0, 0}, + []bool{false, false}, ) if helper.CheckErrors(lengthErrors) != nil { errorStr := helper.ConcatenateErrors(lengthErrors) @@ -302,8 +304,9 @@ func ModifyUser(c *gin.Context) { []string{"NewEmail", "NewPassword", "NewReminderTime", "NewTimezone", "NewTelegramBotAPIKey", "NewTelegramUserID"}, []string{req.NewEmail, req.NewPassword, req.NewReminderTime, req.NewTimezone, req.NewTelegramBotAPIKey, req.NewTelegramUserID}, []int{150, 64, 50, 50, 60, 20}, - []int{5, 5, 1, 1, 1, 1}, + []int{0, 0, 1, 1, 1, 1}, []int{0, 0, 0, 0, 0, 0}, + []bool{true, true, false, false, false, false}, ) if helper.CheckErrors(lengthErrors) != nil { errorStr := helper.ConcatenateErrors(lengthErrors) @@ -391,24 +394,29 @@ func ModifyUser(c *gin.Context) { return } - // Update the email in the context - c.Set("Email", req.NewEmail) - - // Get the user data post-changes + // Get user data post-changes userData, err := GetUserData(c) if helper.HE(c, err, http.StatusInternalServerError, "invalid email or password", true) { return } - // Return the new token with the new user data - c.JSON(http.StatusOK, structs.LoginSuccess{ - Token: token, - TelegramBotAPIKey: userData.TelegramBotAPIKey, - TelegramUserID: userData.TelegramUserID, - ReminderTime: userData.ReminderTime, - Timezone: userData.Timezone, - Birthdays: userData.Birthdays, - }) + // Update the email in the context + if req.NewEmail != "" { + c.Set("Email", req.NewEmail) + + // Return the new token with the new user data + c.JSON(http.StatusOK, structs.LoginSuccess{ + Token: token, + TelegramBotAPIKey: userData.TelegramBotAPIKey, + TelegramUserID: userData.TelegramUserID, + ReminderTime: userData.ReminderTime, + Timezone: userData.Timezone, + Birthdays: userData.Birthdays, + }) + } + + // Return the data that calling /me would return + c.JSON(http.StatusOK, userData) } // @Summary Delete a user diff --git a/backend/helper/validation.go b/backend/helper/validation.go index 9170263..31bc2b6 100644 --- a/backend/helper/validation.go +++ b/backend/helper/validation.go @@ -14,9 +14,11 @@ func IsValidEmail(email string) bool { } // Check the length of a particular string and return an error if it is too long -func CheckStringLength(field string, str string, maxLength int, minLength int, strictLength int) error { +func CheckStringLength(field string, str string, maxLength int, minLength int, strictLength int, allowEmpty bool) error { if len(str) == 0 { - return StringInappropriateLengthError(field, "empty") + if !allowEmpty { + return StringInappropriateLengthError(field, "empty") + } } else if len(str) < minLength { return StringInappropriateLengthError(field, "too short") } else if len(str) > maxLength { @@ -28,7 +30,7 @@ func CheckStringLength(field string, str string, maxLength int, minLength int, s } // Check the length of a particular array of string and return an error if it is too long -func CheckArrayStringLength(field []string, str []string, maxLength []int, minLength []int, strictLength []int) []error { +func CheckArrayStringLength(field []string, str []string, maxLength []int, minLength []int, strictLength []int, allowEmpty []bool) []error { errorsList := []error{} // Boundary check for array lengths @@ -37,15 +39,8 @@ func CheckArrayStringLength(field []string, str []string, maxLength []int, minLe } for i := 0; i < len(str); i++ { - if len(str[i]) == 0 { - errorsList = append(errorsList, StringInappropriateLengthError(field[i], "empty")) - } else if len(str[i]) < minLength[i] { - errorsList = append(errorsList, StringInappropriateLengthError(field[i], "too short")) - } else if len(str[i]) > maxLength[i] { - errorsList = append(errorsList, StringInappropriateLengthError(field[i], "too long")) - } else if (strictLength[i] != 0) && (len(str[i]) != strictLength[i]) { - errorsList = append(errorsList, StringInappropriateLengthError(field[i], "incorrect length, should be "+strconv.Itoa(strictLength[i]))) - } + err := CheckStringLength(field[i], str[i], maxLength[i], minLength[i], strictLength[i], allowEmpty[i]) + errorsList = append(errorsList, err) } return errorsList }