2020-10-15 01:24:36 +02:00
|
|
|
package security
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2020-10-15 03:45:45 +02:00
|
|
|
"strings"
|
2020-10-15 01:24:36 +02:00
|
|
|
)
|
|
|
|
|
2021-05-25 03:46:00 +02:00
|
|
|
// Handler takes care of security for a given handler with the given security configuration
|
2020-10-15 01:24:36 +02:00
|
|
|
func Handler(handler http.HandlerFunc, security *Config) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
usernameEntered, passwordEntered, ok := r.BasicAuth()
|
2020-10-15 03:45:45 +02:00
|
|
|
if !ok || usernameEntered != security.Basic.Username || Sha512(passwordEntered) != strings.ToLower(security.Basic.PasswordSha512Hash) {
|
2020-10-15 01:24:36 +02:00
|
|
|
w.Header().Set("WWW-Authenticate", "Basic")
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
_, _ = w.Write([]byte("Unauthorized"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
handler(w, r)
|
|
|
|
}
|
|
|
|
}
|