~bigbes/core-go

3c6ab9bd770d303ea2166cc2b905dd36622f2088 — Drew DeVault 4 years ago ae61e24
auth: improve error response format

This maps more closely onto what normal GQL errors look like.
1 files changed, 6 insertions(+), 2 deletions(-)

M auth/middleware.go
M auth/middleware.go => auth/middleware.go +6 -2
@@ 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 <token>'`, http.StatusUnauthorized)
				authError(w, `Authorization header is required. Expected 'Authorization: Bearer [token]'`, http.StatusUnauthorized)
				return
			}