From a9ca12395e32aad452140f913361f22bb3f2c16c Mon Sep 17 00:00:00 2001 From: Caleb Gardner Date: Tue, 6 Aug 2024 22:36:58 -0500 Subject: [PATCH] Adding legacy API paths --- internal/backend/{log.go => count.go} | 13 +++++++++---- internal/backend/darkstorm.go | 3 +++ internal/backend/header.go | 10 ++++++++++ main.go | 2 +- 4 files changed, 23 insertions(+), 5 deletions(-) rename internal/backend/{log.go => count.go} (93%) diff --git a/internal/backend/log.go b/internal/backend/count.go similarity index 93% rename from internal/backend/log.go rename to internal/backend/count.go index 4f936eb..212c7e5 100644 --- a/internal/backend/log.go +++ b/internal/backend/count.go @@ -36,8 +36,13 @@ func (b *Backend) countLog(w http.ResponseWriter, r *http.Request) { var req countLogReq err = json.NewDecoder(r.Body).Decode(&req) if err != nil || req.Platform == "" { - ReturnError(w, http.StatusBadRequest, "invalidBody", "Bad request") - return + if r.URL.Query().Get("platform") != "" { + //TODO: remove legacy code + req.Platform = r.URL.Query().Get("platform") + } else { + ReturnError(w, http.StatusBadRequest, "invalidBody", "Bad request") + return + } } ap := b.GetApp(hdr.Key) count := ap.CountTable() @@ -72,8 +77,8 @@ func (b *Backend) countLog(w http.ResponseWriter, r *http.Request) { ReturnError(w, http.StatusInternalServerError, "internal", "Server error") return } - json.NewEncoder(w).Encode(map[string]string{"id": req.ID}) w.WriteHeader(http.StatusCreated) + json.NewEncoder(w).Encode(map[string]string{"id": req.ID}) } func addToCountTable(w http.ResponseWriter, c CountTable, platform string, curDate int) error { @@ -93,8 +98,8 @@ func addToCountTable(w http.ResponseWriter, c CountTable, platform string, curDa ReturnError(w, http.StatusInternalServerError, "internal", "Server error") return err } - json.NewEncoder(w).Encode(map[string]string{"id": id.String()}) w.WriteHeader(http.StatusCreated) + json.NewEncoder(w).Encode(map[string]string{"id": id.String()}) return nil } diff --git a/internal/backend/darkstorm.go b/internal/backend/darkstorm.go index cf63beb..a6255ea 100644 --- a/internal/backend/darkstorm.go +++ b/internal/backend/darkstorm.go @@ -56,6 +56,9 @@ func NewBackend(keyTable Table[ApiKey], apps ...App) (*Backend, error) { if hasLog { b.m.HandleFunc("POST /count", b.countLog) b.m.HandleFunc("GET /count", b.getCount) + + //TODO: Remove legacy paths + b.m.HandleFunc("POST /log", b.countLog) } if hasCrash { b.m.HandleFunc("POST /crash", b.reportCrash) diff --git a/internal/backend/header.go b/internal/backend/header.go index d72ce93..755d7a2 100644 --- a/internal/backend/header.go +++ b/internal/backend/header.go @@ -2,6 +2,7 @@ package backend import ( "errors" + "fmt" "net/http" "strings" "time" @@ -36,6 +37,12 @@ type ParsedHeader struct { func (b *Backend) ParseHeader(r *http.Request) (*ParsedHeader, error) { out := &ParsedHeader{} key := r.Header.Get("X-API-Key") + + //TODO: Remove legacy code + if key == "" { + key = r.URL.Query().Get("key") + } + token := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ") if key != "" { apiKey, err := b.keyTable.Get(key) @@ -97,6 +104,7 @@ func (b *Backend) VerifyHeader(w http.ResponseWriter, r *http.Request, keyPerm s hdr, err := b.ParseHeader(r) if hdr == nil || hdr.Key == nil { if err == ErrApiKeyUnauthorized { + fmt.Println("yo1") ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized") return nil, nil } @@ -111,11 +119,13 @@ func (b *Backend) VerifyHeader(w http.ResponseWriter, r *http.Request, keyPerm s if allowManagementKey { return hdr, nil } else { + fmt.Println("yo2") ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized") return nil, nil } } if _, ok := b.apps[hdr.Key.AppID]; !ok { + fmt.Println("yo3") ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized") return nil, errors.New("server misconfigured, appID present in DB, but App not added to backend") } diff --git a/main.go b/main.go index ed5f0b1..73dc460 100644 --- a/main.go +++ b/main.go @@ -80,7 +80,7 @@ func setupBackend(mux *http.ServeMux) { swApp, cdrApp, ) - back.AddCorsAddress("darkstorm.tech") + back.AddCorsAddress("https://darkstorm.tech") if err != nil { log.Fatal("error setting up backend:", err) }