Skip to content

Commit

Permalink
Fixed issue with authClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
jigar-f committed Jun 24, 2024
1 parent 81d396e commit 1a0406b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ require (
github.com/getlantern/idletiming v0.0.0-20231030193830-6767b09f86db
github.com/getlantern/ipproxy v0.0.0-20240305190756-6b5b6347158b
github.com/getlantern/jibber_jabber v0.0.0-20210901195950-68955124cc42
github.com/getlantern/lantern-client v0.0.0-20240529061800-bef437cdab3e
github.com/getlantern/launcher v0.0.0-20230622120034-fe87f9bff286
github.com/getlantern/memhelper v0.0.0-20220104170102-df557102babd
github.com/getlantern/mtime v0.0.0-20200417132445-23682092d1f7
Expand All @@ -66,7 +65,6 @@ require (
github.com/jackpal/gateway v1.0.13
github.com/joho/godotenv v1.5.1
github.com/leekchan/accounting v1.0.0
github.com/moul/http2curl v1.0.0
github.com/shopspring/decimal v1.4.0
github.com/stretchr/testify v1.9.0
golang.org/x/crypto v0.23.0
Expand Down
33 changes: 23 additions & 10 deletions internalsdk/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/getlantern/lantern-client/internalsdk/common"
"github.com/getlantern/lantern-client/internalsdk/protos"
"github.com/getlantern/lantern-client/internalsdk/webclient"

"github.com/getlantern/lantern-client/internalsdk/webclient/defaultwebclient"
"github.com/go-resty/resty/v2"
)
Expand All @@ -18,7 +19,8 @@ var (
)

type authClient struct {
webclient webclient.RESTClient
webclient webclient.RESTClient
userConfig func() common.UserConfig
}

type AuthClient interface {
Expand Down Expand Up @@ -51,19 +53,30 @@ func NewClient(baseURL string, opts *webclient.Opts) AuthClient {
if httpClient == nil {
httpClient = &http.Client{}
}
webclient := webclient.NewRESTClient(defaultwebclient.SendToURL(httpClient, baseURL, func(client *resty.Client, req *resty.Request) error {
req.SetHeader(common.ContentType, "application/x-protobuf")
uc := opts.UserConfig()
if req.URL != "" && strings.HasPrefix(req.URL, "/users/#") {
authClient := &authClient{
userConfig: opts.UserConfig,
}
authClient.webclient = webclient.NewRESTClient(defaultwebclient.SendToURL(httpClient, baseURL, prepareUserRequest(opts.UserConfig), nil))
return authClient
}

func prepareUserRequest(userConfig func() common.UserConfig) func(client *resty.Client, req *http.Request) error {
return func(client *resty.Client, req *http.Request) error {
req.Header.Set(common.ContentType, "application/x-protobuf")
req.Header.Set("Access-Control-Allow-Headers", strings.Join([]string{
common.DeviceIdHeader,
common.ProTokenHeader,
common.UserIdHeader,
}, ", "))
uc := userConfig()
if req.URL != nil && strings.HasSuffix(req.URL.Path, "/users/#") {
// for the /users/# endpoint, we do need to pass all default headers
webclient.AddCommonUserHeaders(uc, req)
common.AddCommonHeadersWithOptions(uc, req, false)
} else {
webclient.AddInternalHeaders(uc, req)
common.AddCommonNonUserHeaders(uc, req)
}
return nil
}, nil))

return &authClient{webclient}
}
}

// Auth APIS
Expand Down
1 change: 0 additions & 1 deletion internalsdk/pro/pro.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ func (c *proClient) Plans(ctx context.Context) (*PlansResponse, error) {
// UserCreate creates a new user
func (c *proClient) UserCreate(ctx context.Context) (*UserDataResponse, error) {
var resp UserDataResponse
log.Debugf("UserCreate header is %v")
err := c.webclient.PostFormReadingJSON(ctx, "/user-create", nil, &resp)
if err != nil {
return nil, errors.New("error fetching user data: %v", err)
Expand Down

0 comments on commit 1a0406b

Please # to comment.