3cff8ccd64
* Rewrite the code handling basic auth to make it easier to extend for other types of auth. * The behaviour of the existing code is maintained. * No changes to how basic auth is configured from a user's perspective. https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/merge_requests/89
17 lines
389 B
Go
17 lines
389 B
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/auth"
|
|
)
|
|
|
|
func BasicAuthMiddleware(handlerFunc http.HandlerFunc, authProvider auth.AuthProvider) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if authProvider.IsAllowed(r) {
|
|
handlerFunc.ServeHTTP(w, r)
|
|
} else {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
}
|
|
}
|
|
}
|