golang middleware for HTTP basic auth.
// Chi
router.Use(basicauth.New("MyRealm", map[string][]string{
"bob": {"password1", "password2"},
}))
// Manual wrapping
middleware := basicauth.New("MyRealm", map[string][]string{
"bob": {"password1", "password2"},
})
h := middlware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request)) {
/// do stuff
})
log.Fatal(http.ListenAndServe(":8080", h))
If your environment looks like this:
SOME_PREFIX_BOB=password
SOME_PREFIX_JANE=password1,password2
you can load it like this:
middleware := basicauth.NewFromEnv("MyRealm", "SOME_PREFIX")