Return 400 instead of panicking on malformed JSON request bodies
This commit is contained in:
parent
1db5983ed5
commit
d9fdd3a017
|
|
@ -76,9 +76,18 @@ func Login(db store.IStore) echo.HandlerFunc {
|
||||||
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
||||||
}
|
}
|
||||||
|
|
||||||
username := data["username"].(string)
|
username, ok := data["username"].(string)
|
||||||
password := data["password"].(string)
|
if !ok {
|
||||||
rememberMe := data["rememberMe"].(bool)
|
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
||||||
|
}
|
||||||
|
password, ok := data["password"].(string)
|
||||||
|
if !ok {
|
||||||
|
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
||||||
|
}
|
||||||
|
rememberMe, ok := data["rememberMe"].(bool)
|
||||||
|
if !ok {
|
||||||
|
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
||||||
|
}
|
||||||
|
|
||||||
if !usernameRegexp.MatchString(username) {
|
if !usernameRegexp.MatchString(username) {
|
||||||
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Please provide a valid username"})
|
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Please provide a valid username"})
|
||||||
|
|
@ -220,10 +229,22 @@ func UpdateUser(db store.IStore) echo.HandlerFunc {
|
||||||
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
||||||
}
|
}
|
||||||
|
|
||||||
username := data["username"].(string)
|
username, ok := data["username"].(string)
|
||||||
password := data["password"].(string)
|
if !ok {
|
||||||
previousUsername := data["previous_username"].(string)
|
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
||||||
admin := data["admin"].(bool)
|
}
|
||||||
|
password, ok := data["password"].(string)
|
||||||
|
if !ok {
|
||||||
|
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
||||||
|
}
|
||||||
|
previousUsername, ok := data["previous_username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
||||||
|
}
|
||||||
|
admin, ok := data["admin"].(bool)
|
||||||
|
if !ok {
|
||||||
|
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
||||||
|
}
|
||||||
|
|
||||||
if !isAdmin(c) && (previousUsername != currentUser(c)) {
|
if !isAdmin(c) && (previousUsername != currentUser(c)) {
|
||||||
return c.JSON(http.StatusForbidden, jsonHTTPResponse{false, "Manager cannot access other user data"})
|
return c.JSON(http.StatusForbidden, jsonHTTPResponse{false, "Manager cannot access other user data"})
|
||||||
|
|
@ -294,9 +315,18 @@ func CreateUser(db store.IStore) echo.HandlerFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
var user model.User
|
var user model.User
|
||||||
username := data["username"].(string)
|
username, ok := data["username"].(string)
|
||||||
password := data["password"].(string)
|
if !ok {
|
||||||
admin := data["admin"].(bool)
|
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
||||||
|
}
|
||||||
|
password, ok := data["password"].(string)
|
||||||
|
if !ok {
|
||||||
|
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
||||||
|
}
|
||||||
|
admin, ok := data["admin"].(bool)
|
||||||
|
if !ok {
|
||||||
|
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
||||||
|
}
|
||||||
|
|
||||||
if username == "" || !usernameRegexp.MatchString(username) {
|
if username == "" || !usernameRegexp.MatchString(username) {
|
||||||
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Please provide a valid username"})
|
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Please provide a valid username"})
|
||||||
|
|
@ -338,7 +368,10 @@ func RemoveUser(db store.IStore) echo.HandlerFunc {
|
||||||
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
||||||
}
|
}
|
||||||
|
|
||||||
username := data["username"].(string)
|
username, ok := data["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
||||||
|
}
|
||||||
|
|
||||||
if !usernameRegexp.MatchString(username) {
|
if !usernameRegexp.MatchString(username) {
|
||||||
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Please provide a valid username"})
|
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Please provide a valid username"})
|
||||||
|
|
@ -753,8 +786,14 @@ func SetClientStatus(db store.IStore) echo.HandlerFunc {
|
||||||
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
||||||
}
|
}
|
||||||
|
|
||||||
clientID := data["id"].(string)
|
clientID, ok := data["id"].(string)
|
||||||
status := data["status"].(bool)
|
if !ok {
|
||||||
|
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
||||||
|
}
|
||||||
|
status, ok := data["status"].(bool)
|
||||||
|
if !ok {
|
||||||
|
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := xid.FromString(clientID); err != nil {
|
if _, err := xid.FromString(clientID); err != nil {
|
||||||
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Please provide a valid client ID"})
|
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Please provide a valid client ID"})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue