Adding legacy API paths
This commit is contained in:
@@ -36,9 +36,14 @@ 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 == "" {
|
||||
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()
|
||||
if count == nil {
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user