Skip to content

Commit 8ee0ffb

Browse files
authored
Change device id hash length (#532)
* Change device id hash length * Rename devices attribute * Update min version * Validate device id * Set device property for anon user
1 parent 56258b4 commit 8ee0ffb

File tree

6 files changed

+32
-25
lines changed

6 files changed

+32
-25
lines changed

Leanplum-iOS-SDK.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Pod::Spec.new do |s|
3232
s.resource_bundle = {
3333
'Leanplum-iOS-SDK' => 'LeanplumSDK/LeanplumSDKBundle/Resources/**/*'
3434
}
35-
s.dependency 'CleverTap-iOS-SDK', '~> 4.1'
35+
s.dependency 'CleverTap-iOS-SDK', '~> 4.1.4'
3636
s.swift_version = '5.0'
3737
end
3838

LeanplumSDK/LeanplumSDK/ClassesSwift/Migration/Wrapper/CTWrapper.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class CTWrapper: Wrapper {
2222
static let iOSReceiptDataParam = "iOSReceiptData"
2323
static let iOSSandboxParam = "iOSSandbox"
2424

25-
static let DevicesUserProperty = "devices"
25+
static let DevicesUserProperty = "lp_devices"
26+
static let AnonymousDeviceUserProperty = "lp_device"
2627
}
2728

2829
// MARK: Initialization
@@ -69,8 +70,11 @@ class CTWrapper: Wrapper {
6970
""")
7071
cleverTapInstance?.onUserLogin(identityManager.profile,
7172
withCleverTapID: identityManager.cleverTapID)
72-
7373
setDevicesProperty()
74+
} else {
75+
if !identityManager.isValidCleverTapID {
76+
cleverTapInstance?.profilePush([Constants.AnonymousDeviceUserProperty: identityManager.deviceId])
77+
}
7478
}
7579
triggerInstanceCallback()
7680
}
@@ -218,7 +222,9 @@ class CTWrapper: Wrapper {
218222

219223
func setDevicesProperty() {
220224
// CleverTap SDK ensures the values are unique locally
221-
cleverTapInstance?.profileAddMultiValue(identityManager.deviceId, forKey: Constants.DevicesUserProperty)
225+
if !identityManager.isValidCleverTapID {
226+
cleverTapInstance?.profileAddMultiValue(identityManager.deviceId, forKey: Constants.DevicesUserProperty)
227+
}
222228
}
223229

224230
// MARK: Traffic Source

LeanplumSDK/LeanplumSDK/ClassesSwift/Migration/Wrapper/IdentityManager.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,8 @@ class IdentityManager {
106106
}
107107

108108
var isValidCleverTapID: Bool {
109-
// Only the deviceId could be invalid, since the userIdHash should always be valid,
110-
// but we still validate the whole CTID to be safe
111-
CleverTap.isValidCleverTapId(originalCleverTapID) &&
109+
// Only the deviceId could be invalid, since the userIdHash should always be valid
110+
CleverTap.isValidCleverTapId(deviceId) &&
112111
deviceId.count <= Constants.DeviceIdLengthLimit
113112
}
114113

@@ -125,7 +124,7 @@ class IdentityManager {
125124
return originalCleverTapID
126125
}
127126

128-
guard let ctDevice = Utilities.sha256_128(string: deviceId) else {
127+
guard let ctDevice = Utilities.sha256_200(string: deviceId) else {
129128
Log.error("[Wrapper] Failed to generate SHA256 for deviceId: \(deviceId)")
130129
return originalCleverTapID
131130
}

LeanplumSDK/LeanplumSDK/ClassesSwift/Utilities/Utilities.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ public class Utilities: NSObject {
3737
return hashedData.hexEncodedString()
3838
}
3939

40-
@objc public static func sha256_128(string: String) -> String? {
40+
@objc public static func sha256_200(string: String) -> String? {
4141
guard let str = sha256(string: string) else { return nil }
4242

43-
let hexLength = 256/2/4
43+
let hexLength = 200/4
4444
return substring(string: str, openEndIndex: hexLength)
4545
}
4646

LeanplumSDKApp/LeanplumSDKTests/Classes/Migration/IdentityManagerTest.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class IdentityManagerTest: XCTestCase {
2222
let userId2_hash = "c9430313f8"
2323

2424
let totalIdLengthLimit = 61
25+
let deviceIdHashLength = 50
2526

2627
func testProfile() {
2728
let identityManager = IdentityManagerMock(userId: deviceId, deviceId: deviceId)
@@ -128,56 +129,57 @@ class IdentityManagerTest: XCTestCase {
128129
}
129130

130131
func testAnonymousLimitDeviceId() {
131-
let deviceId = Array(repeating: "1", count: 50).joined()
132+
let deviceId = Array(repeating: "1", count: deviceIdHashLength).joined()
132133
let identityManager = IdentityManagerMock(userId: deviceId, deviceId: deviceId)
133134

134135
XCTAssertEqual(identityManager.cleverTapID, deviceId)
135136
XCTAssertTrue(identityManager.cleverTapID.count <= totalIdLengthLimit)
136137
}
137138

138139
func testIdentifiedLimitDeviceId() {
139-
let deviceId = Array(repeating: "1", count: 50).joined()
140+
let deviceId = Array(repeating: "1", count: deviceIdHashLength).joined()
140141
let identityManager = IdentityManagerMock(userId: userId, deviceId: deviceId)
141142

142143
XCTAssertEqual(identityManager.cleverTapID, "\(deviceId)_\(userId_hash)")
143144
XCTAssertTrue(identityManager.cleverTapID.count == totalIdLengthLimit)
144145
}
145146

146147
func testAnonymousLongDeviceId() {
147-
let deviceId = Array(repeating: "1", count: 51).joined()
148+
let deviceId = Array(repeating: "1", count: deviceIdHashLength + 1).joined()
148149
let identityManager = IdentityManagerMock(userId: deviceId, deviceId: deviceId)
149150

150-
let deviceId_sha = Utilities.sha256_128(string: deviceId)!
151+
let deviceId_sha = Utilities.sha256_200(string: deviceId)!
151152

152153
XCTAssertEqual(identityManager.cleverTapID, deviceId_sha)
154+
XCTAssertTrue(identityManager.cleverTapID.count == deviceIdHashLength)
153155
XCTAssertTrue(identityManager.cleverTapID.count <= totalIdLengthLimit)
154156
}
155157

156158
func testIdentifiedLongDeviceId() {
157-
let deviceId = Array(repeating: "1", count: 51).joined()
159+
let deviceId = Array(repeating: "1", count: deviceIdHashLength + 1).joined()
158160
let identityManager = IdentityManagerMock(userId: userId, deviceId: deviceId)
159161

160-
let deviceId_sha = Utilities.sha256_128(string: deviceId)!
162+
let deviceId_sha = Utilities.sha256_200(string: deviceId)!
161163

162164
XCTAssertEqual(identityManager.cleverTapID, "\(deviceId_sha)_\(userId_hash)")
163-
XCTAssertTrue(identityManager.cleverTapID.count <= totalIdLengthLimit)
165+
XCTAssertTrue(identityManager.cleverTapID.count == totalIdLengthLimit)
164166
}
165167

166168
func testIdentifiedLongerDeviceId() {
167-
let deviceId = Array(repeating: "1", count: 60).joined()
169+
let deviceId = Array(repeating: "1", count: deviceIdHashLength + 10).joined()
168170
let identityManager = IdentityManagerMock(userId: userId, deviceId: deviceId)
169171

170-
let deviceId_sha = Utilities.sha256_128(string: deviceId)!
172+
let deviceId_sha = Utilities.sha256_200(string: deviceId)!
171173

172174
XCTAssertEqual(identityManager.cleverTapID, "\(deviceId_sha)_\(userId_hash)")
173-
XCTAssertTrue(identityManager.cleverTapID.count <= totalIdLengthLimit)
175+
XCTAssertTrue(identityManager.cleverTapID.count == totalIdLengthLimit)
174176
}
175177

176178
func testAnonymousInvalidDeviceId() {
177179
let deviceId = Array(repeating: "&", count: 10).joined()
178180
let identityManager = IdentityManagerMock(userId: deviceId, deviceId: deviceId)
179181

180-
let deviceId_sha = Utilities.sha256_128(string: deviceId)!
182+
let deviceId_sha = Utilities.sha256_200(string: deviceId)!
181183

182184
XCTAssertEqual(identityManager.cleverTapID, deviceId_sha)
183185
XCTAssertTrue(identityManager.cleverTapID.count <= totalIdLengthLimit)
@@ -187,7 +189,7 @@ class IdentityManagerTest: XCTestCase {
187189
let deviceId = Array(repeating: "&", count: 10).joined()
188190
let identityManager = IdentityManagerMock(userId: userId, deviceId: deviceId)
189191

190-
let deviceId_sha = Utilities.sha256_128(string: deviceId)!
192+
let deviceId_sha = Utilities.sha256_200(string: deviceId)!
191193

192194
XCTAssertEqual(identityManager.cleverTapID, "\(deviceId_sha)_\(userId_hash)")
193195
XCTAssertTrue(identityManager.cleverTapID.count <= totalIdLengthLimit)
@@ -226,7 +228,7 @@ class IdentityManagerTest: XCTestCase {
226228
"9d29641dc261454239456122f13de042b3a0cc3f45d4c27e7ddc97b300eb11aa"
227229
]
228230

229-
let hashes = invalidDeviceIds.map(Utilities.sha256_128(string:))
231+
let hashes = invalidDeviceIds.map(Utilities.sha256_200(string:))
230232

231233
for (i, id) in invalidDeviceIds.enumerated() {
232234
let identityManager = IdentityManagerMock(userId: userId, deviceId: id)

LeanplumSDKApp/LeanplumSDKTests/Classes/UtilitiesTest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ class UtilitiesTest: XCTestCase {
9191
]
9292

9393
for (i, str) in strings.enumerated() {
94-
let hash = Utilities.sha256_128(string: str)!
95-
XCTAssertEqual(hash.count, 32)
94+
let hash = Utilities.sha256_200(string: str)!
95+
XCTAssertEqual(hash.count, 50)
9696
XCTAssertTrue(hashes[i].contains(hash))
9797
}
9898
}

0 commit comments

Comments
 (0)