tweak middleware headers; v0.2.1

This commit is contained in:
Iris Lightshard 2023-01-26 20:54:48 -07:00
parent 10ecb3058f
commit ab1d495514
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398

View file

@ -29,7 +29,7 @@ func Protected(next http.Handler, method string, userStore auth.UserStore, login
req.Method = method
next.ServeHTTP(w, req)
return
} else if err != nil && err.Error() == "Cookie or token expired"{
} else if err != nil && err.Error() == "Cookie or token expired" {
auth.Logout(user, userStore, w)
}
}
@ -100,7 +100,6 @@ func Provision(userStore auth.UserStore, ttl int) http.Handler {
return
}
}
w.Header().Add("WWW-Authenticate", "Basic")
w.WriteHeader(http.StatusUnauthorized)
return
}
@ -110,6 +109,7 @@ func Provision(userStore auth.UserStore, ttl int) http.Handler {
func Validate(next http.Handler, userStore auth.UserStore, scopes map[string]string) http.Handler {
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
errString := ""
authHeader := req.Header.Get("Authorization")
if strings.HasPrefix(authHeader, "Bearer ") {
authToken := strings.Split(authHeader, "Bearer ")[1]
@ -117,9 +117,13 @@ func Validate(next http.Handler, userStore auth.UserStore, scopes map[string]str
if validated && err == nil {
next.ServeHTTP(w, req)
return
} else {
errString = err.Error()
}
} else {
errString = "No authentication data"
}
w.Header().Add("WWW-Authenticate", "Basic")
w.Header().Add("Quartzgun-Error", errString)
w.WriteHeader(http.StatusUnauthorized)
}