Skip to content

Commit

Permalink
Merge pull request #30 from nats-io/vr_as_text
Browse files Browse the repository at this point in the history
Moved validation result json -> text message for bad JWT upload
  • Loading branch information
sasbury authored Jul 26, 2019
2 parents 8ef6ff6 + 314cc64 commit 0b6f2a3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
15 changes: 7 additions & 8 deletions server/core/handlers_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package core

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
Expand Down Expand Up @@ -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
}

Expand Down
5 changes: 5 additions & 0 deletions server/core/handlers_accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0b6f2a3

Please sign in to comment.