x/net/http/auth.go
2020-07-27 17:20:10 -07:00

20 lines
404 B
Go

package http
import (
"net/http"
"os"
)
func BasicAuth(h http.Handler) http.HandlerFunc {
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
}
h.ServeHTTP(w, r)
}
}