Apply basic auth to whole server

This commit is contained in:
Ben Sarmiento
2024-01-15 23:04:11 +01:00
parent 5b8ea46fa4
commit e65ade4f38

View File

@@ -2,13 +2,12 @@ package handlers
import ( import (
"net/http" "net/http"
"strings"
) )
// basicAuth is a middleware that performs basic authentication. // basicAuth is a middleware that performs basic authentication.
func (hs *Handlers) basicAuth(next http.Handler) http.Handler { func (hs *Handlers) basicAuth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "OPTIONS" && needsAuth(r.URL.Path) { if r.Method != "OPTIONS" {
if !hs.authenticate(r) { if !hs.authenticate(r) {
hs.unauthorized(w) hs.unauthorized(w)
return return
@@ -20,15 +19,15 @@ func (hs *Handlers) basicAuth(next http.Handler) http.Handler {
} }
// needsAuth checks if the given path requires authentication. // needsAuth checks if the given path requires authentication.
func needsAuth(path string) bool { // func needsAuth(path string) bool {
authenticatedPaths := []string{"/http/", "/dav/", "/infuse/"} // authenticatedPaths := []string{"/http/", "/dav/", "/infuse/"}
for _, p := range authenticatedPaths { // for _, p := range authenticatedPaths {
if strings.HasPrefix(path, p) { // if strings.HasPrefix(path, p) {
return true // return true
} // }
} // }
return false // return false
} // }
// authenticate performs the basic authentication check. // authenticate performs the basic authentication check.
func (hs *Handlers) authenticate(r *http.Request) bool { func (hs *Handlers) authenticate(r *http.Request) bool {