Skip to content

Commit 46607fb

Browse files
authored
Merge pull request #2337 from OneSignal/jwt/resolve_iam_conditions_after_user_create
JWT: Resolve IAM conditions after user create to allow fetching of IAMs
2 parents 6ae37fd + aeb4553 commit 46607fb

File tree

6 files changed

+65
-21
lines changed

6 files changed

+65
-21
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/backend/IUserBackendService.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,9 @@ class CreateUserResponse(
8484
* The subscriptions for the user.
8585
*/
8686
val subscriptions: List<SubscriptionObject>,
87+
/**
88+
* The RYW data returned by the server.
89+
* This is expected for the response to CreateUser only, not UpdateUser.
90+
*/
91+
val rywData: RywData?,
8792
)

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/backend/impl/JSONConverter.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.onesignal.user.internal.backend.impl
22

3+
import com.onesignal.common.consistency.RywData
34
import com.onesignal.common.expandJSONArray
45
import com.onesignal.common.putJSONArray
56
import com.onesignal.common.putMap
@@ -8,6 +9,7 @@ import com.onesignal.common.safeBool
89
import com.onesignal.common.safeDouble
910
import com.onesignal.common.safeInt
1011
import com.onesignal.common.safeJSONObject
12+
import com.onesignal.common.safeLong
1113
import com.onesignal.common.safeString
1214
import com.onesignal.common.toMap
1315
import com.onesignal.user.internal.backend.CreateUserResponse
@@ -55,7 +57,15 @@ object JSONConverter {
5557
return@expandJSONArray null
5658
}
5759

58-
return CreateUserResponse(respIdentities, respProperties, respSubscriptions)
60+
val rywToken = jsonObject.safeString("ryw_token")
61+
val rywDelay = jsonObject.safeLong("ryw_delay")
62+
var rywData: RywData? = null
63+
64+
if (rywToken != null) {
65+
rywData = RywData(rywToken, rywDelay)
66+
}
67+
68+
return CreateUserResponse(respIdentities, respProperties, respSubscriptions, rywData)
5969
}
6070

6171
fun convertToJSON(properties: PropertiesObject): JSONObject {

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/operations/impl/executors/LoginUserOperationExecutor.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import com.onesignal.common.NetworkUtils
77
import com.onesignal.common.OneSignalUtils
88
import com.onesignal.common.RootToolsInternalMethods
99
import com.onesignal.common.TimeUtils
10+
import com.onesignal.common.consistency.IamFetchReadyCondition
11+
import com.onesignal.common.consistency.enums.IamFetchRywTokenKey
12+
import com.onesignal.common.consistency.models.IConsistencyManager
1013
import com.onesignal.common.exceptions.BackendException
1114
import com.onesignal.common.modeling.ModelChangeTags
1215
import com.onesignal.core.internal.application.IApplicationService
@@ -46,6 +49,7 @@ internal class LoginUserOperationExecutor(
4649
private val _subscriptionsModelStore: SubscriptionModelStore,
4750
private val _configModelStore: ConfigModelStore,
4851
private val _languageContext: ILanguageContext,
52+
private val _consistencyManager: IConsistencyManager,
4953
) : IOperationExecutor {
5054
override val operations: List<String>
5155
get() = listOf(LOGIN_USER)
@@ -216,6 +220,13 @@ internal class LoginUserOperationExecutor(
216220
subscriptionModel?.setStringProperty(SubscriptionModel::id.name, backendSubscription.id, ModelChangeTags.HYDRATE)
217221
}
218222

223+
// Process any ryw data returned
224+
if (response.rywData != null) {
225+
_consistencyManager.setRywData(backendOneSignalId, IamFetchRywTokenKey.USER, response.rywData)
226+
} else {
227+
_consistencyManager.resolveConditionsWithID(IamFetchReadyCondition.ID)
228+
}
229+
219230
val wasPossiblyAnUpsert = identities.isNotEmpty()
220231
val followUpOperations =
221232
if (wasPossiblyAnUpsert) {

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/user/internal/operations/LoginUserOperationExecutorTests.kt

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.onesignal.user.internal.operations
22

33
import br.com.colman.kotest.android.extensions.robolectric.RobolectricTest
4+
import com.onesignal.common.consistency.RywData
5+
import com.onesignal.common.consistency.models.IConsistencyManager
46
import com.onesignal.common.exceptions.BackendException
57
import com.onesignal.core.internal.operations.ExecutionResponse
68
import com.onesignal.core.internal.operations.ExecutionResult
@@ -25,10 +27,13 @@ import io.kotest.core.spec.style.FunSpec
2527
import io.kotest.matchers.collections.shouldBeOneOf
2628
import io.kotest.matchers.shouldBe
2729
import io.kotest.matchers.types.shouldBeInstanceOf
30+
import io.mockk.clearMocks
2831
import io.mockk.coEvery
2932
import io.mockk.coVerify
3033
import io.mockk.every
34+
import io.mockk.just
3135
import io.mockk.mockk
36+
import io.mockk.runs
3237

3338
@RobolectricTest
3439
class LoginUserOperationExecutorTests : FunSpec({
@@ -39,6 +44,8 @@ class LoginUserOperationExecutorTests : FunSpec({
3944
val localSubscriptionId2 = "local-subscriptionId2"
4045
val remoteSubscriptionId1 = "remote-subscriptionId1"
4146
val remoteSubscriptionId2 = "remote-subscriptionId2"
47+
val rywData = RywData("1", 500L)
48+
val mockConsistencyManager = mockk<IConsistencyManager>()
4249
val createSubscriptionOperation =
4350
CreateSubscriptionOperation(
4451
appId,
@@ -50,6 +57,11 @@ class LoginUserOperationExecutorTests : FunSpec({
5057
SubscriptionStatus.SUBSCRIBED,
5158
)
5259

60+
beforeTest {
61+
clearMocks(mockConsistencyManager)
62+
coEvery { mockConsistencyManager.setRywData(any(), any(), any()) } just runs
63+
}
64+
5365
test("login anonymous user successfully creates user") {
5466
// Given
5567
val mockUserBackendService = mockk<IUserBackendService>()
@@ -58,6 +70,7 @@ class LoginUserOperationExecutorTests : FunSpec({
5870
mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId),
5971
PropertiesObject(),
6072
listOf(),
73+
rywData,
6174
)
6275
// Given
6376
val mockIdentityOperationExecutor = mockk<IdentityOperationExecutor>()
@@ -76,6 +89,7 @@ class LoginUserOperationExecutorTests : FunSpec({
7689
mockSubscriptionsModelStore,
7790
MockHelper.configModelStore(),
7891
MockHelper.languageContext(),
92+
mockConsistencyManager,
7993
)
8094
val operations =
8195
listOf<Operation>(
@@ -120,6 +134,7 @@ class LoginUserOperationExecutorTests : FunSpec({
120134
mockSubscriptionsModelStore,
121135
MockHelper.configModelStore(),
122136
MockHelper.languageContext(),
137+
mockConsistencyManager,
123138
)
124139
val operations =
125140
listOf<Operation>(
@@ -148,7 +163,7 @@ class LoginUserOperationExecutorTests : FunSpec({
148163
val mockSubscriptionsModelStore = mockk<SubscriptionModelStore>()
149164

150165
val loginUserOperationExecutor =
151-
LoginUserOperationExecutor(mockIdentityOperationExecutor, AndroidMockHelper.applicationService(), MockHelper.deviceService(), mockUserBackendService, mockIdentityModelStore, mockPropertiesModelStore, mockSubscriptionsModelStore, MockHelper.configModelStore(), MockHelper.languageContext())
166+
LoginUserOperationExecutor(mockIdentityOperationExecutor, AndroidMockHelper.applicationService(), MockHelper.deviceService(), mockUserBackendService, mockIdentityModelStore, mockPropertiesModelStore, mockSubscriptionsModelStore, MockHelper.configModelStore(), MockHelper.languageContext(), mockConsistencyManager)
152167
val operations =
153168
listOf<Operation>(
154169
LoginUserOperation(appId, localOneSignalId, null, null),
@@ -167,7 +182,7 @@ class LoginUserOperationExecutorTests : FunSpec({
167182
// Given
168183
val mockUserBackendService = mockk<IUserBackendService>()
169184
coEvery { mockUserBackendService.createUser(any(), any(), any(), any()) } returns
170-
CreateUserResponse(mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId), PropertiesObject(), listOf())
185+
CreateUserResponse(mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId), PropertiesObject(), listOf(), rywData)
171186

172187
val mockIdentityOperationExecutor = mockk<IdentityOperationExecutor>()
173188

@@ -176,7 +191,7 @@ class LoginUserOperationExecutorTests : FunSpec({
176191
val mockSubscriptionsModelStore = mockk<SubscriptionModelStore>()
177192

178193
val loginUserOperationExecutor =
179-
LoginUserOperationExecutor(mockIdentityOperationExecutor, MockHelper.applicationService(), MockHelper.deviceService(), mockUserBackendService, mockIdentityModelStore, mockPropertiesModelStore, mockSubscriptionsModelStore, MockHelper.configModelStore(), MockHelper.languageContext())
194+
LoginUserOperationExecutor(mockIdentityOperationExecutor, MockHelper.applicationService(), MockHelper.deviceService(), mockUserBackendService, mockIdentityModelStore, mockPropertiesModelStore, mockSubscriptionsModelStore, MockHelper.configModelStore(), MockHelper.languageContext(), mockConsistencyManager)
180195
val operations = listOf<Operation>(LoginUserOperation(appId, localOneSignalId, "externalId", null))
181196

182197
// When
@@ -195,7 +210,7 @@ class LoginUserOperationExecutorTests : FunSpec({
195210
// Given
196211
val mockUserBackendService = mockk<IUserBackendService>()
197212
coEvery { mockUserBackendService.createUser(any(), any(), any(), any()) } returns
198-
CreateUserResponse(mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId), PropertiesObject(), listOf())
213+
CreateUserResponse(mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId), PropertiesObject(), listOf(), rywData)
199214

200215
val mockIdentityOperationExecutor = mockk<IdentityOperationExecutor>()
201216

@@ -214,6 +229,7 @@ class LoginUserOperationExecutorTests : FunSpec({
214229
mockSubscriptionsModelStore,
215230
MockHelper.configModelStore(),
216231
MockHelper.languageContext(),
232+
mockConsistencyManager,
217233
)
218234
val operations = listOf<Operation>(LoginUserOperation(appId, localOneSignalId, "externalId", null))
219235

@@ -242,7 +258,7 @@ class LoginUserOperationExecutorTests : FunSpec({
242258
val mockSubscriptionsModelStore = mockk<SubscriptionModelStore>()
243259

244260
val loginUserOperationExecutor =
245-
LoginUserOperationExecutor(mockIdentityOperationExecutor, MockHelper.applicationService(), MockHelper.deviceService(), mockUserBackendService, mockIdentityModelStore, mockPropertiesModelStore, mockSubscriptionsModelStore, MockHelper.configModelStore(), MockHelper.languageContext())
261+
LoginUserOperationExecutor(mockIdentityOperationExecutor, MockHelper.applicationService(), MockHelper.deviceService(), mockUserBackendService, mockIdentityModelStore, mockPropertiesModelStore, mockSubscriptionsModelStore, MockHelper.configModelStore(), MockHelper.languageContext(), mockConsistencyManager)
246262
val operations = listOf<Operation>(LoginUserOperation(appId, localOneSignalId, "externalId", "existingOneSignalId"))
247263

248264
// When
@@ -268,7 +284,7 @@ class LoginUserOperationExecutorTests : FunSpec({
268284
// Given
269285
val mockUserBackendService = mockk<IUserBackendService>()
270286
coEvery { mockUserBackendService.createUser(any(), any(), any(), any()) } returns
271-
CreateUserResponse(mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId), PropertiesObject(), listOf())
287+
CreateUserResponse(mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId), PropertiesObject(), listOf(), rywData)
272288

273289
val mockIdentityOperationExecutor = mockk<IdentityOperationExecutor>()
274290
coEvery { mockIdentityOperationExecutor.execute(any()) } returns ExecutionResponse(ExecutionResult.FAIL_RETRY)
@@ -278,7 +294,7 @@ class LoginUserOperationExecutorTests : FunSpec({
278294
val mockSubscriptionsModelStore = mockk<SubscriptionModelStore>()
279295

280296
val loginUserOperationExecutor =
281-
LoginUserOperationExecutor(mockIdentityOperationExecutor, MockHelper.applicationService(), MockHelper.deviceService(), mockUserBackendService, mockIdentityModelStore, mockPropertiesModelStore, mockSubscriptionsModelStore, MockHelper.configModelStore(), MockHelper.languageContext())
297+
LoginUserOperationExecutor(mockIdentityOperationExecutor, MockHelper.applicationService(), MockHelper.deviceService(), mockUserBackendService, mockIdentityModelStore, mockPropertiesModelStore, mockSubscriptionsModelStore, MockHelper.configModelStore(), MockHelper.languageContext(), mockConsistencyManager)
282298
val operations = listOf<Operation>(LoginUserOperation(appId, localOneSignalId, "externalId", "existingOneSignalId"))
283299

284300
// When
@@ -304,7 +320,7 @@ class LoginUserOperationExecutorTests : FunSpec({
304320
// Given
305321
val mockUserBackendService = mockk<IUserBackendService>()
306322
coEvery { mockUserBackendService.createUser(any(), any(), any(), any()) } returns
307-
CreateUserResponse(mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId), PropertiesObject(), listOf())
323+
CreateUserResponse(mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId), PropertiesObject(), listOf(), rywData)
308324

309325
val mockIdentityOperationExecutor = mockk<IdentityOperationExecutor>()
310326
coEvery { mockIdentityOperationExecutor.execute(any()) } returns ExecutionResponse(ExecutionResult.FAIL_NORETRY)
@@ -314,7 +330,7 @@ class LoginUserOperationExecutorTests : FunSpec({
314330
val mockSubscriptionsModelStore = mockk<SubscriptionModelStore>()
315331

316332
val loginUserOperationExecutor =
317-
LoginUserOperationExecutor(mockIdentityOperationExecutor, MockHelper.applicationService(), MockHelper.deviceService(), mockUserBackendService, mockIdentityModelStore, mockPropertiesModelStore, mockSubscriptionsModelStore, MockHelper.configModelStore(), MockHelper.languageContext())
333+
LoginUserOperationExecutor(mockIdentityOperationExecutor, MockHelper.applicationService(), MockHelper.deviceService(), mockUserBackendService, mockIdentityModelStore, mockPropertiesModelStore, mockSubscriptionsModelStore, MockHelper.configModelStore(), MockHelper.languageContext(), mockConsistencyManager)
318334
val operations = listOf<Operation>(LoginUserOperation(appId, localOneSignalId, "externalId", "existingOneSignalId"))
319335

320336
// When
@@ -352,7 +368,7 @@ class LoginUserOperationExecutorTests : FunSpec({
352368
val mockSubscriptionsModelStore = mockk<SubscriptionModelStore>()
353369

354370
val loginUserOperationExecutor =
355-
LoginUserOperationExecutor(mockIdentityOperationExecutor, MockHelper.applicationService(), MockHelper.deviceService(), mockUserBackendService, mockIdentityModelStore, mockPropertiesModelStore, mockSubscriptionsModelStore, MockHelper.configModelStore(), MockHelper.languageContext())
371+
LoginUserOperationExecutor(mockIdentityOperationExecutor, MockHelper.applicationService(), MockHelper.deviceService(), mockUserBackendService, mockIdentityModelStore, mockPropertiesModelStore, mockSubscriptionsModelStore, MockHelper.configModelStore(), MockHelper.languageContext(), mockConsistencyManager)
356372
val operations = listOf<Operation>(LoginUserOperation(appId, localOneSignalId, "externalId", "existingOneSignalId"))
357373

358374
// When
@@ -385,6 +401,7 @@ class LoginUserOperationExecutorTests : FunSpec({
385401
mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId),
386402
PropertiesObject(),
387403
listOf(),
404+
rywData,
388405
)
389406
// Given
390407
val mockIdentityOperationExecutor = mockk<IdentityOperationExecutor>()
@@ -403,6 +420,7 @@ class LoginUserOperationExecutorTests : FunSpec({
403420
mockSubscriptionsModelStore,
404421
MockHelper.configModelStore(),
405422
MockHelper.languageContext(),
423+
mockConsistencyManager,
406424
)
407425
val operations =
408426
listOf<Operation>(
@@ -471,6 +489,7 @@ class LoginUserOperationExecutorTests : FunSpec({
471489
mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId),
472490
PropertiesObject(),
473491
listOf(SubscriptionObject(remoteSubscriptionId1, SubscriptionObjectType.ANDROID_PUSH), SubscriptionObject(remoteSubscriptionId2, SubscriptionObjectType.EMAIL)),
492+
rywData,
474493
)
475494
// Given
476495
val mockIdentityOperationExecutor = mockk<IdentityOperationExecutor>()
@@ -504,6 +523,7 @@ class LoginUserOperationExecutorTests : FunSpec({
504523
mockSubscriptionsModelStore,
505524
MockHelper.configModelStore(),
506525
MockHelper.languageContext(),
526+
mockConsistencyManager,
507527
)
508528
val operations =
509529
listOf<Operation>(
@@ -557,6 +577,7 @@ class LoginUserOperationExecutorTests : FunSpec({
557577
mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId),
558578
PropertiesObject(),
559579
listOf(SubscriptionObject(remoteSubscriptionId1, SubscriptionObjectType.ANDROID_PUSH), SubscriptionObject(remoteSubscriptionId2, SubscriptionObjectType.EMAIL)),
580+
rywData,
560581
)
561582
// Given
562583
val mockIdentityOperationExecutor = mockk<IdentityOperationExecutor>()
@@ -590,6 +611,7 @@ class LoginUserOperationExecutorTests : FunSpec({
590611
mockSubscriptionsModelStore,
591612
MockHelper.configModelStore(),
592613
MockHelper.languageContext(),
614+
mockConsistencyManager,
593615
)
594616
val operations =
595617
listOf<Operation>(
@@ -643,6 +665,7 @@ class LoginUserOperationExecutorTests : FunSpec({
643665
mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId),
644666
PropertiesObject(),
645667
listOf(SubscriptionObject(remoteSubscriptionId1, SubscriptionObjectType.ANDROID_PUSH), SubscriptionObject(remoteSubscriptionId2, SubscriptionObjectType.EMAIL)),
668+
rywData,
646669
)
647670
// Given
648671
val mockIdentityOperationExecutor = mockk<IdentityOperationExecutor>()
@@ -662,6 +685,7 @@ class LoginUserOperationExecutorTests : FunSpec({
662685
mockSubscriptionsModelStore,
663686
MockHelper.configModelStore(),
664687
MockHelper.languageContext(),
688+
mockConsistencyManager,
665689
)
666690
val operations =
667691
listOf<Operation>(
@@ -706,7 +730,7 @@ class LoginUserOperationExecutorTests : FunSpec({
706730
// Given
707731
val mockUserBackendService = mockk<IUserBackendService>()
708732
coEvery { mockUserBackendService.createUser(any(), any(), any(), any()) } returns
709-
CreateUserResponse(mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId), PropertiesObject(), listOf())
733+
CreateUserResponse(mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId), PropertiesObject(), listOf(), rywData)
710734

711735
val mockIdentityOperationExecutor = mockk<IdentityOperationExecutor>()
712736

@@ -725,6 +749,7 @@ class LoginUserOperationExecutorTests : FunSpec({
725749
mockSubscriptionsModelStore,
726750
MockHelper.configModelStore(),
727751
MockHelper.languageContext(),
752+
mockConsistencyManager,
728753
)
729754
// anonymous Login request
730755
val operations = listOf<Operation>(LoginUserOperation(appId, localOneSignalId, null, null))

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/user/internal/operations/RefreshUserOperationExecutorTests.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class RefreshUserOperationExecutorTests : FunSpec({
4949
SubscriptionObject(remoteSubscriptionId1, SubscriptionObjectType.ANDROID_PUSH, enabled = true, token = "pushToken2"),
5050
SubscriptionObject(remoteSubscriptionId2, SubscriptionObjectType.EMAIL, token = "name@company.com"),
5151
),
52+
null,
5253
)
5354

5455
// Given
@@ -142,6 +143,7 @@ class RefreshUserOperationExecutorTests : FunSpec({
142143
mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId),
143144
PropertiesObject(),
144145
listOf(),
146+
null,
145147
)
146148

147149
// Given

OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/InAppMessagesManager.kt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,6 @@ internal class InAppMessagesManager(
196196
for (redisplayInAppMessage in redisplayedInAppMessages) {
197197
redisplayInAppMessage.isDisplayedInSession = false
198198
}
199-
200-
// attempt to fetch messages from the backend (if we have the pre-requisite data already)
201-
val onesignalId = _userManager.onesignalId
202-
val updateConditionDeferred =
203-
_consistencyManager.getRywDataFromAwaitableCondition(IamFetchReadyCondition(onesignalId))
204-
val rywToken = updateConditionDeferred.await()
205-
if (rywToken != null) {
206-
fetchMessages(rywToken)
207-
}
208199
}
209200
}
210201

0 commit comments

Comments
 (0)