82 lines
2.7 KiB
Go
82 lines
2.7 KiB
Go
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,
|
|
}
|
|
}
|