Add basic auth and fix repair issues
This commit is contained in:
17
internal/handlers/basicauth.go
Normal file
17
internal/handlers/basicauth.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package handlers
|
||||
|
||||
import "net/http"
|
||||
|
||||
func (hs *Handlers) basicAuth(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
username, password, ok := r.BasicAuth()
|
||||
if !ok || username != hs.cfg.GetUsername() || password != hs.cfg.GetPassword() {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="restricted"`)
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write([]byte(http.StatusText(http.StatusUnauthorized)))
|
||||
return
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user