Skip to content

Commit

Permalink
Merge branch 'master' into charliecruzan-update-for-connect
Browse files Browse the repository at this point in the history
  • Loading branch information
charliecruzan-stripe committed Sep 20, 2024
2 parents 6f71f17 + 01fb13a commit 0f25676
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
8 changes: 5 additions & 3 deletions pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,14 @@ func (p *Proxy) Run(ctx context.Context) error {

for nAttempts < maxConnectAttempts {
session, err := p.createSession(ctx)
*p.cfg.DeviceToken = session.DeviceToken
if err != nil {
p.cfg.OutCh <- websocket.ErrorElement{
Error: fmt.Errorf("Error while authenticating with Stripe: %v", err),
}
return err
}

*p.cfg.DeviceToken = session.DeviceToken
p.webSocketClient = websocket.NewClient(
session.WebSocketURL,
session.WebSocketID,
Expand Down Expand Up @@ -266,8 +266,10 @@ func (p *Proxy) createSession(ctx context.Context) (*stripeauth.StripeCLISession
// transient errors that we just need to retry for.
for i := 0; i <= 5; i++ {
devURLMap := stripeauth.DeviceURLMap{
ForwardURL: p.cfg.ForwardURL,
ForwardConnectURL: p.cfg.ForwardConnectURL,
ForwardURL: p.cfg.ForwardURL,
ForwardConnectURL: p.cfg.ForwardConnectURL,
ForwardThinURL: p.cfg.ForwardThinURL,
ForwardThinConnectURL: p.cfg.ForwardThinConnectURL,
}

session, err = p.stripeAuthClient.Authorize(ctx, stripeauth.CreateSessionRequest{
Expand Down
13 changes: 10 additions & 3 deletions pkg/stripeauth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ type Client struct {
// DeviceURLMap is a mapping of the urls that the device is listening
// for forwarded events on.
type DeviceURLMap struct {
ForwardURL string
ForwardConnectURL string
ForwardURL string
ForwardConnectURL string
ForwardThinURL string
ForwardThinConnectURL string
}

// CreateSessionRequest defines the API input parameters for client.Authorize.
Expand Down Expand Up @@ -68,10 +70,15 @@ func (c *Client) Authorize(ctx context.Context, req CreateSessionRequest) (*Stri
if len(devURLMap.ForwardURL) > 0 {
form.Add("forward_to_url", devURLMap.ForwardURL)
}

if len(devURLMap.ForwardConnectURL) > 0 {
form.Add("forward_connect_to_url", devURLMap.ForwardConnectURL)
}
if len(devURLMap.ForwardThinURL) > 0 {
form.Add("forward_thin_to_url", devURLMap.ForwardThinURL)
}
if len(devURLMap.ForwardThinConnectURL) > 0 {
form.Add("forward_thin_connect_to_url", devURLMap.ForwardThinConnectURL)
}
}

resp, err := c.client.PerformRequest(ctx, http.MethodPost, stripeCLISessionPath, form.Encode(), nil)
Expand Down
8 changes: 6 additions & 2 deletions pkg/stripeauth/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func TestAuthorizeWithURLDeviceMap(t *testing.T) {
require.Equal(t, "webhooks", r.FormValue("websocket_features[]"))
require.Equal(t, "http://localhost:3000/events", r.FormValue("forward_to_url"))
require.Equal(t, "http://localhost:3000/connect/events", r.FormValue("forward_connect_to_url"))
require.Equal(t, "http://localhost:3000/thin/events", r.FormValue("forward_thin_to_url"))
require.Equal(t, "http://localhost:3000/thin/connect/events", r.FormValue("forward_thin_connect_to_url"))

w.Write([]byte(`{}`))
}))
Expand All @@ -112,8 +114,10 @@ func TestAuthorizeWithURLDeviceMap(t *testing.T) {
client := NewClient(&stripe.Client{APIKey: "sk_test_123", BaseURL: baseURL}, nil)

devURLMap := DeviceURLMap{
ForwardURL: "http://localhost:3000/events",
ForwardConnectURL: "http://localhost:3000/connect/events",
ForwardURL: "http://localhost:3000/events",
ForwardConnectURL: "http://localhost:3000/connect/events",
ForwardThinURL: "http://localhost:3000/thin/events",
ForwardThinConnectURL: "http://localhost:3000/thin/connect/events",
}

_, err := client.Authorize(context.Background(), CreateSessionRequest{
Expand Down

0 comments on commit 0f25676

Please sign in to comment.