fix(httpd): fail bearerauth if shared secret blank

pull/13088/head
David Norton 2019-04-02 11:26:57 -04:00
parent 932521b6ff
commit 3ec2ac43a4
2 changed files with 23 additions and 0 deletions

View File

@ -1595,6 +1595,11 @@ func authenticate(inner func(http.ResponseWriter, *http.Request, meta.User), h *
return
}
case BearerAuthentication:
if h.Config.SharedSecret == "" {
atomic.AddInt64(&h.stats.AuthenticationFailures, 1)
h.httpError(w, "bearer auth disabled", http.StatusUnauthorized)
return
}
keyLookupFn := func(token *jwt.Token) (interface{}, error) {
// Check for expected signing method.
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {

View File

@ -234,6 +234,24 @@ func TestHandler_Query_Auth(t *testing.T) {
t.Fatalf("unexpected body: %s", body)
}
// Test that auth fails if shared secret is blank.
origSecret := h.Config.SharedSecret
h.Config.SharedSecret = ""
token, _ = MustJWTToken("user1", h.Config.SharedSecret, false)
signedToken, err = token.SignedString([]byte(h.Config.SharedSecret))
if err != nil {
t.Fatal(err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", signedToken))
w = httptest.NewRecorder()
h.ServeHTTP(w, req)
if w.Code != http.StatusUnauthorized {
t.Fatalf("unexpected status: %d: %s", w.Code, w.Body.String())
} else if body := strings.TrimSpace(w.Body.String()); body != `{"error":"bearer auth disabled"}` {
t.Fatalf("unexpected body: %s", body)
}
h.Config.SharedSecret = origSecret
// Test the handler with valid user and password in the url and invalid in
// basic auth (prioritize url).
w = httptest.NewRecorder()