Skip to content

Commit 544b407

Browse files
mirek26stainless-app[bot]
authored andcommitted
fix(api): include schema and base URL in GetEmbedURL
1 parent d652bce commit 544b407

File tree

2 files changed

+55
-20
lines changed

2 files changed

+55
-20
lines changed

card.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ func (r *CardService) GetEmbedHTML(ctx context.Context, params CardGetEmbedHTMLP
222222
// but **do not ever embed your API key into front end code, as doing so introduces
223223
// a serious security vulnerability**.
224224
func (r *CardService) GetEmbedURL(ctx context.Context, params CardGetEmbedURLParams, opts ...option.RequestOption) (res *url.URL, err error) {
225+
opts = slices.Concat(r.Options, opts)
225226
buf, err := params.MarshalJSON()
226227
if err != nil {
227228
return nil, err
@@ -240,7 +241,14 @@ func (r *CardService) GetEmbedURL(ctx context.Context, params CardGetEmbedURLPar
240241
if err != nil {
241242
return nil, err
242243
}
243-
return cfg.Request.URL, nil
244+
baseURL := cfg.BaseURL
245+
if baseURL == nil {
246+
baseURL = cfg.DefaultBaseURL
247+
}
248+
if baseURL == nil {
249+
return nil, errors.New("base url is not set")
250+
}
251+
return baseURL.Parse(cfg.Request.URL.String())
244252
}
245253

246254
// Allow your cardholders to directly add payment cards to the device's digital

card_test.go

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -259,31 +259,58 @@ func TestCardGetEmbedHTMLWithOptionalParams(t *testing.T) {
259259
}
260260
}
261261

262-
func TestCardGetEmbedURLWithOptionalParams(t *testing.T) {
263-
baseURL := "http://localhost:4010"
264-
if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
265-
baseURL = envURL
266-
}
267-
if !testutil.CheckTestServer(t, baseURL) {
268-
return
269-
}
262+
func TestCardGetEmbedURL_URLConstruction(t *testing.T) {
263+
// Use fixed inputs to verify URL construction and signature
270264
client := lithic.NewClient(
271-
option.WithBaseURL(baseURL),
272-
option.WithAPIKey("My Lithic API Key"),
265+
option.WithBaseURL("https://sandbox.lithic.com"),
266+
option.WithAPIKey("test-api-key-12345"),
273267
)
274-
_, err := client.Cards.GetEmbedURL(context.TODO(), lithic.CardGetEmbedURLParams{
275-
Token: lithic.F("182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e"),
276-
Css: lithic.F("string"),
277-
Expiration: lithic.F(time.Now()),
278-
TargetOrigin: lithic.F("string"),
268+
269+
expiration, err := time.Parse(time.RFC3339, "2025-01-01T00:00:00Z")
270+
if err != nil {
271+
t.Fatalf("failed to parse expiration: %s", err.Error())
272+
}
273+
274+
url, err := client.Cards.GetEmbedURL(context.TODO(), lithic.CardGetEmbedURLParams{
275+
Token: lithic.F("test-card-token-abc123"),
276+
Css: lithic.F("https://example.com/style.css"),
277+
Expiration: lithic.F(expiration),
278+
TargetOrigin: lithic.F("https://example.com"),
279279
})
280280
if err != nil {
281-
var apierr *lithic.Error
282-
if errors.As(err, &apierr) {
283-
t.Log(string(apierr.DumpRequest(true)))
284-
}
285281
t.Fatalf("err should be nil: %s", err.Error())
286282
}
283+
284+
// Verify URL structure
285+
if url.Scheme != "https" {
286+
t.Errorf("expected scheme 'https', got '%s'", url.Scheme)
287+
}
288+
if url.Host != "sandbox.lithic.com" {
289+
t.Errorf("expected host 'sandbox.lithic.com', got '%s'", url.Host)
290+
}
291+
if url.Path != "/v1/embed/card" {
292+
t.Errorf("expected path '/v1/embed/card', got '%s'", url.Path)
293+
}
294+
295+
// Verify query params exist
296+
query := url.Query()
297+
if query.Get("embed_request") == "" {
298+
t.Error("expected 'embed_request' query param to be present")
299+
}
300+
if query.Get("hmac") == "" {
301+
t.Error("expected 'hmac' query param to be present")
302+
}
303+
304+
// Verify exact values to prevent signature regressions
305+
expectedEmbedRequest := "eyJjc3MiOiJodHRwczovL2V4YW1wbGUuY29tL3N0eWxlLmNzcyIsImV4cGlyYXRpb24iOiIyMDI1LTAxLTAxVDAwOjAwOjAwWiIsInRhcmdldF9vcmlnaW4iOiJodHRwczovL2V4YW1wbGUuY29tIiwidG9rZW4iOiJ0ZXN0LWNhcmQtdG9rZW4tYWJjMTIzIn0="
306+
expectedHmac := "tHf1AsLDIO7gHDA+N/3d5RT446tSmorVbjELGXF/UKQ="
307+
308+
if query.Get("embed_request") != expectedEmbedRequest {
309+
t.Errorf("embed_request mismatch:\n got: %s\n want: %s", query.Get("embed_request"), expectedEmbedRequest)
310+
}
311+
if query.Get("hmac") != expectedHmac {
312+
t.Errorf("hmac mismatch:\n got: %s\n want: %s", query.Get("hmac"), expectedHmac)
313+
}
287314
}
288315

289316
func TestCardProvisionWithOptionalParams(t *testing.T) {

0 commit comments

Comments
 (0)