Skip to content

Commit

Permalink
rename Telemetry to Auth0ClientInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
ewanharris committed Feb 13, 2023
1 parent df29e05 commit 6256c6d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 50 deletions.
30 changes: 15 additions & 15 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@ import (
// UserAgent is the default user agent string.
var UserAgent = fmt.Sprintf("Go-Auth0-SDK/%s", auth0.Version)

// Telemetry is the structure used to send telemetry data in the "Auth0-Client" header.
type Telemetry struct {
// Auth0ClientInfo is the structure used to send client information in the "Auth0-Client" header.
type Auth0ClientInfo struct {
Name string `json:"name"`
Version string `json:"version"`
Env map[string]string `json:"env,omitempty"`
}

// IsEmpty checks whether the provide Telemetry data is nil or has no data to allow
// IsEmpty checks whether the provided Auth0ClientInfo data is nil or has no data to allow
// short-circuiting the "Auth0-Client" header configuration.
func (td *Telemetry) IsEmpty() bool {
func (td *Auth0ClientInfo) IsEmpty() bool {
if td == nil {
return true
}
return td.Name == "" && td.Version == "" && len(td.Env) == 0
}

// DefaultTelemetryData is the default telemetry data sent by the go-auth0 SDK.
var DefaultTelemetryData = &Telemetry{Name: "go-auth0", Version: auth0.Version}
// DefaultAuth0ClientInfo is the default client information sent by the go-auth0 SDK.
var DefaultAuth0ClientInfo = &Auth0ClientInfo{Name: "go-auth0", Version: auth0.Version}

// RoundTripFunc is an adapter to allow the use of ordinary functions as HTTP
// round trips.
Expand Down Expand Up @@ -91,21 +91,21 @@ func UserAgentTransport(base http.RoundTripper, userAgent string) http.RoundTrip
})
}

// TelemetryTransport wraps base transport with a customized "Auth0-Client" header.
func TelemetryTransport(base http.RoundTripper, telemetryData *Telemetry) (http.RoundTripper, error) {
// Auth0ClientInfoTransport wraps base transport with a customized "Auth0-Client" header.
func Auth0ClientInfoTransport(base http.RoundTripper, auth0ClientInfo *Auth0ClientInfo) (http.RoundTripper, error) {
if base == nil {
base = http.DefaultTransport
}

telemetryDataJson, err := json.Marshal(telemetryData)
auth0ClientJson, err := json.Marshal(auth0ClientInfo)
if err != nil {
return nil, err
}

encodedTelemetry := base64.StdEncoding.EncodeToString(telemetryDataJson)
auth0ClientEncoded := base64.StdEncoding.EncodeToString(auth0ClientJson)

return RoundTripFunc(func(req *http.Request) (*http.Response, error) {
req.Header.Set("Auth0-Client", encodedTelemetry)
req.Header.Set("Auth0-Client", auth0ClientEncoded)
return base.RoundTrip(req)
}), nil
}
Expand Down Expand Up @@ -164,13 +164,13 @@ func WithUserAgent(userAgent string) Option {
}
}

// WithTelemetry configures the client to overwrite the "Auth0-Client" header.
func WithTelemetry(telemetryData *Telemetry) Option {
// WithAuth0ClientInfo configures the client to overwrite the "Auth0-Client" header.
func WithAuth0ClientInfo(auth0ClientInfo *Auth0ClientInfo) Option {
return func(c *http.Client) {
if telemetryData.IsEmpty() {
if auth0ClientInfo.IsEmpty() {
return
}
transport, err := TelemetryTransport(c.Transport, telemetryData)
transport, err := Auth0ClientInfoTransport(c.Transport, auth0ClientInfo)
if err != nil {
return
}
Expand Down
12 changes: 6 additions & 6 deletions internal/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,25 @@ func TestOAuth2ClientCredentialsAndAudience(t *testing.T) {
assert.Equal(t, "someToken", token.AccessToken)
}

func TestWrapTelemetry(t *testing.T) {
func TestWrapAuth0ClientInfo(t *testing.T) {
var testCases = []struct {
name string
given Telemetry
given Auth0ClientInfo
expected string
}{
{
name: "Default client",
given: *DefaultTelemetryData,
given: *DefaultAuth0ClientInfo,
expected: "eyJuYW1lIjoiZ28tYXV0aDAiLCJ2ZXJzaW9uIjoibGF0ZXN0In0=",
},
{
name: "Custom client",
given: Telemetry{"foo", "1.0.0", map[string]string{"os": "windows"}},
given: Auth0ClientInfo{"foo", "1.0.0", map[string]string{"os": "windows"}},
expected: "eyJuYW1lIjoiZm9vIiwidmVyc2lvbiI6IjEuMC4wIiwiZW52Ijp7Im9zIjoid2luZG93cyJ9fQ==",
},
{
name: "Missing information",
given: Telemetry{Name: "foo"},
given: Auth0ClientInfo{Name: "foo"},
expected: "eyJuYW1lIjoiZm9vIiwidmVyc2lvbiI6IiJ9",
},
}
Expand All @@ -131,7 +131,7 @@ func TestWrapTelemetry(t *testing.T) {
testServer.Close()
})

httpClient := Wrap(testServer.Client(), StaticToken(""), WithTelemetry(&testCase.given))
httpClient := Wrap(testServer.Client(), StaticToken(""), WithAuth0ClientInfo(&testCase.given))
_, err := httpClient.Get(testServer.URL)
assert.NoError(t, err)
})
Expand Down
32 changes: 16 additions & 16 deletions management/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ type Management struct {
// EmailProvider manages Auth0 Email Providers.
EmailProvider *EmailProviderManager

url *url.URL
basePath string
userAgent string
debug bool
ctx context.Context
tokenSource oauth2.TokenSource
http *http.Client
telemetry *client.Telemetry
url *url.URL
basePath string
userAgent string
debug bool
ctx context.Context
tokenSource oauth2.TokenSource
http *http.Client
auth0ClientInfo *client.Auth0ClientInfo
}

// New creates a new Auth0 Management client by authenticating using the
Expand All @@ -134,13 +134,13 @@ func New(domain string, options ...Option) (*Management, error) {
}

m := &Management{
url: u,
basePath: "api/v2",
userAgent: client.UserAgent,
debug: false,
ctx: context.Background(),
http: http.DefaultClient,
telemetry: client.DefaultTelemetryData,
url: u,
basePath: "api/v2",
userAgent: client.UserAgent,
debug: false,
ctx: context.Background(),
http: http.DefaultClient,
auth0ClientInfo: client.DefaultAuth0ClientInfo,
}

for _, option := range options {
Expand All @@ -153,7 +153,7 @@ func New(domain string, options ...Option) (*Management, error) {
client.WithDebug(m.debug),
client.WithUserAgent(m.userAgent),
client.WithRateLimit(),
client.WithTelemetry(m.telemetry),
client.WithAuth0ClientInfo(m.auth0ClientInfo),
)

m.Client = newClientManager(m)
Expand Down
14 changes: 7 additions & 7 deletions management/management_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,20 @@ func WithClient(client *http.Client) Option {
}
}

// WithTelemetry configures the management client to use the provided telemetry data
// WithAuth0ClientInfo configures the management client to use the provided client information
// instead of the default one.
func WithTelemetry(telemetry client.Telemetry) Option {
func WithAuth0ClientInfo(auth0ClientInfo client.Auth0ClientInfo) Option {
return func(m *Management) {
if !telemetry.IsEmpty() {
m.telemetry = &telemetry
if !auth0ClientInfo.IsEmpty() {
m.auth0ClientInfo = &auth0ClientInfo
}
}
}

// WithNoTelemetry configures the management client to not send the "Auth0-Client" header
// WithNoAuth0ClientInfo configures the management client to not send the "Auth0-Client" header
// at all.
func WithNoTelemetry() Option {
func WithNoAuth0ClientInfo() Option {
return func(m *Management) {
m.telemetry = nil
m.auth0ClientInfo = nil
}
}
12 changes: 6 additions & 6 deletions management/management_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func TestManagement_URI(t *testing.T) {
}
}

func TestTelemetry(t *testing.T) {
func TestAuth0Client(t *testing.T) {
t.Run("Defaults to the default data", func(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
header := r.Header.Get("Auth0-Client")
Expand All @@ -276,8 +276,8 @@ func TestTelemetry(t *testing.T) {
assert.NoError(t, err)
})

t.Run("Allows passing a custom telemetry data", func(t *testing.T) {
customClient := client.Telemetry{Name: "test-client", Version: "1.0.0"}
t.Run("Allows passing custom Auth0ClientInfo", func(t *testing.T) {
customClient := client.Auth0ClientInfo{Name: "test-client", Version: "1.0.0"}

h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
header := r.Header.Get("Auth0-Client")
Expand All @@ -288,7 +288,7 @@ func TestTelemetry(t *testing.T) {
m, err := New(
s.URL,
WithInsecure(),
WithTelemetry(customClient),
WithAuth0ClientInfo(customClient),
)
assert.NoError(t, err)

Expand All @@ -297,7 +297,7 @@ func TestTelemetry(t *testing.T) {
assert.NoError(t, err)
})

t.Run("Allows disabling telemetry", func(t *testing.T) {
t.Run("Allows disabling Auth0ClientInfo", func(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rawHeader := r.Header.Get("Auth0-Client")
assert.Empty(t, rawHeader)
Expand All @@ -307,7 +307,7 @@ func TestTelemetry(t *testing.T) {
m, err := New(
s.URL,
WithInsecure(),
WithNoTelemetry(),
WithNoAuth0ClientInfo(),
)
assert.NoError(t, err)
_, err = m.User.Read("123")
Expand Down

0 comments on commit 6256c6d

Please sign in to comment.