Auth token functionality
This commit is contained in:
21
daemon.go
21
daemon.go
@@ -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)
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user