Auth token functionality

This commit is contained in:
2026-06-11 20:16:33 -05:00
parent 806ca3d2e7
commit d610a84718
3 changed files with 106 additions and 10 deletions

View File

@@ -55,13 +55,32 @@ func StartDaemon(cfg *AppConfig, configPath string) error {
corsWrappedHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
}
authCheck := func() bool {
ds.RLock()
defer ds.RUnlock()
if ds.cfg.Daemon.AuthToken != "" {
authHeader := r.Header.Get("Authorization")
expected := "Bearer " + ds.cfg.Daemon.AuthToken
if authHeader != expected {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return false
}
}
return true
}()
if !authCheck {
return
}
mux.ServeHTTP(w, r)
})