commit
d11f802f05
28
main.go
28
main.go
@ -75,7 +75,7 @@ func (u *userHandlers) find(name string) *userHandler {
|
||||
}
|
||||
|
||||
var (
|
||||
auth bool
|
||||
auth string
|
||||
davDir string
|
||||
fullListen string
|
||||
genHtpass bool
|
||||
@ -101,7 +101,7 @@ func init() {
|
||||
flag.StringVar(&tlsCert, "tlscert", "", "TLS certificate.")
|
||||
flag.StringVar(&tlsKey, "tlskey", "", "TLS key.")
|
||||
flag.StringVar(&passPath, "htpass", fmt.Sprintf("%s/.htpasswd", dir), "Path to .htpasswd file..")
|
||||
flag.BoolVar(&auth, "auth", true, "Enable HTTP Basic Authentication.")
|
||||
flag.StringVar(&auth, "auth", "basic", "Enable HTTP Basic Authentication.")
|
||||
flag.BoolVar(&genHtpass, "gen", false, "Generate a .htpasswd file or add a new entry to an existing file.")
|
||||
flag.Parse()
|
||||
|
||||
@ -116,7 +116,6 @@ func init() {
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func authenticate(user string, pass string) bool {
|
||||
@ -221,7 +220,7 @@ func main() {
|
||||
|
||||
_, fErr := os.Stat(passPath)
|
||||
if os.IsNotExist(fErr) {
|
||||
if auth {
|
||||
if auth == "basic" || auth == "header" {
|
||||
fmt.Println("No .htpasswd file found!")
|
||||
os.Exit(1)
|
||||
}
|
||||
@ -251,7 +250,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
if auth {
|
||||
if auth == "basic" || auth == "header" {
|
||||
for u := range users {
|
||||
uPath := path.Join(davDir, u)
|
||||
handlers.list = append(handlers.list, userHandler{
|
||||
@ -290,8 +289,24 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
if auth {
|
||||
if auth == "basic" {
|
||||
user, pass, ok = r.BasicAuth()
|
||||
if !(ok && authenticate(user, pass)) {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="widdler"`)
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
} else if auth == "header" {
|
||||
var prefix = "Auth"
|
||||
for name, values := range r.Header {
|
||||
if strings.HasPrefix(name, prefix) {
|
||||
user = strings.TrimLeft(name, prefix)
|
||||
pass = values[0]
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !(ok && authenticate(user, pass)) {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="widdler"`)
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
@ -392,5 +407,4 @@ func main() {
|
||||
log.Printf("Listening for HTTP on 'http://%s'", listen)
|
||||
log.Fatalln(s.Serve(lis))
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user