Skip to content

Commit 33de197

Browse files
committed
revert auth header changes
1 parent 8490569 commit 33de197

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

core/src/main/java/com/segment/analytics/kotlin/core/HTTPClient.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.segment.analytics.kotlin.core
22

33
import com.segment.analytics.kotlin.core.Constants.LIBRARY_VERSION
4+
import com.segment.analytics.kotlin.core.utilities.encodeToBase64
45
import java.io.BufferedReader
56
import java.io.Closeable
67
import java.io.IOException
@@ -12,6 +13,7 @@ import java.net.URL
1213
import java.util.zip.GZIPOutputStream
1314

1415
class HTTPClient(private val writeKey: String) {
16+
internal val authHeader = authorizationHeader(writeKey)
1517

1618
fun settings(cdnHost: String): Connection {
1719
val connection: HttpURLConnection =
@@ -29,6 +31,7 @@ class HTTPClient(private val writeKey: String) {
2931
val connection: HttpURLConnection = openConnection("https://$apiHost/b")
3032
connection.setRequestProperty("Content-Type", "text/plain")
3133
connection.setRequestProperty("Content-Encoding", "gzip")
34+
connection.setRequestProperty("Authorization", authHeader)
3235
connection.doOutput = true
3336
connection.setChunkedStreamingMode(0)
3437
return connection.createPostConnection()
@@ -55,6 +58,11 @@ class HTTPClient(private val writeKey: String) {
5558
connection.doInput = true
5659
return connection
5760
}
61+
62+
private fun authorizationHeader(writeKey: String): String {
63+
val auth = "$writeKey:"
64+
return "Basic ${encodeToBase64(auth)}"
65+
}
5866
}
5967

6068
/**
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
@file:JvmName("Base64Utils")
2+
package com.segment.analytics.kotlin.core.utilities
3+
4+
// Encode string to base64
5+
fun encodeToBase64(str: String) = encodeToBase64(str.toByteArray())
6+
7+
// Encode byte-array to base64, this implementation is not url-safe
8+
fun encodeToBase64(bytes: ByteArray) = buildString {
9+
val wData = ByteArray(3) // working data
10+
var i = 0
11+
while (i < bytes.size) {
12+
val leftover = bytes.size - i
13+
val available = if (leftover >= 3) {
14+
3
15+
} else {
16+
leftover
17+
}
18+
for (j in 0 until available) {
19+
wData[j] = bytes[i++]
20+
}
21+
for (j in 2 downTo available) {
22+
wData[j] = 0 // clear out
23+
}
24+
// Given a 3 byte block (24 bits), encode it to 4 base64 characters
25+
val chunk = ((wData[0].toInt() and 0xFF) shl 16) or
26+
((wData[1].toInt() and 0xFF) shl 8) or
27+
(wData[2].toInt() and 0xFF)
28+
29+
// if we have too little characters in this block, we add padding
30+
val padCount = (wData.size - available) * 8 / 6
31+
32+
// encode to base64
33+
for (index in 3 downTo padCount) { // 4 base64 characters
34+
val char = (chunk shr (6 * index)) and 0x3f // 0b00111111
35+
append(char.base64Val())
36+
}
37+
38+
// add padding if needed
39+
repeat(padCount) { append("=") }
40+
}
41+
}
42+
43+
private const val ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
44+
private fun Int.base64Val(): Char = ALPHABET[this]

core/src/test/kotlin/com/segment/analytics/kotlin/core/HTTPClientTests.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class HTTPClientTests {
5757

5858
@Test
5959
fun `safeGetInputStream properly catches exception`() {
60-
val connection = spyk(URL("https://api.segment.io/v1/batch").openConnection() as HttpURLConnection)
60+
val connection = spyk(URL("https://api.segment.io/v1/b").openConnection() as HttpURLConnection)
6161
every { connection.inputStream } throws IOException()
6262
val errorStream: InputStream? = safeGetInputStream(connection)
6363
assertEquals(connection.errorStream, errorStream)
@@ -79,4 +79,10 @@ class HTTPClientTests {
7979
e.message?.let { assertTrue(it.contains("300")) }
8080
}
8181
}
82+
83+
@Test
84+
fun `authHeader is correctly computed`() {
85+
assertEquals("Basic MXZOZ1Vxd0plQ0htcWdJOVMxc09tOVVIQ3lmWXFiYVE6", httpClient.authHeader)
86+
}
87+
8288
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.segment.analytics.kotlin.core.utilities
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
import org.junit.jupiter.api.TestInstance
6+
7+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
8+
class Base64UtilsTest {
9+
10+
@Test
11+
fun testBase64Encoding() {
12+
assertEquals(encodeToBase64(""), "")
13+
assertEquals(encodeToBase64("f"), "Zg==")
14+
assertEquals(encodeToBase64("fo"), "Zm8=")
15+
assertEquals(encodeToBase64("foo"), "Zm9v")
16+
assertEquals(encodeToBase64("foob"), "Zm9vYg==")
17+
assertEquals(encodeToBase64("fooba"), "Zm9vYmE=")
18+
assertEquals(encodeToBase64("foobar"), "Zm9vYmFy")
19+
}
20+
}

0 commit comments

Comments
 (0)