x/net/http/auth.go

20 lines
404 B
Go
Raw Normal View History

2020-06-12 00:26:57 -04:00
package http
import (
"net/http"
"os"
)
2020-07-27 20:20:10 -04:00
func BasicAuth(h http.Handler) http.HandlerFunc {
2020-06-12 00:26:57 -04:00
return func(w http.ResponseWriter, r *http.Request) {
user, pass, _ := r.BasicAuth()
if !(user == os.Getenv("WIKI_USERNAME") && pass == os.Getenv("WIKI_PASSWORD")) {
w.Header().Set("WWW-Authenticate", `Basic realm="wiki"`)
http.Error(w, "Unauthorized.", 401)
return
}
2020-07-27 20:20:10 -04:00
h.ServeHTTP(w, r)
2020-06-12 00:26:57 -04:00
}
}