Send SDK version as X-SDK-Version header
Move the cookie-banner SDK version from the POST consents request body to a custom X-SDK-Version header sent on every API call. The server now reads it from the header and the CORS middleware allows it through preflight. Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
@@ -288,7 +288,6 @@ export class CookieBannerClient {
|
|||||||
version: cfg.version,
|
version: cfg.version,
|
||||||
action,
|
action,
|
||||||
consent_data: consentData,
|
consent_data: consentData,
|
||||||
sdk_version: __SDK_VERSION__,
|
|
||||||
};
|
};
|
||||||
void fetchJSON<ConsentRecord>(url, { method: "POST", body })
|
void fetchJSON<ConsentRecord>(url, { method: "POST", body })
|
||||||
.then(() => void flush(this.bannerId))
|
.then(() => void flush(this.bannerId))
|
||||||
|
|||||||
@@ -120,6 +120,7 @@ export async function fetchJSON<T>(
|
|||||||
credentials: "omit",
|
credentials: "omit",
|
||||||
headers: {
|
headers: {
|
||||||
Accept: "application/json",
|
Accept: "application/json",
|
||||||
|
"X-SDK-Version": __SDK_VERSION__,
|
||||||
...(body !== undefined && { "Content-Type": "application/json" }),
|
...(body !== undefined && { "Content-Type": "application/json" }),
|
||||||
...headers,
|
...headers,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ func newCORSMiddleware(logger *log.Logger, cookieBannerSvc *cookiebanner.Service
|
|||||||
|
|
||||||
w.Header().Set("Access-Control-Allow-Origin", origin)
|
w.Header().Set("Access-Control-Allow-Origin", origin)
|
||||||
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
||||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, X-SDK-Version")
|
||||||
w.Header().Set("Access-Control-Max-Age", "600")
|
w.Header().Set("Access-Control-Max-Age", "600")
|
||||||
w.Header().Set("Vary", "Origin")
|
w.Header().Set("Vary", "Origin")
|
||||||
|
|
||||||
|
|||||||
@@ -122,7 +122,6 @@ type (
|
|||||||
Version int `json:"version"`
|
Version int `json:"version"`
|
||||||
Action coredata.CookieConsentAction `json:"action"`
|
Action coredata.CookieConsentAction `json:"action"`
|
||||||
ConsentData json.RawMessage `json:"consent_data"`
|
ConsentData json.RawMessage `json:"consent_data"`
|
||||||
SdkVersion string `json:"sdk_version"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
postConsentResponse struct {
|
postConsentResponse struct {
|
||||||
@@ -148,6 +147,7 @@ func (h *Handler) handlePostConsent(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
ip := clientip.Extract(r)
|
ip := clientip.Extract(r)
|
||||||
ua := r.UserAgent()
|
ua := r.UserAgent()
|
||||||
|
sdkVersion := r.Header.Get("X-SDK-Version")
|
||||||
|
|
||||||
req := cookiebanner.RecordConsentRequest{
|
req := cookiebanner.RecordConsentRequest{
|
||||||
Version: body.Version,
|
Version: body.Version,
|
||||||
@@ -156,7 +156,7 @@ func (h *Handler) handlePostConsent(w http.ResponseWriter, r *http.Request) {
|
|||||||
UserAgent: &ua,
|
UserAgent: &ua,
|
||||||
ConsentData: body.ConsentData,
|
ConsentData: body.ConsentData,
|
||||||
Action: body.Action,
|
Action: body.Action,
|
||||||
SdkVersion: body.SdkVersion,
|
SdkVersion: sdkVersion,
|
||||||
}
|
}
|
||||||
|
|
||||||
record, err := h.cookieBannerSvc.RecordConsent(r.Context(), bannerID, req)
|
record, err := h.cookieBannerSvc.RecordConsent(r.Context(), bannerID, req)
|
||||||
|
|||||||
Reference in New Issue
Block a user