From 32ccb88f18504bedb7d815b0dc80811eeea1e468 Mon Sep 17 00:00:00 2001 From: Wallace Soares Date: Thu, 23 Jan 2025 10:46:59 -0300 Subject: [PATCH] Adding more info to user-agent --- incognia.go | 20 ++++++++++++++++++++ incognia_test.go | 14 ++++++++++++++ token_client.go | 21 +++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/incognia.go b/incognia.go index 91cef97..e4c8e5f 100644 --- a/incognia.go +++ b/incognia.go @@ -7,6 +7,8 @@ import ( "fmt" "io/ioutil" "net/http" + "runtime" + "runtime/debug" "time" ) @@ -384,7 +386,25 @@ func (c *Client) registerLogin(login *Login) (*TransactionAssessment, error) { } func (c *Client) doRequest(request *http.Request, response interface{}) error { + libVersion := "unknown" + if buildInfo, ok := debug.ReadBuildInfo(); ok { + for _, dep := range buildInfo.Deps { + if dep.Path == "repo.incognia.com/go/incognia" { + libVersion = dep.Version + } + } + } + + userAgent := fmt.Sprintf( + "incognia-api-go/%s (%s %s) Go/%s", + libVersion, + runtime.GOOS, + runtime.GOARCH, + runtime.Version(), + ) + request.Header.Add("Content-Type", "application/json") + request.Header.Add("User-agent", userAgent) err := c.authorizeRequest(request) if err != nil { diff --git a/incognia_test.go b/incognia_test.go index d19da74..252e4d5 100644 --- a/incognia_test.go +++ b/incognia_test.go @@ -5,6 +5,7 @@ import ( "net/http" "net/http/httptest" "reflect" + "regexp" "strconv" "strings" "testing" @@ -21,6 +22,7 @@ const ( ) var ( + userAgentRegex = regexp.MustCompile(`^incognia-api-go(/(v[0-9]+\.[0-9]+\.[0-9]+|unknown))? \([a-z]+ [a-z0-9]+\) Go/go[0-9]+\.[0-9]+\.[0-9]+$`) now = time.Now() nowMinusSeconds = now.Add(-1 * time.Second) installationId = "installation-id" @@ -957,6 +959,9 @@ func (suite *IncogniaTestSuite) mockFeedbackEndpoint(expectedToken string, expec feedbackServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("content-type", "application/json") + userAgent := r.Header.Get("User-Agent") + suite.True(userAgentRegex.MatchString(userAgent), "User-Agent header does not match the expected format") + if !isRequestAuthorized(r, expectedToken) { w.WriteHeader(http.StatusForbidden) return @@ -991,6 +996,9 @@ func (suite *IncogniaTestSuite) mockTokenEndpointUnauthorized() *httptest.Server tokenServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("content-type", "application/json") w.WriteHeader(http.StatusUnauthorized) + + userAgent := r.Header.Get("User-Agent") + suite.True(userAgentRegex.MatchString(userAgent), "User-Agent header does not match the expected format") })) return tokenServer @@ -1000,6 +1008,9 @@ func (suite *IncogniaTestSuite) mockPostTransactionsEndpoint(expectedToken strin transactionsServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("content-type", "application/json") + userAgent := r.Header.Get("User-Agent") + suite.True(userAgentRegex.MatchString(userAgent), "User-Agent header does not match the expected format") + if !isRequestAuthorized(r, expectedToken) { w.WriteHeader(http.StatusForbidden) return @@ -1038,6 +1049,9 @@ func (suite *IncogniaTestSuite) mockPostSignupsEndpoint(expectedToken string, ex signupsServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("content-type", "application/json") + userAgent := r.Header.Get("User-Agent") + suite.True(userAgentRegex.MatchString(userAgent), "User-Agent header does not match the expected format") + if !isRequestAuthorized(r, expectedToken) { w.WriteHeader(http.StatusForbidden) return diff --git a/token_client.go b/token_client.go index 2482453..b28f937 100644 --- a/token_client.go +++ b/token_client.go @@ -3,7 +3,10 @@ package incognia import ( "encoding/json" "errors" + "fmt" "net/http" + "runtime" + "runtime/debug" "strconv" "time" ) @@ -51,8 +54,26 @@ func (tm TokenClient) requestToken() (Token, error) { return nil, err } + libVersion := "unknown" + if buildInfo, ok := debug.ReadBuildInfo(); ok { + for _, dep := range buildInfo.Deps { + if dep.Path == "repo.incognia.com/go/incognia" { + libVersion = dep.Version + } + } + } + + userAgent := fmt.Sprintf( + "incognia-api-go/%s (%s %s) Go/%s", + libVersion, + runtime.GOOS, + runtime.GOARCH, + runtime.Version(), + ) + req.SetBasicAuth(tm.ClientID, tm.ClientSecret) req.Header.Add("content-type", "application/x-www-form-urlencoded") + req.Header.Add("User-agent", userAgent) res, err := tm.netClient.Do(req) if err != nil {