Trying to optimize

This commit is contained in:
Neale Pickett 2023-02-22 18:19:26 -07:00
parent 0dd54351e4
commit 5293240b89
2 changed files with 26 additions and 19 deletions

View File

@ -125,6 +125,10 @@ WebDAV client code to work with anything else I found.
* Keycloak - Didn't try it, looked way too complicated. * Keycloak - Didn't try it, looked way too complicated.
# Todo
* [ ] Performance testing: somehow this takes more CPU than caddy?
# Project Home # Project Home
The canonical home for this project is The canonical home for this project is

View File

@ -56,10 +56,10 @@ func usernameIfAuthenticated(req *http.Request) string {
return authUsername return authUsername
} }
} else { } else {
debugf("no basic auth") debugf("no basic auth")
} }
ncookies := 0 ncookies := 0
for i, cookie := range req.Cookies() { for i, cookie := range req.Cookies() {
if cookie.Name != CookieName { if cookie.Name != CookieName {
continue continue
@ -70,11 +70,11 @@ func usernameIfAuthenticated(req *http.Request) string {
if valid { if valid {
return t.Username return t.Username
} }
ncookies += 1 ncookies += 1
}
if ncookies == 0 {
debugf("no cookies")
} }
if ncookies == 0 {
debugf("no cookies")
}
return "" return ""
} }
@ -91,16 +91,17 @@ func rootHandler(w http.ResponseWriter, req *http.Request) {
status = "succeeded" status = "succeeded"
w.Header().Set("X-Simpleauth-Username", username) w.Header().Set("X-Simpleauth-Username", username)
if !login { if login {
// Send back a token; this will turn into a cookie
t := token.New(secret, username, time.Now().Add(lifespan))
w.Header().Set("X-Simpleauth-Cookie", fmt.Sprintf("%s=%s", CookieName, t.String()))
w.Header().Set("X-Simpleauth-Token", t.String())
} else {
// This is the only time simpleauth returns 200 // This is the only time simpleauth returns 200
// That will cause Caddy to proceed with the original request // That will cause Caddy to proceed with the original request
http.Error(w, "Success", http.StatusOK) http.Error(w, "Success", http.StatusOK)
return return
} }
// Send back a token; this will turn into a cookie
t := token.New(secret, username, time.Now().Add(lifespan))
w.Header().Set("X-Simpleauth-Cookie", fmt.Sprintf("%s=%s", CookieName, t.String()))
w.Header().Set("X-Simpleauth-Token", t.String())
// Fall through to the 401 response, though, // Fall through to the 401 response, though,
// so that Caddy will send our response back to the client, // so that Caddy will send our response back to the client,
// which needs these headers to set the cookie and try again. // which needs these headers to set the cookie and try again.
@ -113,16 +114,18 @@ func rootHandler(w http.ResponseWriter, req *http.Request) {
forwardedMethod := req.Header.Get("X-Forwarded-Method") forwardedMethod := req.Header.Get("X-Forwarded-Method")
forwardedURL := url.URL{ forwardedURL := url.URL{
Scheme: req.Header.Get("X-Forwarded-Proto"), Scheme: req.Header.Get("X-Forwarded-Proto"),
Host: req.Header.Get("X-Forwarded-Host"), Host: req.Header.Get("X-Forwarded-Host"),
Path: req.Header.Get("X-Forwarded-Uri"), Path: req.Header.Get("X-Forwarded-Uri"),
User: url.UserPassword(username, ""), User: url.UserPassword(username, ""),
} }
// Log the request // Log the request
log.Printf("%s %s %s login:%v %s", if false {
clientIP, forwardedMethod, forwardedURL.String(), log.Printf("%s %s %s login:%v %s",
login, status, clientIP, forwardedMethod, forwardedURL.String(),
) login, status,
)
}
w.Header().Set("Content-Type", "text/html") w.Header().Set("Content-Type", "text/html")
w.Header().Set("X-Simpleauth-Authentication", status) w.Header().Set("X-Simpleauth-Authentication", status)