Skip to content

Commit

Permalink
Merge pull request #195 from auth0/update-telemetry
Browse files Browse the repository at this point in the history
Update Telemetry format and allow to customize it
  • Loading branch information
lbalmaceda authored Apr 17, 2019
2 parents f3bb0f4 + cdf8649 commit 4648f6c
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 35 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/auth0/client/auth/AuthAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ public void doNotSendTelemetry() {
telemetry.setEnabled(false);
}

/**
* Setter for the Telemetry to send in every request to Auth0.
*
* @param telemetry to send in every request to Auth0
*/
public void setTelemetry(Telemetry telemetry) {
this.telemetry.setTelemetry(telemetry);
}

/**
* Whether to enable or not the current HTTP Logger for every Request, Response and other sensitive information.
*
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/auth0/client/mgmt/ManagementAPI.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.auth0.client.mgmt;

import com.auth0.net.Telemetry;
import com.auth0.net.TelemetryInterceptor;
import com.auth0.utils.Asserts;
import okhttp3.HttpUrl;
Expand Down Expand Up @@ -65,6 +66,15 @@ public void doNotSendTelemetry() {
telemetry.setEnabled(false);
}

/**
* Setter for the Telemetry to send in every request to Auth0.
*
* @param telemetry to send in every request to Auth0
*/
public void setTelemetry(Telemetry telemetry) {
this.telemetry.setTelemetry(telemetry);
}

/**
* Whether to enable or not the current HTTP Logger for every Request, Response and other sensitive information.
*
Expand Down
75 changes: 55 additions & 20 deletions src/main/java/com/auth0/net/Telemetry.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,85 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.codec.binary.Base64;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("WeakerAccess")
class Telemetry {
public class Telemetry {
static final String HEADER_NAME = "Auth0-Client";

private static final String NAME_KEY = "name";
private static final String VERSION_KEY = "version";
private static final String LIBRARY_VERSION_KEY = "auth0-java";
private static final String ENV_KEY = "env";
private static final String JAVA_KEY = "java";

private final String name;
private final String version;
private final String libraryVersion;
private final Map<String, String> env;
private final String value;

Telemetry(String name, String version) {
this.name = name;
this.version = version;
}

public String getName() {
return name;
public Telemetry(String name, String version) {
this(name, version, null);
}

public String getVersion() {
return version;
}
public Telemetry(String name, String version, String libraryVersion) {
this.name = name;
this.version = version;
this.libraryVersion = libraryVersion;

public String getValue() {
Map<String, String> values = new HashMap<>();
if (name != null) {
values.put(NAME_KEY, name);
if (name == null) {
env = Collections.emptyMap();
value = null;
return;
}

Map<String, Object> values = new HashMap<>();
values.put(NAME_KEY, name);
if (version != null) {
values.put(VERSION_KEY, version);
}
if (values.isEmpty()) {
return null;

HashMap<String, String> tmpEnv = new HashMap<>();
tmpEnv.put(JAVA_KEY, Runtime.class.getPackage().getSpecificationVersion());
if (libraryVersion != null) {
tmpEnv.put(LIBRARY_VERSION_KEY, libraryVersion);
}
String urlSafe = null;
this.env = Collections.unmodifiableMap(tmpEnv);
values.put(ENV_KEY, env);

String tmpValue;
try {
String json = new ObjectMapper().writeValueAsString(values);
urlSafe = Base64.encodeBase64URLSafeString(json.getBytes());
tmpValue = Base64.encodeBase64URLSafeString(json.getBytes());
} catch (JsonProcessingException e) {
tmpValue = null;
e.printStackTrace();
}
return urlSafe;
value = tmpValue;
}

public String getName() {
return name;
}

public String getVersion() {
return version;
}

//Visible for testing
String getLibraryVersion() {
return libraryVersion;
}

//Visible for testing
Map<String, String> getEnvironment() {
return env;
}

public String getValue() {
return value;
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/auth0/net/TelemetryInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public Response intercept(Chain chain) throws IOException {
return chain.proceed(request);
}

public void setTelemetry(Telemetry telemetry) {
this.telemetry = telemetry;
}

public Telemetry getTelemetry() {
return this.telemetry;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
Expand Down
39 changes: 33 additions & 6 deletions src/test/java/com/auth0/client/auth/AuthAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import com.auth0.json.auth.CreatedUser;
import com.auth0.json.auth.TokenHolder;
import com.auth0.json.auth.UserInfo;
import com.auth0.net.AuthRequest;
import com.auth0.net.Request;
import com.auth0.net.SignUpRequest;
import com.auth0.net.TelemetryInterceptor;
import com.auth0.net.*;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.Interceptor;
Expand All @@ -20,6 +17,7 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;

import java.io.FileReader;
import java.util.HashMap;
Expand Down Expand Up @@ -106,6 +104,33 @@ public void shouldThrowWhenClientSecretIsNull() throws Exception {
new AuthAPI(DOMAIN, CLIENT_ID, null);
}

@Test
public void shouldUseCustomTelemetry() throws Exception {
AuthAPI api = new AuthAPI(DOMAIN, CLIENT_ID, CLIENT_SECRET);
assertThat(api.getClient().interceptors(), hasItem(isA(TelemetryInterceptor.class)));

Telemetry currentTelemetry = null;
for (Interceptor i : api.getClient().interceptors()) {
if (i instanceof TelemetryInterceptor) {
TelemetryInterceptor interceptor = (TelemetryInterceptor) i;
currentTelemetry = interceptor.getTelemetry();
}
}
assertThat(currentTelemetry, is(notNullValue()));

Telemetry newTelemetry = Mockito.mock(Telemetry.class);
api.setTelemetry(newTelemetry);

Telemetry updatedTelemetry = null;
for (Interceptor i : api.getClient().interceptors()) {
if (i instanceof TelemetryInterceptor) {
TelemetryInterceptor interceptor = (TelemetryInterceptor) i;
updatedTelemetry = interceptor.getTelemetry();
}
}
assertThat(updatedTelemetry, is(newTelemetry));
}

@Test
public void shouldAddAndEnableTelemetryInterceptor() throws Exception {
AuthAPI api = new AuthAPI(DOMAIN, CLIENT_ID, CLIENT_SECRET);
Expand Down Expand Up @@ -389,7 +414,8 @@ public void shouldThrowOnUsernameSignUpWithNullConnection() throws Exception {
public void shouldHaveNotStrongPasswordWithDetailedDescription() throws Exception {
ObjectMapper mapper = new ObjectMapper();
FileReader fr = new FileReader(PASSWORD_STRENGTH_ERROR_RESPONSE_NONE);
Map<String, Object> mapPayload = mapper.readValue(fr, new TypeReference<Map<String, Object>>() {});
Map<String, Object> mapPayload = mapper.readValue(fr, new TypeReference<Map<String, Object>>() {
});
APIException ex = new APIException(mapPayload, 400);
assertThat(ex.getError(), is("invalid_password"));
String expectedDescription = "At least 10 characters in length; Contain at least 3 of the following 4 types of characters: lower case letters (a-z), upper case letters (A-Z), numbers (i.e. 0-9), special characters (e.g. !@#$%^&*); Should contain: lower case letters (a-z), upper case letters (A-Z), numbers (i.e. 0-9), special characters (e.g. !@#$%^&*); No more than 2 identical characters in a row (e.g., \"aaa\" not allowed)";
Expand All @@ -400,7 +426,8 @@ public void shouldHaveNotStrongPasswordWithDetailedDescription() throws Exceptio
public void shouldHaveNotStrongPasswordWithShortDetailedDescription() throws Exception {
ObjectMapper mapper = new ObjectMapper();
FileReader fr = new FileReader(PASSWORD_STRENGTH_ERROR_RESPONSE_SOME);
Map<String, Object> mapPayload = mapper.readValue(fr, new TypeReference<Map<String, Object>>() {});
Map<String, Object> mapPayload = mapper.readValue(fr, new TypeReference<Map<String, Object>>() {
});
APIException ex = new APIException(mapPayload, 400);
assertThat(ex.getError(), is("invalid_password"));
String expectedDescription = "Should contain: lower case letters (a-z), upper case letters (A-Z), numbers (i.e. 0-9), special characters (e.g. !@#$%^&*)";
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/com/auth0/client/mgmt/ManagementAPITest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.auth0.client.mgmt;

import com.auth0.client.MockServer;
import com.auth0.net.Telemetry;
import com.auth0.net.TelemetryInterceptor;
import okhttp3.Interceptor;
import okhttp3.logging.HttpLoggingInterceptor;
Expand All @@ -9,6 +10,7 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;

import static com.auth0.client.UrlMatcher.isUrl;
import static okhttp3.logging.HttpLoggingInterceptor.Level;
Expand Down Expand Up @@ -144,6 +146,33 @@ public void shouldAddAndEnableTelemetryInterceptor() throws Exception {
}
}

@Test
public void shouldUseCustomTelemetry() throws Exception {
ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN);
assertThat(api.getClient().interceptors(), hasItem(isA(TelemetryInterceptor.class)));

Telemetry currentTelemetry = null;
for (Interceptor i : api.getClient().interceptors()) {
if (i instanceof TelemetryInterceptor) {
TelemetryInterceptor interceptor = (TelemetryInterceptor) i;
currentTelemetry = interceptor.getTelemetry();
}
}
assertThat(currentTelemetry, is(notNullValue()));

Telemetry newTelemetry = Mockito.mock(Telemetry.class);
api.setTelemetry(newTelemetry);

Telemetry updatedTelemetry = null;
for (Interceptor i : api.getClient().interceptors()) {
if (i instanceof TelemetryInterceptor) {
TelemetryInterceptor interceptor = (TelemetryInterceptor) i;
updatedTelemetry = interceptor.getTelemetry();
}
}
assertThat(updatedTelemetry, is(newTelemetry));
}

@Test
public void shouldDisableTelemetryInterceptor() throws Exception {
ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN);
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/com/auth0/net/TelemetryInterceptorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,39 @@ public void shouldEnable() throws Exception {
assertThat(interceptor.isEnabled(), is(true));
}

@Test
public void shouldGetTelemetry() {
TelemetryInterceptor interceptor = new TelemetryInterceptor(telemetry);
assertThat(interceptor.getTelemetry(), is(telemetry));
}

@Test
public void shouldAllowToModifyTelemetryOnceSet() throws Exception {
TelemetryInterceptor interceptor = new TelemetryInterceptor(telemetry);
Telemetry updatedTelemetry = mock(Telemetry.class);
when(updatedTelemetry.getValue()).thenReturn("UpdatedClientInfo");
interceptor.setTelemetry(updatedTelemetry);

OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.build();

MockWebServer server = new MockWebServer();
server.start();
server.enqueue(new MockResponse());

Request request = new Request.Builder()
.get()
.url(server.url("/"))
.build();
client.newCall(request).execute();

RecordedRequest finalRequest = server.takeRequest();
assertThat(finalRequest.getHeader("Auth0-Client"), is("UpdatedClientInfo"));

server.shutdown();
}

@Test
public void shouldAddTelemetryHeaderIfEnabled() throws Exception {
TelemetryInterceptor interceptor = new TelemetryInterceptor(telemetry);
Expand Down
Loading

0 comments on commit 4648f6c

Please sign in to comment.