package helpers import ( "crypto/subtle" "net/http" "tea.chunkbyte.com/kato/go-worm/lib/config" ) func BasicAuth(next http.Handler) http.Handler { user := []byte(config.AuthUser) pass := []byte(config.AuthPass) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/openapi", "/openapi.json": next.ServeHTTP(w, r) return } u, p, ok := r.BasicAuth() if !ok || subtle.ConstantTimeCompare([]byte(u), user) != 1 || subtle.ConstantTimeCompare([]byte(p), pass) != 1 { w.Header().Set("WWW-Authenticate", `Basic realm="win64_mp"`) WriteError(w, http.StatusUnauthorized, "unauthorized") return } next.ServeHTTP(w, r) }) }