Simple webDAV auth implementation (#82)
This commit is contained in:
parent
f6e155f07e
commit
02842b1917
5 changed files with 25 additions and 4 deletions
webdav
|
@ -8,7 +8,22 @@ import (
|
|||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func NewWebDAVServer(fs fs.Filesystem, port int) error {
|
||||
func NewWebDAVServer(fs fs.Filesystem, port int, user, pass string) error {
|
||||
log.Info().Str("host", fmt.Sprintf("0.0.0.0:%d", port)).Msg("starting webDAV server")
|
||||
return http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), newHandler(fs))
|
||||
|
||||
srv := newHandler(fs)
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
username, password, _ := r.BasicAuth()
|
||||
if username == user && password == pass {
|
||||
srv.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="BASIC WebDAV REALM"`)
|
||||
w.WriteHeader(401)
|
||||
w.Write([]byte("401 Unauthorized\n"))
|
||||
})
|
||||
|
||||
return http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), nil)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue