Adding legacy API paths

This commit is contained in:
Caleb Gardner
2024-08-06 22:36:58 -05:00
parent bb44e26dd4
commit a9ca12395e
4 changed files with 23 additions and 5 deletions
@@ -36,8 +36,13 @@ func (b *Backend) countLog(w http.ResponseWriter, r *http.Request) {
var req countLogReq var req countLogReq
err = json.NewDecoder(r.Body).Decode(&req) err = json.NewDecoder(r.Body).Decode(&req)
if err != nil || req.Platform == "" { if err != nil || req.Platform == "" {
ReturnError(w, http.StatusBadRequest, "invalidBody", "Bad request") if r.URL.Query().Get("platform") != "" {
return //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) ap := b.GetApp(hdr.Key)
count := ap.CountTable() count := ap.CountTable()
@@ -72,8 +77,8 @@ func (b *Backend) countLog(w http.ResponseWriter, r *http.Request) {
ReturnError(w, http.StatusInternalServerError, "internal", "Server error") ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
return return
} }
json.NewEncoder(w).Encode(map[string]string{"id": req.ID})
w.WriteHeader(http.StatusCreated) 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 { 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") ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
return err return err
} }
json.NewEncoder(w).Encode(map[string]string{"id": id.String()})
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(map[string]string{"id": id.String()})
return nil return nil
} }
+3
View File
@@ -56,6 +56,9 @@ func NewBackend(keyTable Table[ApiKey], apps ...App) (*Backend, error) {
if hasLog { if hasLog {
b.m.HandleFunc("POST /count", b.countLog) b.m.HandleFunc("POST /count", b.countLog)
b.m.HandleFunc("GET /count", b.getCount) b.m.HandleFunc("GET /count", b.getCount)
//TODO: Remove legacy paths
b.m.HandleFunc("POST /log", b.countLog)
} }
if hasCrash { if hasCrash {
b.m.HandleFunc("POST /crash", b.reportCrash) b.m.HandleFunc("POST /crash", b.reportCrash)
+10
View File
@@ -2,6 +2,7 @@ package backend
import ( import (
"errors" "errors"
"fmt"
"net/http" "net/http"
"strings" "strings"
"time" "time"
@@ -36,6 +37,12 @@ type ParsedHeader struct {
func (b *Backend) ParseHeader(r *http.Request) (*ParsedHeader, error) { func (b *Backend) ParseHeader(r *http.Request) (*ParsedHeader, error) {
out := &ParsedHeader{} out := &ParsedHeader{}
key := r.Header.Get("X-API-Key") 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 ") token := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
if key != "" { if key != "" {
apiKey, err := b.keyTable.Get(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) hdr, err := b.ParseHeader(r)
if hdr == nil || hdr.Key == nil { if hdr == nil || hdr.Key == nil {
if err == ErrApiKeyUnauthorized { if err == ErrApiKeyUnauthorized {
fmt.Println("yo1")
ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized") ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized")
return nil, nil return nil, nil
} }
@@ -111,11 +119,13 @@ func (b *Backend) VerifyHeader(w http.ResponseWriter, r *http.Request, keyPerm s
if allowManagementKey { if allowManagementKey {
return hdr, nil return hdr, nil
} else { } else {
fmt.Println("yo2")
ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized") ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized")
return nil, nil return nil, nil
} }
} }
if _, ok := b.apps[hdr.Key.AppID]; !ok { if _, ok := b.apps[hdr.Key.AppID]; !ok {
fmt.Println("yo3")
ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized") ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized")
return nil, errors.New("server misconfigured, appID present in DB, but App not added to backend") return nil, errors.New("server misconfigured, appID present in DB, but App not added to backend")
} }
+1 -1
View File
@@ -80,7 +80,7 @@ func setupBackend(mux *http.ServeMux) {
swApp, swApp,
cdrApp, cdrApp,
) )
back.AddCorsAddress("darkstorm.tech") back.AddCorsAddress("https://darkstorm.tech")
if err != nil { if err != nil {
log.Fatal("error setting up backend:", err) log.Fatal("error setting up backend:", err)
} }