Files
.github
alerting
client
config
controller
core
docs
examples
jsonpath
metric
pattern
security
config.go
config_test.go
handler.go
handler_test.go
sha512.go
sha512_test.go
storage
util
vendor
watchdog
web
.dockerignore
.gitattributes
.gitignore
Dockerfile
LICENSE.md
Makefile
README.md
config.yaml
go.mod
go.sum
main.go
gatus/security/handler.go
TwinProduction 7bc381b356 Fix typo
2021-05-24 21:46:00 -04:00

21 lines
636 B
Go

package security
import (
"net/http"
"strings"
)
// Handler takes care of security for a given handler with the given security configuration
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) != strings.ToLower(security.Basic.PasswordSha512Hash) {
w.Header().Set("WWW-Authenticate", "Basic")
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte("Unauthorized"))
return
}
handler(w, r)
}
}