cookie ready

This commit is contained in:
2026-05-13 22:34:11 +02:00
parent 8c4e664ca8
commit 1b53c1c199
16 changed files with 798 additions and 146 deletions
+81
View File
@@ -0,0 +1,81 @@
package handlers
import (
"net/http"
"strings"
"github.com/labstack/echo/v4"
appmiddleware "git.ma-al.com/goc_marek/ps_shop/internal/http/middleware"
pscookie "git.ma-al.com/goc_marek/ps_shop/internal/prestashop/cookie"
)
type cookieDecodeResponse struct {
Source string `json:"source"`
CookieName string `json:"cookie_name,omitempty"`
RawCookie string `json:"raw_cookie,omitempty"`
Plaintext string `json:"plaintext,omitempty"`
ParseStatus pscookie.ParseStatus `json:"parse_status"`
IsLoggedIn bool `json:"is_logged_in"`
CustomerID *int64 `json:"customer_id,omitempty"`
CartID *int64 `json:"cart_id,omitempty"`
LanguageID *int64 `json:"language_id,omitempty"`
CurrencyID *int64 `json:"currency_id,omitempty"`
ShopID *int64 `json:"shop_id,omitempty"`
GuestID *int64 `json:"guest_id,omitempty"`
OrderedKeys []string `json:"ordered_keys,omitempty"`
Values map[string]string `json:"values"`
}
func DecodeCookie(codec pscookie.Codec) echo.HandlerFunc {
return func(c echo.Context) error {
raw := strings.TrimSpace(c.FormValue("value"))
if raw == "" {
raw = strings.TrimSpace(c.FormValue("cookie"))
}
source := "request-session"
if raw != "" {
source = "request-parameter"
session, err := codec.Decode(raw)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "prestashop cookie decode failed: "+err.Error())
}
session.RawCookie = raw
return c.JSON(http.StatusOK, newCookieDecodeResponse(source, session))
}
session := appmiddleware.GetSession(c)
if session.RawCookie == "" && session.Plaintext == "" && len(session.Values) == 0 {
return echo.NewHTTPError(http.StatusBadRequest, "missing prestashop cookie; pass ?value=<cookie> or send the cookie in the request")
}
return c.JSON(http.StatusOK, newCookieDecodeResponse(source, session))
}
}
func newCookieDecodeResponse(source string, session *pscookie.SessionContext) cookieDecodeResponse {
if session == nil {
session = &pscookie.SessionContext{Values: map[string]string{}}
}
values := session.Values
if values == nil {
values = map[string]string{}
}
return cookieDecodeResponse{
Source: source,
CookieName: session.CookieName,
RawCookie: session.RawCookie,
Plaintext: session.Plaintext,
ParseStatus: session.ParseStatus,
IsLoggedIn: session.IsLoggedIn,
CustomerID: session.CustomerID,
CartID: session.CartID,
LanguageID: session.LanguageID,
CurrencyID: session.CurrencyID,
ShopID: session.ShopID,
GuestID: session.GuestID,
OrderedKeys: session.OrderedKeys,
Values: values,
}
}
@@ -0,0 +1,96 @@
package handlers
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
appmiddleware "git.ma-al.com/goc_marek/ps_shop/internal/http/middleware"
pscookie "git.ma-al.com/goc_marek/ps_shop/internal/prestashop/cookie"
"github.com/labstack/echo/v4"
)
const (
testCookieKey = "def000008bf3d70e7012b7493c382d561e193218d0c74ab162fb0ea8029ce20e926531b4bcf0aaec9381152e6c161f198e06918b2d1aad67cc7cf40819a51ee328c63830"
testCookie = "def5020099dce5cd9ecf197adb5532a74e3db2ed9cba3d59b98f365353099b710bd562efa48b6bad1ad0a12b2ee54de0fbfcc6baa0545a8234141b03bfc1fbbbb9061af5011764b9c4dfd9c0ddcad767a453e0cc24d6b4a7c524e6c49aabd66ecc390e1a964b6e81a051b171051c829542facbb36cf64fcfebf069906dcc95476578be3fe59aaae466cf70bd9c877d301d908ec3aa4f55366567f460dfefac1684ce381293e8d4138382a42716d6aaecdcc7"
)
func TestDecodeCookieFromQueryParameter(t *testing.T) {
codec, err := pscookie.NewCodec(pscookie.Config{
CookieName: "PrestaShop-test",
CookieKey: testCookieKey,
})
if err != nil {
t.Fatalf("NewCodec() error = %v", err)
}
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/debug/cookie/decode?value="+testCookie, nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
if err := DecodeCookie(codec)(c); err != nil {
t.Fatalf("DecodeCookie() error = %v", err)
}
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
}
var response cookieDecodeResponse
if err := json.Unmarshal(rec.Body.Bytes(), &response); err != nil {
t.Fatalf("json.Unmarshal() error = %v", err)
}
if response.Source != "request-parameter" {
t.Fatalf("source = %q, want request-parameter", response.Source)
}
if response.Values["id_lang"] != "1" {
t.Fatalf("id_lang = %q, want 1", response.Values["id_lang"])
}
}
func TestDecodeCookieFromSession(t *testing.T) {
codec, err := pscookie.NewCodec(pscookie.Config{
CookieName: "PrestaShop-test",
CookieKey: testCookieKey,
})
if err != nil {
t.Fatalf("NewCodec() error = %v", err)
}
session, err := codec.Decode(testCookie)
if err != nil {
t.Fatalf("Decode() error = %v", err)
}
session.CookieName = "PrestaShop-test"
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/debug/cookie/decode", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
appmiddleware.SetSession(c, session)
if err := DecodeCookie(codec)(c); err != nil {
t.Fatalf("DecodeCookie() error = %v", err)
}
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
}
var response cookieDecodeResponse
if err := json.Unmarshal(rec.Body.Bytes(), &response); err != nil {
t.Fatalf("json.Unmarshal() error = %v", err)
}
if response.Source != "request-session" {
t.Fatalf("source = %q, want request-session", response.Source)
}
if response.CookieName != "PrestaShop-test" {
t.Fatalf("cookie_name = %q, want PrestaShop-test", response.CookieName)
}
if response.Values["id_currency"] != "1" {
t.Fatalf("id_currency = %q, want 1", response.Values["id_currency"])
}
}