From 3fb7d27f3a5dcba8b71f5302edd69780f23d6143 Mon Sep 17 00:00:00 2001 From: TwinProduction Date: Wed, 14 Oct 2020 19:24:36 -0400 Subject: [PATCH] Add security handler (#8) --- security/handler.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 security/handler.go diff --git a/security/handler.go b/security/handler.go new file mode 100644 index 00000000..251a9cef --- /dev/null +++ b/security/handler.go @@ -0,0 +1,18 @@ +package security + +import ( + "net/http" +) + +func Handler(handler http.HandlerFunc, security *Config) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + usernameEntered, passwordEntered, ok := r.BasicAuth() + if !ok || usernameEntered != security.Basic.Username || Sha512(passwordEntered) != security.Basic.PasswordSha512Hash { + w.Header().Set("WWW-Authenticate", "Basic") + w.WriteHeader(http.StatusUnauthorized) + _, _ = w.Write([]byte("Unauthorized")) + return + } + handler(w, r) + } +}