diff --git a/auth0/src/main/java/com/auth0/android/util/Base64.java b/auth0/src/main/java/com/auth0/android/util/Base64.java index 7a394de81..00f5cbe1d 100755 --- a/auth0/src/main/java/com/auth0/android/util/Base64.java +++ b/auth0/src/main/java/com/auth0/android/util/Base64.java @@ -3,9 +3,15 @@ import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; +/** + * Helper class originally used to generate URL safe Base64 encoded strings with padding. + * + * @deprecated this class is no longer used and will be removed soon. See {@link android.util.Base64} as an alternative. + */ +@Deprecated public class Base64 { - private static final byte[] ENCODE_MAP = new byte[] { + private static final byte[] ENCODE_MAP = new byte[]{ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', diff --git a/auth0/src/main/java/com/auth0/android/util/Telemetry.java b/auth0/src/main/java/com/auth0/android/util/Telemetry.java index 7838bfeef..e1b78f25e 100755 --- a/auth0/src/main/java/com/auth0/android/util/Telemetry.java +++ b/auth0/src/main/java/com/auth0/android/util/Telemetry.java @@ -1,12 +1,16 @@ package com.auth0.android.util; import android.support.annotation.VisibleForTesting; +import android.util.Base64; import com.google.gson.Gson; +import java.nio.charset.Charset; +import java.util.Collections; import java.util.HashMap; import java.util.Map; + public class Telemetry { public static final String HEADER_NAME = "Auth0-Client"; @@ -19,6 +23,7 @@ public class Telemetry { private final String name; private final String version; private final Map env; + private final String value; public Telemetry(String name, String version) { this(name, version, null); @@ -27,11 +32,25 @@ public Telemetry(String name, String version) { public Telemetry(String name, String version, String core) { this.name = name; this.version = version; - this.env = new HashMap<>(); - env.put(ANDROID_KEY, String.valueOf(android.os.Build.VERSION.SDK_INT)); + Map tmpEnv = new HashMap<>(); + tmpEnv.put(ANDROID_KEY, String.valueOf(android.os.Build.VERSION.SDK_INT)); if (core != null) { - env.put(CORE_KEY, core); + tmpEnv.put(CORE_KEY, core); + } + this.env = Collections.unmodifiableMap(tmpEnv); + + Map values = new HashMap<>(); + if (name != null) { + values.put(NAME_KEY, name); } + if (version != null) { + values.put(VERSION_KEY, version); + } + values.put(ENV_KEY, env); + String json = new Gson().toJson(values); + Charset utf8 = Charset.forName("UTF-8"); + byte[] bytes = json.getBytes(utf8); + value = new String(Base64.encode(bytes, Base64.URL_SAFE | Base64.NO_WRAP), utf8); } public String getName() { @@ -52,15 +71,6 @@ Map getEnvironment() { } public String getValue() { - Map values = new HashMap<>(); - if (name != null) { - values.put(NAME_KEY, name); - } - if (version != null) { - values.put(VERSION_KEY, version); - } - values.put(ENV_KEY, env); - String json = new Gson().toJson(values); - return Base64.encodeUrlSafe(json); + return value; } } diff --git a/auth0/src/test/java/com/auth0/android/util/TelemetryTest.java b/auth0/src/test/java/com/auth0/android/util/TelemetryTest.java index eb8637291..fe961bc1e 100755 --- a/auth0/src/test/java/com/auth0/android/util/TelemetryTest.java +++ b/auth0/src/test/java/com/auth0/android/util/TelemetryTest.java @@ -5,7 +5,6 @@ import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; @@ -22,24 +21,12 @@ @RunWith(RobolectricTestRunner.class) public class TelemetryTest { - private Telemetry telemetry; - - @Before - public void setUp() throws Exception { - telemetry = new Telemetry("auth0-java", "1.0.0", "1.2.3"); - } - - @Test - public void shouldReturnBase64() throws Exception { - assertThat(telemetry.getValue(), is(notNullValue())); - } - //Testing Android version only for a few SDKs @Test @Config(sdk = 21) public void shouldAlwaysIncludeAndroidVersionAPI21() throws Exception { - telemetry = new Telemetry(null, null); + Telemetry telemetry = new Telemetry(null, null); assertThat(telemetry.getEnvironment(), is(notNullValue())); assertThat(telemetry.getEnvironment().get("android"), is("21")); } @@ -47,37 +34,40 @@ public void shouldAlwaysIncludeAndroidVersionAPI21() throws Exception { @Test @Config(sdk = 23) public void shouldAlwaysIncludeAndroidVersionAPI23() throws Exception { - telemetry = new Telemetry(null, null); + Telemetry telemetry = new Telemetry(null, null); assertThat(telemetry.getEnvironment(), is(notNullValue())); assertThat(telemetry.getEnvironment().get("android"), is("23")); } @Test public void shouldNotIncludeCoreIfNotProvided() throws Exception { - telemetry = new Telemetry(null, null); + Telemetry telemetry = new Telemetry(null, null); assertThat(telemetry.getEnvironment(), is(notNullValue())); assertThat(telemetry.getEnvironment().containsKey("core"), is(false)); } @Test public void shouldGetName() throws Exception { + Telemetry telemetry = new Telemetry("auth0-java", "1.0.0", "1.2.3"); assertThat(telemetry.getName(), is("auth0-java")); } @Test public void shouldGetVersion() throws Exception { + Telemetry telemetry = new Telemetry("auth0-java", "1.0.0", "1.2.3"); assertThat(telemetry.getVersion(), is("1.0.0")); } @Test public void shouldGetLibraryVersion() throws Exception { + Telemetry telemetry = new Telemetry("auth0-java", "1.0.0", "1.2.3"); assertThat(telemetry.getLibraryVersion(), is("1.2.3")); assertThat(telemetry.getEnvironment().get("core"), is("1.2.3")); } @Test @Config(sdk = 23) - public void shouldGenerateValidCompleteTelemetryValue() throws Exception { + public void shouldGenerateCompleteTelemetryBase64Value() throws Exception { Gson gson = new Gson(); Type mapType = new TypeToken>() { }.getType(); @@ -96,7 +86,7 @@ public void shouldGenerateValidCompleteTelemetryValue() throws Exception { @Test @Config(sdk = 23) - public void shouldGenerateValidBasicTelemetryValue() throws Exception { + public void shouldGenerateBasicTelemetryBase64Value() throws Exception { Gson gson = new Gson(); Type mapType = new TypeToken>() { }.getType(); @@ -115,7 +105,7 @@ public void shouldGenerateValidBasicTelemetryValue() throws Exception { @Test @Config(sdk = 21) - public void shouldGenerateValidRegularTelemetryValue() throws Exception { + public void shouldGenerateRegularTelemetryBase64Value() throws Exception { Gson gson = new Gson(); Type mapType = new TypeToken>() { }.getType();