From 35fd91332184f90a30494a02e822bf7831a5674e Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 12 May 2019 16:01:25 +0100 Subject: [PATCH] feat: allow no auth mode License: MIT Signed-off-by: Henrique Dias --- cmd/webdav/main.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/cmd/webdav/main.go b/cmd/webdav/main.go index f275a78..adc8cd0 100644 --- a/cmd/webdav/main.go +++ b/cmd/webdav/main.go @@ -163,6 +163,7 @@ type cfg struct { address string port string tls bool + noAuth bool cert string key string auth map[string]string @@ -176,6 +177,7 @@ func parseConfig() *cfg { Port string `json:"port" yaml:"port"` TLS bool `json:"tls" yaml:"tls"` Cert string `json:"cert" yaml:"cert"` + NoAuth bool `json:"noauth" yaml:"noauth"` Key string `json:"key" yaml:"key"` Scope string `json:"scope" yaml:"scope"` Modify bool `json:"modify" yaml:"modify"` @@ -188,6 +190,7 @@ func parseConfig() *cfg { Cert: "cert.pem", Key: "key.pem", Scope: "./", + NoAuth: false, Modify: true, } @@ -208,6 +211,7 @@ func parseConfig() *cfg { tls: data.TLS, cert: data.Cert, key: data.Key, + noAuth: data.NoAuth, auth: map[string]string{}, webdav: &webdav.Config{ User: &webdav.User{ @@ -223,10 +227,6 @@ func parseConfig() *cfg { }, } - if len(data.Users) == 0 { - log.Fatal("no user defined") - } - if len(data.Rules) != 0 { config.webdav.User.Rules = parseRules(data.Rules) } @@ -236,6 +236,12 @@ func parseConfig() *cfg { } func basicAuth(c *cfg) http.Handler { + if c.noAuth { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + c.webdav.ServeHTTP(w, r) + }) + } + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) @@ -273,6 +279,7 @@ func checkPassword(saved, input string) bool { func main() { flag.Parse() cfg := parseConfig() + handler := basicAuth(cfg) // Builds the address and a listener.