Skip to content

Commit

Permalink
fix: jwt can be used at initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Feb 15, 2024
1 parent b44d038 commit c40baae
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import org.apache.commons.lang3.RandomStringUtils
import org.assertj.core.api.Assertions.assertThat
import org.awaitility.Awaitility.await
import org.awaitility.Duration
import org.json.JSONObject
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.time.Instant


@RunWith(AndroidJUnit4::class)
class ExtoleSdkTests {

Expand Down Expand Up @@ -414,4 +414,71 @@ class ExtoleSdkTests {
assertThat(emailAfterIdentify).isEqualTo("null")
}

@Test
fun testIdentifyJwtAtInit() {
val jwt = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6IjUzNmQwNWE2LTMzZWUtNDI2NC04ODI2LW" +
"JhZDRjOTAyMWZhZiJ9.eyJpc3MiOiJtb2JpbGUtc2RrLmV4dG9sZS5jb20iLCJhdWQiOlsiZXh0b2xlLmNvbSJ" +
"dLCJlbWFpbCI6InNka3BlcnNvbi1lbWFpbEBtYWlsb3NhdXIuY29tIiwiaWF0IjoxNzA1NTg0Mjg0LCJleHAiO" +
"jI0ODMxODQyODR9.XdB5-j58GcEeKqKkCLd5f_G78CLLJIHCmsfcOpH-n3o"
val extole =
runBlocking {
return@runBlocking Extole.init(
"mobile-monitor.extole.io",
context = context, appName = "mobile-monitor", labels = setOf("business"),
data = mapOf("version" to "1.0"),
jwt = jwt
)
}

val emailBeforeIdentify = runBlocking {
val (ctaZone, _) = extole.fetchZone("mobile_cta_timestamp")
ctaZone?.get("email").toString()
}
assertThat(emailBeforeIdentify).isEqualTo("[email protected]")
}

@Test
fun testIdentifyJwtWithoutEmailAndEmailPassedAsASeparateParameter() {
val jwt = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6IjUzNmQwNWE2LTMzZWUtNDI2NC04ODI2" +
"LWJhZDRjOTAyMWZhZiJ9.eyJpc3MiOiJtb2JpbGUtc2RrLmV4dG9sZS5jb20iLCJhdWQiOlsiZXh0b2xlLmN" +
"vbSJdLCJpYXQiOjE3MDU5MjQwMDksImV4cCI6MjQ4MzUyNDAwOX0.X2GnR6OV9amojSLSzoXeecoujrnMzyY" +
"As5VWzR86U4M"
val extole =
runBlocking {
return@runBlocking Extole.init(
"mobile-monitor.extole.io",
context = context, appName = "mobile-monitor", labels = setOf("business"),
data = mapOf("version" to "1.0"),
email = "[email protected]",
jwt = jwt
)
}

val personEmail = runBlocking {
val (ctaZone, _) = extole.fetchZone("mobile_cta_timestamp")
ctaZone?.get("email").toString()
}
assertThat(personEmail).isEqualTo("[email protected]")
}

@Test
fun testIdentifyJwtAtInitWithoutEmailIsInitializedAsAnonymous() {
val jwt = "invalid_jwt"
val extole =
runBlocking {
return@runBlocking Extole.init(
"mobile-monitor.extole.io",
context = context, appName = "mobile-monitor", labels = setOf("business"),
data = mapOf("version" to "1.0"),
jwt = jwt
)
}

val personEmail = runBlocking {
val (ctaZone, _) = extole.fetchZone("mobile_cta_timestamp")
ctaZone?.get("email").toString()
}
assertThat(personEmail).isEqualTo("null")
}

}
4 changes: 2 additions & 2 deletions mobile-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ android {
defaultConfig {
minSdkVersion 21
targetSdkVersion 34
versionCode 47
versionName "1.0.49"
versionCode 48
versionName "1.0.50"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand Down
6 changes: 4 additions & 2 deletions mobile-sdk/src/main/java/com/extole/android/sdk/Extole.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ interface Extole {
listenToEvents: Boolean = true,
configurationLoader: ((app: App, data: Map<String, Any>) -> List<Operation>)? = null,
additionalProtocolHandlers: List<ProtocolHandler> = emptyList(),
disabledActions: Set<Action.ActionType> = emptySet()
disabledActions: Set<Action.ActionType> = emptySet(),
jwt: String? = null
): Extole {
return withContext(Dispatchers.IO) {
return@withContext ExtoleInternal.init(
Expand All @@ -164,7 +165,8 @@ interface Extole {
listenToEvents,
configurationLoader,
additionalProtocolHandlers,
disabledActions
disabledActions,
jwt
)
}
}
Expand Down
23 changes: 12 additions & 11 deletions mobile-sdk/src/main/java/com/extole/android/sdk/impl/ExtoleImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class ExtoleImpl(
val listenToEvents: Boolean = true,
val additionalProtocolHandlers: List<ProtocolHandler>,
private val configurationLoader: ((app: App, data: Map<String, Any>) -> List<Operation>)? = null,
private val disabledActions: Set<Action.ActionType> = emptySet()
private val disabledActions: Set<Action.ActionType> = emptySet(),
val jwt: String? = null
) : ExtoleInternal {

companion object {
Expand Down Expand Up @@ -71,7 +72,7 @@ class ExtoleImpl(

init {
appData["appName"] = appName
initApiClient(identifier)
initApiClient(identifier, jwt)
if (listenToEvents) {
subscribe()
}
Expand Down Expand Up @@ -240,19 +241,19 @@ class ExtoleImpl(
context.getPersistence().put(key, value)
}

private fun initApiClient(identifier: String? = null) {
private fun initApiClient(identifier: String? = null, jwt: String? = null) {
val androidLogger = ExtoleLogger.builder().build()
androidLogger.debug("Initialized Extole for programDomain=$programDomain")
accessToken = context.getPersistence().get(ACCESS_TOKEN_PREFERENCES_KEY)
tokenApi = AuthorizationEndpoints(programDomain, accessToken, getHeaders())
if (accessToken == null || identifier != null) {
createAccessToken(identifier)
} else {
try {
try {
if (accessToken == null || identifier != null) {
createAccessToken(identifier, jwt)
} else {
tokenApi.getTokenDetails(mapOf(ACCESS_TOKEN to accessToken))
} catch (e: RestException) {
createAccessToken()
}
} catch (e: RestException) {
createAccessToken()
}
extoleServices = ExtoleServicesImpl(this)
val extoleLogger = ExtoleLogger.builder()
Expand All @@ -262,8 +263,8 @@ class ExtoleImpl(
extoleLogger.debug("Access Token initialized: $accessToken")
}

private fun createAccessToken(identifier: String? = null) {
val accessToken = tokenApi.createToken(identifier).entity.getString(ACCESS_TOKEN)
private fun createAccessToken(identifier: String? = null, jwt: String? = null) {
val accessToken = tokenApi.createToken(identifier, jwt).entity.getString(ACCESS_TOKEN)
setAccessToken(accessToken)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ interface ExtoleInternal : Extole {
listenToEvents: Boolean = true,
configurationLoader: ((app: App, data: Map<String, Any>) -> List<Operation>)? = null,
additionalProtocolHandlers: List<ProtocolHandler> = emptyList(),
disabledActions: Set<Action.ActionType> = emptySet()
disabledActions: Set<Action.ActionType> = emptySet(),
jwt: String? = null
): ExtoleInternal {
val applicationContext =
ApplicationContext(context, SharedPreferencesPersistence(context))
Expand All @@ -43,7 +44,8 @@ interface ExtoleInternal : Extole {
listenToEvents,
additionalProtocolHandlers,
configurationLoader,
disabledActions
disabledActions,
jwt
)
return extole
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ class AuthorizationEndpoints(
return endpoints.handleResponse(httpRequest)
}

fun createToken(email: String?): ResponseEntity<JSONObject> {
fun createToken(email: String?, jwt: String?): ResponseEntity<JSONObject> {
val body = JSONObject()
val httpRequest = endpoints.createHttpRequest(baseUrl, METHOD_POST)
email?.let { body.put("email", it) }
jwt?.let { body.put("jwt", it) }
httpRequest.send(body.toString())
return endpoints.handleResponse(httpRequest)
}
Expand Down

0 comments on commit c40baae

Please sign in to comment.