rename auth middleware

This commit is contained in:
Hector 2023-06-21 11:39:49 +01:00
parent 29d46be1d3
commit b9a4caade0
3 changed files with 7 additions and 7 deletions

View File

@ -6,7 +6,7 @@ import (
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/auth" "gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/auth"
) )
func BasicAuthMiddleware(handlerFunc http.HandlerFunc, authProvider auth.AuthProvider) http.HandlerFunc { func AuthMiddleware(handlerFunc http.HandlerFunc, authProvider auth.AuthProvider) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if authProvider.IsAllowed(r) { if authProvider.IsAllowed(r) {
handlerFunc.ServeHTTP(w, r) handlerFunc.ServeHTTP(w, r)

View File

@ -18,13 +18,13 @@ func newTestRequest() *http.Request {
return httptest.NewRequest(http.MethodGet, "http://example.com", nil) return httptest.NewRequest(http.MethodGet, "http://example.com", nil)
} }
func executeBasicAuthMiddlewareTest(t *testing.T, authMatches bool, expectedCode int, expectedCallCount int) { func executeAuthMiddlewareTest(t *testing.T, authMatches bool, expectedCode int, expectedCallCount int) {
callCount := 0 callCount := 0
testHandler := func(w http.ResponseWriter, r *http.Request) { testHandler := func(w http.ResponseWriter, r *http.Request) {
callCount++ callCount++
} }
handler := BasicAuthMiddleware(testHandler, testAuthProvider{match: authMatches}) handler := AuthMiddleware(testHandler, testAuthProvider{match: authMatches})
recorder := httptest.NewRecorder() recorder := httptest.NewRecorder()
request := newTestRequest() request := newTestRequest()
handler.ServeHTTP(recorder, request) handler.ServeHTTP(recorder, request)
@ -38,9 +38,9 @@ func executeBasicAuthMiddlewareTest(t *testing.T, authMatches bool, expectedCode
} }
func Test_GIVEN_MatchingBasicAuth_WHEN_MethodCalled_THEN_RequestProcessed(t *testing.T) { func Test_GIVEN_MatchingBasicAuth_WHEN_MethodCalled_THEN_RequestProcessed(t *testing.T) {
executeBasicAuthMiddlewareTest(t, true, http.StatusOK, 1) executeAuthMiddlewareTest(t, true, http.StatusOK, 1)
} }
func Test_GIVEN_NonMatchingBasicAuth_WHEN_MethodCalled_THEN_RequestRejected(t *testing.T) { func Test_GIVEN_NonMatchingBasicAuth_WHEN_MethodCalled_THEN_RequestRejected(t *testing.T) {
executeBasicAuthMiddlewareTest(t, false, http.StatusUnauthorized, 0) executeAuthMiddlewareTest(t, false, http.StatusUnauthorized, 0)
} }

View File

@ -12,11 +12,11 @@ func StartServer(
appSettings *cfg.AppSettings, appSettings *cfg.AppSettings,
textFileCollector *textfile.Collector, textFileCollector *textfile.Collector,
) chan error { ) chan error {
http.HandleFunc("/", BasicAuthMiddleware( http.HandleFunc("/", AuthMiddleware(
rootHtmlHandler, rootHtmlHandler,
appSettings.AuthProvider, appSettings.AuthProvider,
)) ))
http.HandleFunc(metricsPath, BasicAuthMiddleware( http.HandleFunc(metricsPath, AuthMiddleware(
func(w http.ResponseWriter, r *http.Request) { func(w http.ResponseWriter, r *http.Request) {
metricHandler(w, r, textFileCollector) metricHandler(w, r, textFileCollector)
}, },