Add the ability to disable http auth.

While here use better names fofr the flags
This commit is contained in:
Aaron Bieber 2021-05-18 16:34:49 -06:00
parent 723e91e05d
commit 769dac74ea
2 changed files with 34 additions and 21 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
widdler widdler
.htpasswd

54
main.go
View File

@ -29,6 +29,7 @@ var tiddly embed.FS
var ( var (
davDir string davDir string
listen string listen string
auth bool
passPath string passPath string
users map[string]string users map[string]string
) )
@ -40,9 +41,10 @@ func init() {
log.Fatalln(err) log.Fatalln(err)
} }
flag.StringVar(&davDir, "davdir", dir, "Directory to serve over WebDAV.") flag.StringVar(&davDir, "wikis", dir, "Directory of TiddlyWikis to serve over WebDAV.")
flag.StringVar(&listen, "http", "127.0.0.1:8080", "Listen on") flag.StringVar(&listen, "http", "127.0.0.1:8080", "Listen on")
flag.StringVar(&passPath, "htpass", fmt.Sprintf("%s/.htpasswd", dir), "Path to .htpasswd file..") flag.StringVar(&passPath, "htpass", fmt.Sprintf("%s/.htpasswd", dir), "Path to .htpasswd file..")
flag.BoolVar(&auth, "auth", true, "Enable HTTP Basic Authentication.")
flag.Parse() flag.Parse()
// These are OpenBSD specific protections used to prevent unnecessary file access. // These are OpenBSD specific protections used to prevent unnecessary file access.
@ -52,24 +54,32 @@ func init() {
_ = protect.Unveil("/etc/resolv.conf", "r") _ = protect.Unveil("/etc/resolv.conf", "r")
_ = protect.Pledge("stdio wpath rpath cpath inet dns") _ = protect.Pledge("stdio wpath rpath cpath inet dns")
p, err := os.Open(passPath) _, fErr := os.Stat(passPath)
if err != nil { if os.IsNotExist(fErr) {
log.Fatal(err) if auth {
} fmt.Println("No .htpasswd file found!")
defer p.Close() os.Exit(1)
}
} else {
p, err := os.Open(passPath)
if err != nil {
log.Fatal(err)
}
defer p.Close()
ht := csv.NewReader(p) ht := csv.NewReader(p)
ht.Comma = ':' ht.Comma = ':'
ht.Comment = '#' ht.Comment = '#'
ht.TrimLeadingSpace = true ht.TrimLeadingSpace = true
entries, err := ht.ReadAll() entries, err := ht.ReadAll()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
for _, parts := range entries { for _, parts := range entries {
users[parts[0]] = parts[1] users[parts[0]] = parts[1]
}
} }
} }
@ -128,11 +138,13 @@ func main() {
return return
} }
user, pass, ok := r.BasicAuth() if auth {
if !(ok && authenticate(user, pass)) { user, pass, ok := r.BasicAuth()
w.Header().Set("WWW-Authenticate", `Basic realm="davfs"`) if !(ok && authenticate(user, pass)) {
http.Error(w, "Unauthorized", http.StatusUnauthorized) w.Header().Set("WWW-Authenticate", `Basic realm="davfs"`)
return http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
} }
//wdav.Prefix = user //wdav.Prefix = user