From 3c6ab9bd770d303ea2166cc2b905dd36622f2088 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 25 Aug 2021 13:56:30 +0200 Subject: [PATCH] auth: improve error response format This maps more closely onto what normal GQL errors look like. --- auth/middleware.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/auth/middleware.go b/auth/middleware.go index 63a477ef6846331ce169dfa32bd9c1129c34e270..a82888a67ea48279a40fe8b37e777197180ebba5 100644 --- a/auth/middleware.go +++ b/auth/middleware.go @@ -83,7 +83,11 @@ type AuthContext struct { func authError(w http.ResponseWriter, reason string, code int) { gqlerr := gqlerror.Errorf("Authentication error: %s", reason) - b, err := json.Marshal(gqlerr) + b, err := json.Marshal(struct { + Errors []*gqlerror.Error `json:"errors"` + } { + Errors: []*gqlerror.Error{gqlerr}, + }) if err != nil { panic(err) } @@ -708,7 +712,7 @@ func Middleware(conf ini.File, apiconf string) func(http.Handler) http.Handler { auth := r.Header.Get("Authorization") if auth == "" { - authError(w, `Authorization header is required. Expected 'Authorization: Bearer '`, http.StatusUnauthorized) + authError(w, `Authorization header is required. Expected 'Authorization: Bearer [token]'`, http.StatusUnauthorized) return }