Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/main/kotlin/com/ecwid/apiclient/v3/ApiClientHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.ecwid.apiclient.v3.jsontransformer.JsonTransformerProvider
import com.ecwid.apiclient.v3.jsontransformer.PolymorphicType
import com.ecwid.apiclient.v3.util.buildEndpointPath
import com.ecwid.apiclient.v3.util.maskApiToken
import com.ecwid.apiclient.v3.util.maskAppSecretKey
import java.net.URI
import java.util.*
import java.util.logging.Level
Expand Down Expand Up @@ -175,11 +176,10 @@ class ApiClientHelper private constructor(
internal fun logRequestIfNeeded(requestId: String, httpRequest: HttpRequest, httpBody: HttpBody) {
if (!loggingSettings.logRequest) return

val params = if (loggingSettings.maskRequestApiToken) {
httpRequest.params.withMaskedApiTokenParam()
} else {
httpRequest.params
}
val params = httpRequest.params.withMaskedApiParams(
loggingSettings.maskRequestApiToken,
loggingSettings.maskRequestApiSecretKey
)

logEntry(
prefix = "Request",
Expand Down Expand Up @@ -378,12 +378,20 @@ internal fun Map<String, String>.withAppCredentialsParams(appCredentials: ApiApp
.toMap()
}

private fun Map<String, String>.withMaskedApiTokenParam(): Map<String, String> {
private fun Map<String, String>.withMaskedApiParams(maskRequestApiToken: Boolean, maskRequestApiSecretKey: Boolean): Map<String, String> {
return toMutableMap()
.apply {
val apiTokenParam = get(API_TOKEN_PARAM_NAME)
if (apiTokenParam != null) {
put(API_TOKEN_PARAM_NAME, maskApiToken(apiTokenParam))
if (maskRequestApiToken) {
val apiTokenParam = get(API_TOKEN_PARAM_NAME)
if (apiTokenParam != null) {
put(API_TOKEN_PARAM_NAME, maskApiToken(apiTokenParam))
}
}
if (maskRequestApiSecretKey) {
val apiSecretKeyParam = get(APP_CLIENT_SECRET_PARAM_NAME)
if (apiSecretKeyParam != null) {
put(APP_CLIENT_SECRET_PARAM_NAME, maskAppSecretKey(apiSecretKeyParam))
}
}
}
.toMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ data class LoggingSettings(
val logRequest: Boolean = true,
val logRequestBody: Boolean = false,
val maskRequestApiToken: Boolean = true, /* DISABLE FOR DEBUG PURPOSES ONLY! */
val maskRequestApiSecretKey: Boolean = true, /* DISABLE FOR DEBUG PURPOSES ONLY! */

val logResponse: Boolean = true,
val logSuccessfulResponseBody: Boolean = false,
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/com/ecwid/apiclient/v3/util/MaskUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ internal fun maskApiToken(apiToken: String): String {
}
return ""
}

internal fun maskAppSecretKey(secretKey: String): String {
return if (secretKey.length > 2 * TOKEN_UNMASKED_LENGTH) {
secretKey.take(TOKEN_UNMASKED_LENGTH) + "***" + secretKey.takeLast(minOf(TOKEN_UNMASKED_LENGTH, secretKey.length - 2 * TOKEN_UNMASKED_LENGTH))
} else {
"***"
}
}
9 changes: 9 additions & 0 deletions src/test/kotlin/com/ecwid/apiclient/v3/MaskUtilsUnitTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ecwid.apiclient.v3

import com.ecwid.apiclient.v3.util.maskApiToken
import com.ecwid.apiclient.v3.util.maskAppSecretKey
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

Expand Down Expand Up @@ -32,4 +33,12 @@ class MaskUtilsUnitTest {
assertEquals("01234", maskApiToken("01234"))
assertEquals("012", maskApiToken("012"))
}

@Test
fun testAppSecretKey() {
assertEquals("***", maskAppSecretKey("appSec"))
assertEquals("app***Key", maskAppSecretKey("appSecretKey"))
assertEquals("app***re", maskAppSecretKey("appSecre"))
assertEquals("app***r", maskAppSecretKey("appSecr"))
}
}