Merge pull request #5

Adds header authentication
This commit is contained in:
Aaron Bieber 2021-12-10 06:52:44 -07:00 committed by GitHub
commit d11f802f05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

28
main.go
View File

@ -75,7 +75,7 @@ func (u *userHandlers) find(name string) *userHandler {
} }
var ( var (
auth bool auth string
davDir string davDir string
fullListen string fullListen string
genHtpass bool genHtpass bool
@ -101,7 +101,7 @@ func init() {
flag.StringVar(&tlsCert, "tlscert", "", "TLS certificate.") flag.StringVar(&tlsCert, "tlscert", "", "TLS certificate.")
flag.StringVar(&tlsKey, "tlskey", "", "TLS key.") flag.StringVar(&tlsKey, "tlskey", "", "TLS key.")
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.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.BoolVar(&genHtpass, "gen", false, "Generate a .htpasswd file or add a new entry to an existing file.")
flag.Parse() flag.Parse()
@ -116,7 +116,6 @@ func init() {
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
} }
func authenticate(user string, pass string) bool { func authenticate(user string, pass string) bool {
@ -221,7 +220,7 @@ func main() {
_, fErr := os.Stat(passPath) _, fErr := os.Stat(passPath)
if os.IsNotExist(fErr) { if os.IsNotExist(fErr) {
if auth { if auth == "basic" || auth == "header" {
fmt.Println("No .htpasswd file found!") fmt.Println("No .htpasswd file found!")
os.Exit(1) os.Exit(1)
} }
@ -251,7 +250,7 @@ func main() {
} }
} }
if auth { if auth == "basic" || auth == "header" {
for u := range users { for u := range users {
uPath := path.Join(davDir, u) uPath := path.Join(davDir, u)
handlers.list = append(handlers.list, userHandler{ handlers.list = append(handlers.list, userHandler{
@ -290,8 +289,24 @@ func main() {
return return
} }
if auth { if auth == "basic" {
user, pass, ok = r.BasicAuth() 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)) { if !(ok && authenticate(user, pass)) {
w.Header().Set("WWW-Authenticate", `Basic realm="widdler"`) w.Header().Set("WWW-Authenticate", `Basic realm="widdler"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized) http.Error(w, "Unauthorized", http.StatusUnauthorized)
@ -392,5 +407,4 @@ func main() {
log.Printf("Listening for HTTP on 'http://%s'", listen) log.Printf("Listening for HTTP on 'http://%s'", listen)
log.Fatalln(s.Serve(lis)) log.Fatalln(s.Serve(lis))
} }
} }