Skip to content

Commit

Permalink
Implement jwt to backend service and operation executor for subscript…
Browse files Browse the repository at this point in the history
…ion specific operation

Go through all backend services and enable them with jwt:
  1. Find an OperationExecutor (ex. SubscriptionOperationExecutor.kt)
  2. Look for any BackendService that this OperationExecutor contains (ex. _subscriptionBackendService)
  3. Go to that BackendService, look through all its methods. Add jwt: String? = null if this method is one of the authorized operation in the JWT dev documentation. (ex. createSubscription() in this case), make sure to modify both ISubscriptionBackendService.kt and SubscriptionBackendService.kt)
  4. In implementation of the BackendService (ex. SubscriptionBackendService.kt), look for all http client calls such as _httpClient.post / get / patch and etc, and add jwt as the last parameter.
  5. Go back to the OoperationExecutor (SubscriptionOperationExecutor.kt), if there is no _identityModelStore in its property, add private val _identityModelStore: IdentityModelStore as its property.
  6. Update parameters in all backend service calls with _identityModelStore.model.jwtToken in the end
  • Loading branch information
jinliu9508 committed Mar 15, 2024
1 parent 7cfa79a commit 268d1a7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
if (currentIdentityExternalId == externalId) {
// login is for same user that is already logged in, fetch (refresh)
// the current user.
identityModelStore!!.model.jwtToken = jwtBearerToken
operationRepo!!.enqueue(
RefreshUserOperation(
configModel!!.appId,
Expand Down Expand Up @@ -397,7 +398,6 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
newIdentityOneSignalId,
externalId,
if (currentIdentityExternalId == null) currentIdentityOneSignalId else null,
jwtBearerToken,
),
true,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface ISubscriptionBackendService {
aliasLabel: String,
aliasValue: String,
subscription: SubscriptionObject,
jwt: String?,
): String?

/**
Expand All @@ -34,6 +35,7 @@ interface ISubscriptionBackendService {
appId: String,
subscriptionId: String,
subscription: SubscriptionObject,
jwt: String?,
)

/**
Expand All @@ -45,6 +47,7 @@ interface ISubscriptionBackendService {
suspend fun deleteSubscription(
appId: String,
subscriptionId: String,
jwt: String?,
)

/**
Expand All @@ -60,6 +63,7 @@ interface ISubscriptionBackendService {
subscriptionId: String,
aliasLabel: String,
aliasValue: String,
jwt: String?,
)

/**
Expand All @@ -73,5 +77,6 @@ interface ISubscriptionBackendService {
suspend fun getIdentityFromSubscription(
appId: String,
subscriptionId: String,
jwt: String?,
): Map<String, String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ internal class SubscriptionBackendService(
aliasLabel: String,
aliasValue: String,
subscription: SubscriptionObject,
jwt: String?,
): String? {
val jsonSubscription = JSONConverter.convertToJSON(subscription)
jsonSubscription.remove("id")
val requestJSON = JSONObject().put("subscription", jsonSubscription)

val response = _httpClient.post("apps/$appId/users/by/$aliasLabel/$aliasValue/subscriptions", requestJSON)
val response = _httpClient.post("apps/$appId/users/by/$aliasLabel/$aliasValue/subscriptions", requestJSON, jwt)

if (!response.isSuccess) {
throw BackendException(response.statusCode, response.payload)
Expand All @@ -40,12 +41,13 @@ internal class SubscriptionBackendService(
appId: String,
subscriptionId: String,
subscription: SubscriptionObject,
jwt: String?,
) {
val requestJSON =
JSONObject()
.put("subscription", JSONConverter.convertToJSON(subscription))

val response = _httpClient.patch("apps/$appId/subscriptions/$subscriptionId", requestJSON)
val response = _httpClient.patch("apps/$appId/subscriptions/$subscriptionId", requestJSON, jwt)

if (!response.isSuccess) {
throw BackendException(response.statusCode, response.payload)
Expand All @@ -55,8 +57,9 @@ internal class SubscriptionBackendService(
override suspend fun deleteSubscription(
appId: String,
subscriptionId: String,
jwt: String?,
) {
val response = _httpClient.delete("apps/$appId/subscriptions/$subscriptionId")
val response = _httpClient.delete("apps/$appId/subscriptions/$subscriptionId", jwt)

if (!response.isSuccess) {
throw BackendException(response.statusCode, response.payload)
Expand All @@ -68,12 +71,13 @@ internal class SubscriptionBackendService(
subscriptionId: String,
aliasLabel: String,
aliasValue: String,
jwt: String?,
) {
val requestJSON =
JSONObject()
.put("identity", JSONObject().put(aliasLabel, aliasValue))

val response = _httpClient.patch("apps/$appId/subscriptions/$subscriptionId/owner", requestJSON)
val response = _httpClient.patch("apps/$appId/subscriptions/$subscriptionId/owner", requestJSON, jwt)

if (!response.isSuccess) {
throw BackendException(response.statusCode, response.payload)
Expand All @@ -83,8 +87,9 @@ internal class SubscriptionBackendService(
override suspend fun getIdentityFromSubscription(
appId: String,
subscriptionId: String,
jwt: String?,
): Map<String, String> {
val response = _httpClient.get("apps/$appId/subscriptions/$subscriptionId/user/identity")
val response = _httpClient.get("apps/$appId/subscriptions/$subscriptionId/user/identity", jwt)

if (!response.isSuccess) {
throw BackendException(response.statusCode, response.payload)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,16 @@ class LoginUserOperation() : Operation(LoginUserOperationExecutor.LOGIN_USER) {
setOptStringProperty(::existingOnesignalId.name, value)
}

/**
* The JWT token used for the operation that logs in the user.
*/
var jwt: String?
get() = getStringProperty(::jwt.name)
private set(value) {
setStringProperty(::jwt.name, value!!)
}

override val createComparisonKey: String get() = "$appId.User.$onesignalId"
override val modifyComparisonKey: String = ""
override val groupComparisonType: GroupComparisonType = GroupComparisonType.CREATE
override val canStartExecute: Boolean get() = existingOnesignalId == null || !IDManager.isLocalId(existingOnesignalId!!)

constructor(appId: String, onesignalId: String, externalId: String?, existingOneSignalId: String? = null, jwt: String? = null) : this() {
constructor(appId: String, onesignalId: String, externalId: String?, existingOneSignalId: String? = null) : this() {
this.appId = appId
this.onesignalId = onesignalId
this.externalId = externalId
this.existingOnesignalId = existingOneSignalId
this.jwt = jwt
}

override fun translateIds(map: Map<String, String>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ internal class RefreshUserOperationExecutor(
op.appId,
IdentityConstants.ONESIGNAL_ID,
op.onesignalId,
_identityModelStore.model.jwtToken,
)

if (op.onesignalId != _identityModelStore.model.onesignalId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.onesignal.user.internal.backend.IdentityConstants
import com.onesignal.user.internal.backend.SubscriptionObject
import com.onesignal.user.internal.backend.SubscriptionObjectType
import com.onesignal.user.internal.builduser.IRebuildUserService
import com.onesignal.user.internal.identity.IdentityModelStore
import com.onesignal.user.internal.operations.CreateSubscriptionOperation
import com.onesignal.user.internal.operations.DeleteSubscriptionOperation
import com.onesignal.user.internal.operations.TransferSubscriptionOperation
Expand All @@ -34,6 +35,7 @@ internal class SubscriptionOperationExecutor(
private val _subscriptionBackend: ISubscriptionBackendService,
private val _deviceService: IDeviceService,
private val _applicationService: IApplicationService,
private val _identityModelStore: IdentityModelStore,
private val _subscriptionModelStore: SubscriptionModelStore,
private val _configModelStore: ConfigModelStore,
private val _buildUserService: IRebuildUserService,
Expand Down Expand Up @@ -98,6 +100,7 @@ internal class SubscriptionOperationExecutor(
IdentityConstants.ONESIGNAL_ID,
createOperation.onesignalId,
subscription,
_identityModelStore.model.jwtToken,
) ?: return ExecutionResponse(ExecutionResult.SUCCESS)

// update the subscription model with the new ID, if it's still active.
Expand Down Expand Up @@ -163,7 +166,12 @@ internal class SubscriptionOperationExecutor(
AndroidUtils.getAppVersion(_applicationService.appContext),
)

_subscriptionBackend.updateSubscription(lastOperation.appId, lastOperation.subscriptionId, subscription)
_subscriptionBackend.updateSubscription(
lastOperation.appId,
lastOperation.subscriptionId,
subscription,
_identityModelStore.model.jwtToken,
)
} catch (ex: BackendException) {
val responseType = NetworkUtils.getResponseStatusType(ex.statusCode)

Expand Down Expand Up @@ -202,6 +210,7 @@ internal class SubscriptionOperationExecutor(
startingOperation.subscriptionId,
IdentityConstants.ONESIGNAL_ID,
startingOperation.onesignalId,
_identityModelStore.model.jwtToken,
)
} catch (ex: BackendException) {
val responseType = NetworkUtils.getResponseStatusType(ex.statusCode)
Expand Down Expand Up @@ -233,7 +242,7 @@ internal class SubscriptionOperationExecutor(

private suspend fun deleteSubscription(op: DeleteSubscriptionOperation): ExecutionResponse {
try {
_subscriptionBackend.deleteSubscription(op.appId, op.subscriptionId)
_subscriptionBackend.deleteSubscription(op.appId, op.subscriptionId, _identityModelStore.model.jwtToken,)

// remove the subscription model as a HYDRATE in case for some reason it still exists.
_subscriptionModelStore.remove(op.subscriptionId, ModelChangeTags.HYDRATE)
Expand Down

0 comments on commit 268d1a7

Please sign in to comment.