fix(httpd): fail bearerauth if shared secret blank
parent
932521b6ff
commit
3ec2ac43a4
|
@ -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 {
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue