Skip to content

Commit 58b5211

Browse files
committed
add Telemetry. Allow to disable interceptors.
1 parent 70ef798 commit 58b5211

9 files changed

Lines changed: 426 additions & 4 deletions

File tree

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ dependencies {
3131
compile 'com.squareup.okhttp3:okhttp:3.5.0'
3232
compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'
3333
compile 'com.fasterxml.jackson.core:jackson-databind:2.8.5'
34+
compile 'commons-codec:commons-codec:1.10'
3435

3536
testCompile 'org.mockito:mockito-core:2.5.4'
3637
testCompile 'com.squareup.okhttp3:mockwebserver:3.5.0'

src/main/java/com/auth0/client/auth/AuthAPI.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class AuthAPI {
2828
private final String clientId;
2929
private final String clientSecret;
3030
private final String baseUrl;
31+
private final TelemetryInterceptor telemetry;
32+
private final HttpLoggingInterceptor logging;
3133

3234
public AuthAPI(String domain, String clientId, String clientSecret) {
3335
Asserts.assertNotNull(domain, "domain");
@@ -41,13 +43,35 @@ public AuthAPI(String domain, String clientId, String clientSecret) {
4143
this.clientId = clientId;
4244
this.clientSecret = clientSecret;
4345

44-
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
46+
telemetry = new TelemetryInterceptor();
47+
logging = new HttpLoggingInterceptor();
4548
logging.setLevel(Level.NONE);
4649
client = new OkHttpClient.Builder()
4750
.addInterceptor(logging)
51+
.addInterceptor(telemetry)
4852
.build();
4953
}
5054

55+
/**
56+
* Avoid sending Telemetry data in every request to the Auth0 servers.
57+
*/
58+
public void doNotSendTelemetry() {
59+
telemetry.setEnabled(false);
60+
}
61+
62+
/**
63+
* Whether to enable or not the current Http Logger for every Request, Response and other sensitive information.
64+
*/
65+
public void setLoggingEnabled(boolean enabled) {
66+
logging.setLevel(enabled ? Level.BODY : Level.NONE);
67+
}
68+
69+
//Visible for Testing
70+
OkHttpClient getClient() {
71+
return client;
72+
}
73+
74+
//Visible for Testing
5175
String getBaseUrl() {
5276
return baseUrl;
5377
}

src/main/java/com/auth0/client/mgmt/MgmtAPI.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package com.auth0.client.mgmt;
22

33
import com.auth0.Asserts;
4+
import com.auth0.net.TelemetryInterceptor;
45
import okhttp3.HttpUrl;
56
import okhttp3.OkHttpClient;
67
import okhttp3.logging.HttpLoggingInterceptor;
8+
import okhttp3.logging.HttpLoggingInterceptor.Level;
79

810
public class MgmtAPI {
911

1012
private final String baseUrl;
1113
private final String apiToken;
1214
private final OkHttpClient client;
15+
private final TelemetryInterceptor telemetry;
16+
private final HttpLoggingInterceptor logging;
1317

1418
public MgmtAPI(String domain, String apiToken) {
1519
Asserts.assertNotNull(domain, "domain");
@@ -21,13 +25,35 @@ public MgmtAPI(String domain, String apiToken) {
2125
}
2226
this.apiToken = apiToken;
2327

24-
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
25-
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
28+
telemetry = new TelemetryInterceptor();
29+
logging = new HttpLoggingInterceptor();
30+
logging.setLevel(Level.NONE);
2631
client = new OkHttpClient.Builder()
2732
.addInterceptor(logging)
33+
.addInterceptor(telemetry)
2834
.build();
2935
}
3036

37+
/**
38+
* Avoid sending Telemetry data in every request to the Auth0 servers.
39+
*/
40+
public void doNotSendTelemetry() {
41+
telemetry.setEnabled(false);
42+
}
43+
44+
/**
45+
* Whether to enable or not the current Http Logger for every Request, Response and other sensitive information.
46+
*/
47+
public void setLoggingEnabled(boolean enabled) {
48+
logging.setLevel(enabled ? Level.BODY : Level.NONE);
49+
}
50+
51+
//Visible for testing
52+
OkHttpClient getClient() {
53+
return client;
54+
}
55+
56+
//Visible for testing
3157
String getBaseUrl() {
3258
return baseUrl;
3359
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.auth0.net;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import org.apache.commons.codec.binary.Base64;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
public class Telemetry {
11+
public static final String HEADER_NAME = "Auth0-Client";
12+
13+
private static final String NAME_KEY = "name";
14+
private static final String VERSION_KEY = "version";
15+
16+
private final String name;
17+
private final String version;
18+
19+
public Telemetry(String name, String version) {
20+
this.name = name;
21+
this.version = version;
22+
}
23+
24+
public String getName() {
25+
return name;
26+
}
27+
28+
public String getVersion() {
29+
return version;
30+
}
31+
32+
public String getValue() {
33+
Map<String, String> values = new HashMap<>();
34+
if (name != null) {
35+
values.put(NAME_KEY, name);
36+
}
37+
if (version != null) {
38+
values.put(VERSION_KEY, version);
39+
}
40+
if (values.isEmpty()) {
41+
return null;
42+
}
43+
String urlSafe = null;
44+
try {
45+
String json = new ObjectMapper().writeValueAsString(values);
46+
urlSafe = Base64.encodeBase64URLSafeString(json.getBytes());
47+
} catch (JsonProcessingException e) {
48+
e.printStackTrace();
49+
}
50+
return urlSafe;
51+
}
52+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.auth0.net;
2+
3+
import okhttp3.Interceptor;
4+
import okhttp3.Response;
5+
6+
import java.io.IOException;
7+
8+
public class TelemetryInterceptor implements Interceptor {
9+
10+
private Telemetry telemetry;
11+
private boolean enabled;
12+
13+
public TelemetryInterceptor() {
14+
this(new Telemetry("auth0-java", "1.0"));
15+
}
16+
17+
TelemetryInterceptor(Telemetry telemetry) {
18+
this.telemetry = telemetry;
19+
this.enabled = true;
20+
}
21+
22+
@Override
23+
public Response intercept(Chain chain) throws IOException {
24+
if (!enabled) {
25+
return chain.proceed(chain.request());
26+
}
27+
28+
okhttp3.Request request = chain.request()
29+
.newBuilder()
30+
.addHeader(Telemetry.HEADER_NAME, telemetry.getValue())
31+
.build();
32+
return chain.proceed(request);
33+
}
34+
35+
public void setEnabled(boolean enabled) {
36+
this.enabled = enabled;
37+
}
38+
39+
public boolean isEnabled() {
40+
return enabled;
41+
}
42+
}

src/test/java/com/auth0/client/auth/AuthAPITest.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
import com.auth0.net.AuthRequest;
77
import com.auth0.net.Request;
88
import com.auth0.net.SignUpRequest;
9+
import com.auth0.net.TelemetryInterceptor;
10+
import okhttp3.Interceptor;
11+
import okhttp3.logging.HttpLoggingInterceptor;
12+
import okhttp3.logging.HttpLoggingInterceptor.Level;
913
import okhttp3.mockwebserver.RecordedRequest;
1014
import org.junit.After;
1115
import org.junit.Before;
@@ -93,6 +97,73 @@ public void shouldThrowWhenClientSecretIsNull() throws Exception {
9397
new AuthAPI(DOMAIN, CLIENT_ID, null);
9498
}
9599

100+
@Test
101+
public void shouldAddAndEnableTelemetryInterceptor() throws Exception {
102+
AuthAPI api = new AuthAPI(DOMAIN, CLIENT_ID, CLIENT_SECRET);
103+
assertThat(api.getClient().interceptors(), hasItem(isA(TelemetryInterceptor.class)));
104+
105+
for (Interceptor i : api.getClient().interceptors()) {
106+
if (i instanceof TelemetryInterceptor) {
107+
TelemetryInterceptor telemetry = (TelemetryInterceptor) i;
108+
assertThat(telemetry.isEnabled(), is(true));
109+
}
110+
}
111+
}
112+
113+
@Test
114+
public void shouldDisableTelemetryInterceptor() throws Exception {
115+
AuthAPI api = new AuthAPI(DOMAIN, CLIENT_ID, CLIENT_SECRET);
116+
assertThat(api.getClient().interceptors(), hasItem(isA(TelemetryInterceptor.class)));
117+
api.doNotSendTelemetry();
118+
119+
for (Interceptor i : api.getClient().interceptors()) {
120+
if (i instanceof TelemetryInterceptor) {
121+
TelemetryInterceptor telemetry = (TelemetryInterceptor) i;
122+
assertThat(telemetry.isEnabled(), is(false));
123+
}
124+
}
125+
}
126+
127+
@Test
128+
public void shouldAddAndDisableLoggingInterceptor() throws Exception {
129+
AuthAPI api = new AuthAPI(DOMAIN, CLIENT_ID, CLIENT_SECRET);
130+
assertThat(api.getClient().interceptors(), hasItem(isA(HttpLoggingInterceptor.class)));
131+
132+
for (Interceptor i : api.getClient().interceptors()) {
133+
if (i instanceof HttpLoggingInterceptor) {
134+
HttpLoggingInterceptor logging = (HttpLoggingInterceptor) i;
135+
assertThat(logging.getLevel(), is(Level.NONE));
136+
}
137+
}
138+
}
139+
140+
@Test
141+
public void shouldEnableLoggingInterceptor() throws Exception {
142+
AuthAPI api = new AuthAPI(DOMAIN, CLIENT_ID, CLIENT_SECRET);
143+
assertThat(api.getClient().interceptors(), hasItem(isA(HttpLoggingInterceptor.class)));
144+
api.setLoggingEnabled(true);
145+
146+
for (Interceptor i : api.getClient().interceptors()) {
147+
if (i instanceof HttpLoggingInterceptor) {
148+
HttpLoggingInterceptor logging = (HttpLoggingInterceptor) i;
149+
assertThat(logging.getLevel(), is(Level.BODY));
150+
}
151+
}
152+
}
153+
154+
@Test
155+
public void shouldDisableLoggingInterceptor() throws Exception {
156+
AuthAPI api = new AuthAPI(DOMAIN, CLIENT_ID, CLIENT_SECRET);
157+
assertThat(api.getClient().interceptors(), hasItem(isA(HttpLoggingInterceptor.class)));
158+
api.setLoggingEnabled(false);
159+
160+
for (Interceptor i : api.getClient().interceptors()) {
161+
if (i instanceof HttpLoggingInterceptor) {
162+
HttpLoggingInterceptor logging = (HttpLoggingInterceptor) i;
163+
assertThat(logging.getLevel(), is(Level.NONE));
164+
}
165+
}
166+
}
96167

97168
//Authorize
98169

src/test/java/com/auth0/client/mgmt/MgmtAPITest.java

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package com.auth0.client.mgmt;
22

33
import com.auth0.client.MockServer;
4+
import com.auth0.net.TelemetryInterceptor;
5+
import okhttp3.Interceptor;
6+
import okhttp3.logging.HttpLoggingInterceptor;
47
import org.junit.After;
58
import org.junit.Before;
69
import org.junit.Test;
710
import org.junit.rules.ExpectedException;
811

912
import static com.auth0.client.UrlMatcher.isUrl;
10-
import static org.hamcrest.Matchers.notNullValue;
13+
import static okhttp3.logging.HttpLoggingInterceptor.Level;
14+
import static org.hamcrest.Matchers.*;
1115
import static org.junit.Assert.assertThat;
1216

1317
public class MgmtAPITest {
@@ -68,6 +72,74 @@ public void shouldThrowWhenApiTokenIsNull() throws Exception {
6872
new MgmtAPI(DOMAIN, null);
6973
}
7074

75+
@Test
76+
public void shouldAddAndEnableTelemetryInterceptor() throws Exception {
77+
MgmtAPI api = new MgmtAPI(DOMAIN, API_TOKEN);
78+
assertThat(api.getClient().interceptors(), hasItem(isA(TelemetryInterceptor.class)));
79+
80+
for (Interceptor i : api.getClient().interceptors()) {
81+
if (i instanceof TelemetryInterceptor) {
82+
TelemetryInterceptor telemetry = (TelemetryInterceptor) i;
83+
assertThat(telemetry.isEnabled(), is(true));
84+
}
85+
}
86+
}
87+
88+
@Test
89+
public void shouldDisableTelemetryInterceptor() throws Exception {
90+
MgmtAPI api = new MgmtAPI(DOMAIN, API_TOKEN);
91+
assertThat(api.getClient().interceptors(), hasItem(isA(TelemetryInterceptor.class)));
92+
api.doNotSendTelemetry();
93+
94+
for (Interceptor i : api.getClient().interceptors()) {
95+
if (i instanceof TelemetryInterceptor) {
96+
TelemetryInterceptor telemetry = (TelemetryInterceptor) i;
97+
assertThat(telemetry.isEnabled(), is(false));
98+
}
99+
}
100+
}
101+
102+
@Test
103+
public void shouldAddAndDisableLoggingInterceptor() throws Exception {
104+
MgmtAPI api = new MgmtAPI(DOMAIN, API_TOKEN);
105+
assertThat(api.getClient().interceptors(), hasItem(isA(HttpLoggingInterceptor.class)));
106+
107+
for (Interceptor i : api.getClient().interceptors()) {
108+
if (i instanceof HttpLoggingInterceptor) {
109+
HttpLoggingInterceptor logging = (HttpLoggingInterceptor) i;
110+
assertThat(logging.getLevel(), is(Level.NONE));
111+
}
112+
}
113+
}
114+
115+
@Test
116+
public void shouldEnableLoggingInterceptor() throws Exception {
117+
MgmtAPI api = new MgmtAPI(DOMAIN, API_TOKEN);
118+
assertThat(api.getClient().interceptors(), hasItem(isA(HttpLoggingInterceptor.class)));
119+
api.setLoggingEnabled(true);
120+
121+
for (Interceptor i : api.getClient().interceptors()) {
122+
if (i instanceof HttpLoggingInterceptor) {
123+
HttpLoggingInterceptor logging = (HttpLoggingInterceptor) i;
124+
assertThat(logging.getLevel(), is(Level.BODY));
125+
}
126+
}
127+
}
128+
129+
@Test
130+
public void shouldDisableLoggingInterceptor() throws Exception {
131+
MgmtAPI api = new MgmtAPI(DOMAIN, API_TOKEN);
132+
assertThat(api.getClient().interceptors(), hasItem(isA(HttpLoggingInterceptor.class)));
133+
api.setLoggingEnabled(false);
134+
135+
for (Interceptor i : api.getClient().interceptors()) {
136+
if (i instanceof HttpLoggingInterceptor) {
137+
HttpLoggingInterceptor logging = (HttpLoggingInterceptor) i;
138+
assertThat(logging.getLevel(), is(Level.NONE));
139+
}
140+
}
141+
}
142+
71143
//Entities
72144

73145
@Test

0 commit comments

Comments
 (0)