Skip to content

Commit c4ec34e

Browse files
committed
add key-value Pairs to Body by using @key=value in the req Query. Query Auth now works by using @authorization=TOKEN
1 parent 22013cd commit c4ec34e

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

internals/proxy/proxy.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package proxy
33
import (
44
"bytes"
55
"encoding/base64"
6+
"encoding/json"
67
"io"
78
"net/http"
89
"net/http/httputil"
@@ -61,7 +62,7 @@ func AuthMiddleware(next http.Handler) http.Handler {
6162

6263
authHeader := req.Header.Get("Authorization")
6364

64-
authQuery := req.URL.Query().Get("authorization")
65+
authQuery := req.URL.Query().Get("@authorization")
6566

6667
var authType AuthType = None
6768

@@ -101,6 +102,8 @@ func AuthMiddleware(next http.Handler) http.Handler {
101102

102103
if authToken == token {
103104
success = true
105+
106+
req.URL.Query().Del("@authorization")
104107
}
105108
}
106109

@@ -136,7 +139,7 @@ func TemplatingMiddleware(next http.Handler, VARIABLES map[string]string) http.H
136139
bodyBytes, err := io.ReadAll(req.Body)
137140

138141
if err != nil {
139-
log.Error("Could not decode Body: ", err.Error())
142+
log.Error("Could not read Body: ", err.Error())
140143
http.Error(w, "Internal Error", http.StatusInternalServerError)
141144
return
142145
}
@@ -146,9 +149,43 @@ func TemplatingMiddleware(next http.Handler, VARIABLES map[string]string) http.H
146149
modifiedBody := string(bodyBytes)
147150

148151
modifiedBody, _ = renderTemplate("json", modifiedBody, VARIABLES)
152+
149153
modifiedBodyBytes := []byte(modifiedBody)
150154

155+
if req.URL.Query() != nil {
156+
var modifiedBodyData map[string]interface{}
157+
158+
err = json.Unmarshal(modifiedBodyBytes, &modifiedBodyData)
159+
160+
if err != nil {
161+
log.Error("Could not decode Body: ", err.Error())
162+
http.Error(w, "Internal Error", http.StatusInternalServerError)
163+
return
164+
}
165+
166+
query, _ := renderTemplate("query", req.URL.RawQuery, VARIABLES)
167+
168+
queryData, _ := url.ParseQuery(query)
169+
170+
for key, value := range queryData {
171+
keyWithoutPrefix, found := strings.CutPrefix(key, "@")
172+
173+
if found {
174+
modifiedBodyData[keyWithoutPrefix] = value
175+
}
176+
}
177+
178+
modifiedBodyBytes, err = json.Marshal(modifiedBodyData)
179+
180+
if err != nil {
181+
log.Error("Could not encode Body: ", err.Error())
182+
http.Error(w, "Internal Error", http.StatusInternalServerError)
183+
return
184+
}
185+
}
186+
151187
req.Body = io.NopCloser(bytes.NewReader(modifiedBodyBytes))
188+
152189
req.ContentLength = int64(len(modifiedBody))
153190
req.Header.Set("Content-Length", strconv.Itoa(len(modifiedBody)))
154191
}

0 commit comments

Comments
 (0)