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) } }