mirror of
https://github.com/kemko/reproxy.git
synced 2026-01-01 15:55:49 +03:00
27 lines
566 B
Go
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)
|
|
}
|
|
}
|