Skip to content

Commit 28b2244

Browse files
committed
Add deviceAuthToken to subscription requests
1 parent 0549687 commit 28b2244

File tree

7 files changed

+64
-15
lines changed

7 files changed

+64
-15
lines changed

Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/application/MainApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public void onUserStateChange(@NonNull UserChangedState state) {
145145
@Override
146146
public void onUserJwtInvalidated(@NonNull UserJwtInvalidatedEvent event) {
147147
// !!! For manual testing only
148-
String jwt = "SecondJWT";
148+
String jwt = "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIwMTM5YmQ2Zi00NTFmLTQzOGMtODg4Ni00ZTBmMGZlM2EwODUiLCJleHAiOjE3MjczNjkyMjIsImlkZW50aXR5Ijp7ImV4dGVybmFsX2lkIjoiamluIn0sInN1YnNjcmlwdGlvbnMiOlt7InR5cGUiOiJFbWFpbCIsInRva2VuIjoidGVzdEBkb21haW4uY29tIn0seyJ0eXBlIjoiU01TIiwidG9rZW4iOiIrMTIzNDU2NzgifSx7InR5cGUiOiJBbmRyb2lkUHVzaCIsImlkIjoiMTIzZTQ1NjctZTg5Yi0xMmQzLWE0NTYtNDI2NjE0MTc0MDAwIn1dfQ.6XF7wRF4lLOvKr5Gd3MHv9j7U151hcBjmqSyk6nI6JVYUgt6q0YRp2j1aSJcg8VmaejzP1DouN1DpWUT_JTRXA";
149149
OneSignal.updateUserJwt(event.getExternalId(), jwt);
150150
Log.v(Tag.LOG_TAG, "onUserJwtInvalidated fired with ID:" + event.getExternalId());
151151
}

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/http/impl/HttpClient.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ internal class HttpClient(
9090
// If privacy consent is required but not yet given, any non-GET request should be blocked.
9191
if (method != null && _configModelStore.model.consentRequired == true && _configModelStore.model.consentGiven != true) {
9292
Logging.warn(
93-
"$method `$url` was called before the user provided privacy consent. Your application is set to require the user's privacy consent before the OneSignal SDK can be initialized. Please ensure the user has provided consent before calling this method. You can check the latest OneSignal consent status by calling OneSignal.privacyConsent",
93+
"$method `$url` was called before the user provided privacy consent. " +
94+
"Your application is set to require the user's privacy consent before the OneSignal SDK can be initialized. " +
95+
"Please ensure the user has provided consent before calling this method. You can check the latest OneSignal " +
96+
"consent status by calling OneSignal.privacyConsent",
9497
)
9598
return HttpResponse(0, null, null)
9699
}
@@ -147,8 +150,14 @@ internal class HttpClient(
147150
con.readTimeout = timeout
148151
con.setRequestProperty("SDK-Version", "onesignal/android/" + OneSignalUtils.SDK_VERSION)
149152

150-
if (headers != null && !headers.jwt.isNullOrEmpty()) {
151-
con.setRequestProperty("Authorization", "Bearer ${headers.jwt}")
153+
val jwt = headers?.jwt
154+
if (!jwt.isNullOrEmpty()) {
155+
con.setRequestProperty("Authorization", "Bearer $jwt")
156+
}
157+
158+
val deviceAuthPushToken = headers?.deviceAuthPushToken
159+
if (_configModelStore.model.useIdentityVerification && !deviceAuthPushToken.isNullOrEmpty()) {
160+
con.setRequestProperty("Device-Auth-Push-Token", "Basic $deviceAuthPushToken")
152161
}
153162

154163
if (OneSignalWrapper.sdkType != null && OneSignalWrapper.sdkVersion != null) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ interface IUserBackendService {
2424
subscriptions: List<SubscriptionObject>,
2525
properties: Map<String, String>,
2626
jwt: String? = null,
27+
deviceAuthPushToken: String? = null,
2728
): CreateUserResponse
2829
// TODO: Change to send only the push subscription, optimally
2930

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.onesignal.common.exceptions.BackendException
44
import com.onesignal.common.putMap
55
import com.onesignal.common.toMap
66
import com.onesignal.core.internal.http.IHttpClient
7+
import com.onesignal.core.internal.http.impl.OptionalHeaders
78
import com.onesignal.user.internal.backend.IIdentityBackendService
89
import org.json.JSONObject
910

@@ -21,7 +22,12 @@ internal class IdentityBackendService(
2122
JSONObject()
2223
.put("identity", JSONObject().putMap(identities))
2324

24-
val response = _httpClient.patch("apps/$appId/users/by/$aliasLabel/$aliasValue/identity", requestJSONObject, jwt)
25+
val response =
26+
_httpClient.patch(
27+
"apps/$appId/users/by/$aliasLabel/$aliasValue/identity",
28+
requestJSONObject,
29+
OptionalHeaders(jwt = jwt),
30+
)
2531

2632
if (!response.isSuccess) {
2733
throw BackendException(response.statusCode, response.payload, response.retryAfterSeconds)
@@ -39,7 +45,11 @@ internal class IdentityBackendService(
3945
aliasLabelToDelete: String,
4046
jwt: String?,
4147
) {
42-
val response = _httpClient.delete("apps/$appId/users/by/$aliasLabel/$aliasValue/identity/$aliasLabelToDelete", jwt)
48+
val response =
49+
_httpClient.delete(
50+
"apps/$appId/users/by/$aliasLabel/$aliasValue/identity/$aliasLabelToDelete",
51+
OptionalHeaders(jwt = jwt),
52+
)
4353

4454
if (!response.isSuccess) {
4555
throw BackendException(response.statusCode, response.payload, response.retryAfterSeconds)

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.onesignal.common.exceptions.BackendException
44
import com.onesignal.common.safeJSONObject
55
import com.onesignal.common.toMap
66
import com.onesignal.core.internal.http.IHttpClient
7+
import com.onesignal.core.internal.http.impl.OptionalHeaders
78
import com.onesignal.user.internal.backend.ISubscriptionBackendService
89
import com.onesignal.user.internal.backend.SubscriptionObject
910
import org.json.JSONObject
@@ -22,7 +23,12 @@ internal class SubscriptionBackendService(
2223
jsonSubscription.remove("id")
2324
val requestJSON = JSONObject().put("subscription", jsonSubscription)
2425

25-
val response = _httpClient.post("apps/$appId/users/by/$aliasLabel/$aliasValue/subscriptions", requestJSON, jwt)
26+
val response =
27+
_httpClient.post(
28+
"apps/$appId/users/by/$aliasLabel/$aliasValue/subscriptions",
29+
requestJSON,
30+
OptionalHeaders(jwt = jwt, deviceAuthPushToken = subscription.token),
31+
)
2632

2733
if (!response.isSuccess) {
2834
throw BackendException(response.statusCode, response.payload, response.retryAfterSeconds)
@@ -52,7 +58,12 @@ internal class SubscriptionBackendService(
5258
JSONObject()
5359
.put("subscription", JSONConverter.convertToJSON(subscription))
5460

55-
val response = _httpClient.patch("apps/$appId/subscriptions/$subscriptionId", requestJSON, jwt)
61+
val response =
62+
_httpClient.patch(
63+
"apps/$appId/subscriptions/$subscriptionId",
64+
requestJSON,
65+
OptionalHeaders(jwt = jwt, deviceAuthPushToken = subscription.token),
66+
)
5667

5768
if (!response.isSuccess) {
5869
throw BackendException(response.statusCode, response.payload, response.retryAfterSeconds)
@@ -71,7 +82,7 @@ internal class SubscriptionBackendService(
7182
subscriptionId: String,
7283
jwt: String?,
7384
) {
74-
val response = _httpClient.delete("apps/$appId/subscriptions/$subscriptionId", jwt)
85+
val response = _httpClient.delete("apps/$appId/subscriptions/$subscriptionId", OptionalHeaders(jwt = jwt))
7586

7687
if (!response.isSuccess) {
7788
throw BackendException(response.statusCode, response.payload, response.retryAfterSeconds)
@@ -89,7 +100,7 @@ internal class SubscriptionBackendService(
89100
JSONObject()
90101
.put("identity", JSONObject().put(aliasLabel, aliasValue))
91102

92-
val response = _httpClient.patch("apps/$appId/subscriptions/$subscriptionId/owner", requestJSON, jwt)
103+
val response = _httpClient.patch("apps/$appId/subscriptions/$subscriptionId/owner", requestJSON, OptionalHeaders(jwt = jwt))
93104

94105
if (!response.isSuccess) {
95106
throw BackendException(response.statusCode, response.payload, response.retryAfterSeconds)
@@ -101,7 +112,7 @@ internal class SubscriptionBackendService(
101112
subscriptionId: String,
102113
jwt: String?,
103114
): Map<String, String> {
104-
val response = _httpClient.get("apps/$appId/subscriptions/$subscriptionId/user/identity", jwt)
115+
val response = _httpClient.get("apps/$appId/subscriptions/$subscriptionId/user/identity", OptionalHeaders(jwt = jwt))
105116

106117
if (!response.isSuccess) {
107118
throw BackendException(response.statusCode, response.payload, response.retryAfterSeconds)

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.onesignal.user.internal.backend.impl
33
import com.onesignal.common.exceptions.BackendException
44
import com.onesignal.common.putMap
55
import com.onesignal.core.internal.http.IHttpClient
6+
import com.onesignal.core.internal.http.impl.OptionalHeaders
67
import com.onesignal.user.internal.backend.CreateUserResponse
78
import com.onesignal.user.internal.backend.IUserBackendService
89
import com.onesignal.user.internal.backend.PropertiesDeltasObject
@@ -19,6 +20,7 @@ internal class UserBackendService(
1920
subscriptions: List<SubscriptionObject>,
2021
properties: Map<String, String>,
2122
jwt: String?,
23+
deviceAuthPushToken: String?,
2224
): CreateUserResponse {
2325
val requestJSON = JSONObject()
2426

@@ -37,7 +39,12 @@ internal class UserBackendService(
3739

3840
requestJSON.put("refresh_device_metadata", true)
3941

40-
val response = _httpClient.post("apps/$appId/users", requestJSON, jwt)
42+
val response =
43+
_httpClient.post(
44+
"apps/$appId/users",
45+
requestJSON,
46+
OptionalHeaders(jwt = jwt, deviceAuthPushToken = deviceAuthPushToken),
47+
)
4148

4249
if (!response.isSuccess) {
4350
throw BackendException(response.statusCode, response.payload, response.retryAfterSeconds)
@@ -67,7 +74,7 @@ internal class UserBackendService(
6774
jsonObject.put("deltas", JSONConverter.convertToJSON(propertyiesDelta))
6875
}
6976

70-
val response = _httpClient.patch("apps/$appId/users/by/$aliasLabel/$aliasValue", jsonObject, jwt)
77+
val response = _httpClient.patch("apps/$appId/users/by/$aliasLabel/$aliasValue", jsonObject, OptionalHeaders(jwt = jwt))
7178

7279
if (!response.isSuccess) {
7380
throw BackendException(response.statusCode, response.payload, response.retryAfterSeconds)
@@ -87,7 +94,7 @@ internal class UserBackendService(
8794
aliasValue: String,
8895
jwt: String?,
8996
): CreateUserResponse {
90-
val response = _httpClient.get("apps/$appId/users/by/$aliasLabel/$aliasValue", jwt)
97+
val response = _httpClient.get("apps/$appId/users/by/$aliasLabel/$aliasValue", OptionalHeaders(jwt = jwt))
9198

9299
if (!response.isSuccess) {
93100
throw BackendException(response.statusCode, response.payload, response.retryAfterSeconds)

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,18 @@ internal class LoginUserOperationExecutor(
164164

165165
try {
166166
val subscriptionList = subscriptions.toList()
167-
val response = _userBackend.createUser(createUserOperation.appId, identities, subscriptionList.map { it.second }, properties)
167+
val pushSubscription = subscriptions.values.find { it.type == SubscriptionObjectType.ANDROID_PUSH }
168+
val response =
169+
_userBackend.createUser(
170+
createUserOperation.appId,
171+
identities,
172+
subscriptionList.map {
173+
it.second
174+
},
175+
properties,
176+
_identityModelStore.model.jwtToken,
177+
pushSubscription?.token,
178+
)
168179
val idTranslations = mutableMapOf<String, String>()
169180
// Add the "local-to-backend" ID translation to the IdentifierTranslator for any operations that were
170181
// *not* executed but still reference the locally-generated IDs.

0 commit comments

Comments
 (0)