Files
reproxy/vendor/github.com/go-pkgz/rest/basic_auth.go
2021-04-02 14:27:40 -05:00

27 lines
566 B
Go

package rest
import (
"net/http"
)
// BasicAuth middleware requires basic auth and matches user & passwd with client-provided checker
func BasicAuth(checker func(user, passwd string) bool) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
u, p, ok := r.BasicAuth()
if !ok {
w.WriteHeader(http.StatusUnauthorized)
return
}
if !checker(u, p) {
w.WriteHeader(http.StatusForbidden)
return
}
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
}