Skip to content

Commit f0a65a2

Browse files
committed
Add deviceAuthToken to subscription requests
1 parent 2658229 commit f0a65a2

File tree

6 files changed

+47
-10
lines changed

6 files changed

+47
-10
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
@@ -25,6 +25,7 @@ interface IUserBackendService {
2525
subscriptions: List<SubscriptionObject>,
2626
properties: Map<String, String>,
2727
jwt: String? = null,
28+
deviceAuthPushToken: String? = null,
2829
): CreateUserResponse
2930
// TODO: Change to send only the push subscription, optimally
3031

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/UserBackendService.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ internal class UserBackendService(
2323
subscriptions: List<SubscriptionObject>,
2424
properties: Map<String, String>,
2525
jwt: String?,
26+
deviceAuthPushToken: String?,
2627
): CreateUserResponse {
2728
val requestJSON = JSONObject()
2829

@@ -41,7 +42,12 @@ internal class UserBackendService(
4142

4243
requestJSON.put("refresh_device_metadata", true)
4344

44-
val response = _httpClient.post("apps/$appId/users", requestJSON, OptionalHeaders(jwt = jwt))
45+
val response =
46+
_httpClient.post(
47+
"apps/$appId/users",
48+
requestJSON,
49+
OptionalHeaders(jwt = jwt, deviceAuthPushToken = deviceAuthPushToken),
50+
)
4551

4652
if (!response.isSuccess) {
4753
throw BackendException(response.statusCode, response.payload, response.retryAfterSeconds)
@@ -71,7 +77,7 @@ internal class UserBackendService(
7177
jsonObject.put("deltas", JSONConverter.convertToJSON(propertyiesDelta))
7278
}
7379

74-
val response = _httpClient.patch("apps/$appId/users/by/$aliasLabel/$aliasValue", jsonObject, jwt)
80+
val response = _httpClient.patch("apps/$appId/users/by/$aliasLabel/$aliasValue", jsonObject, OptionalHeaders(jwt = jwt))
7581

7682
if (!response.isSuccess) {
7783
throw BackendException(response.statusCode, response.payload, response.retryAfterSeconds)
@@ -95,7 +101,7 @@ internal class UserBackendService(
95101
aliasValue: String,
96102
jwt: String?,
97103
): CreateUserResponse {
98-
val response = _httpClient.get("apps/$appId/users/by/$aliasLabel/$aliasValue", jwt)
104+
val response = _httpClient.get("apps/$appId/users/by/$aliasLabel/$aliasValue", OptionalHeaders(jwt = jwt))
99105

100106
if (!response.isSuccess) {
101107
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
@@ -171,7 +171,18 @@ internal class LoginUserOperationExecutor(
171171

172172
try {
173173
val subscriptionList = subscriptions.toList()
174-
val response = _userBackend.createUser(createUserOperation.appId, identities, subscriptionList.map { it.second }, properties)
174+
val pushSubscription = subscriptions.values.find { it.type == SubscriptionObjectType.ANDROID_PUSH }
175+
val response =
176+
_userBackend.createUser(
177+
createUserOperation.appId,
178+
identities,
179+
subscriptionList.map {
180+
it.second
181+
},
182+
properties,
183+
_identityModelStore.model.jwtToken,
184+
pushSubscription?.token,
185+
)
175186
val idTranslations = mutableMapOf<String, String>()
176187
// Add the "local-to-backend" ID translation to the IdentifierTranslator for any operations that were
177188
// *not* executed but still reference the locally-generated IDs.

0 commit comments

Comments
 (0)