Skip to content

Instantly share code, notes, and snippets.

@zackproser
Created October 20, 2019 17:46
Show Gist options
  • Select an option

  • Save zackproser/8c01049767dd0e8413363568a36fe066 to your computer and use it in GitHub Desktop.

Select an option

Save zackproser/8c01049767dd0e8413363568a36fe066 to your computer and use it in GitHub Desktop.
func basicAuth(h httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Get the Basic Authentication credentials
user, password, hasAuth := r.BasicAuth()
if hasAuth && user == Config.Server.CatfactsUser && password == Config.Server.CatfactsPassword {
h(w, r, ps)
} else {
// Request Basic Authentication
w.Header().Set("WWW-Authenticate", "Basic realm=Restricted")
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment