From 314cc64e0c45c8fb41ee9c837489f2f6b8828988 Mon Sep 17 00:00:00 2001 From: Stephen Asbury Date: Thu, 25 Jul 2019 14:57:09 -0700 Subject: [PATCH] Moved validation result json -> text message for bad JWT upload --- server/core/handlers_accounts.go | 15 +++++++-------- server/core/handlers_accounts_test.go | 5 +++++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/server/core/handlers_accounts.go b/server/core/handlers_accounts.go index 6866510..45f40f7 100644 --- a/server/core/handlers_accounts.go +++ b/server/core/handlers_accounts.go @@ -17,7 +17,7 @@ package core import ( - "encoding/json" + "fmt" "io/ioutil" "net/http" "strings" @@ -80,15 +80,14 @@ func (server *AccountServer) UpdateAccountJWT(w http.ResponseWriter, r *http.Req claim.Validate(vr) if vr.IsBlocking(true) { - validationResults, err := json.Marshal(vr) - - if err != nil { - server.sendErrorResponse(http.StatusInternalServerError, "unable to marshal JWT validation", shortCode, err, w) - return + var lines []string + lines = append(lines, "The server was unable to update your account JWT. One more more validation issues occurred.") + for _, vi := range vr.Issues { + lines = append(lines, fmt.Sprintf("\t - %s\n", vi.Description)) } - + msg := strings.Join(lines, "\n") server.logger.Errorf("attempt to update JWT %s with blocking validation errors", shortCode) - http.Error(w, string(validationResults), http.StatusBadRequest) + http.Error(w, msg, http.StatusBadRequest) return } diff --git a/server/core/handlers_accounts_test.go b/server/core/handlers_accounts_test.go index 0b62316..7fd93d6 100644 --- a/server/core/handlers_accounts_test.go +++ b/server/core/handlers_accounts_test.go @@ -244,6 +244,11 @@ func TestExpiredJWT(t *testing.T) { resp, err = testEnv.HTTP.Post(url, "application/json", bytes.NewBuffer([]byte(acctJWT))) require.NoError(t, err) require.True(t, resp.StatusCode == http.StatusBadRequest) // Already expired + body, err := ioutil.ReadAll(resp.Body) + require.NoError(t, err) + message := string(body) + + require.True(t, strings.Contains(message, "expired")) account = jwt.NewAccountClaims(pubKey) account.Expires = time.Now().Unix() + 2