Skip to content

Commit 9773e60

Browse files
committed
Revert "have the rest of the SDK call on the new functions"
This reverts commit 8ffd20f.
1 parent 8ffd20f commit 9773e60

8 files changed

+128
-173
lines changed

swift-sdk.xcodeproj/project.pbxproj

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
5531CDAC22A997A4000D05E2 /* IterableInboxViewControllerUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5585DF9022A877E6000A32B9 /* IterableInboxViewControllerUITests.swift */; };
1616
5531CDAE22A9C992000D05E2 /* ClassExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5531CDAD22A9C992000D05E2 /* ClassExtensionsTests.swift */; };
1717
5536781F2576FF9000DB3652 /* IterableUtilTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5536781E2576FF9000DB3652 /* IterableUtilTests.swift */; };
18-
5555425028BED1B400DB5D20 /* KeychainWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5555424F28BED1B400DB5D20 /* KeychainWrapper.swift */; };
1918
556FB1EA244FAF6A00EDF6BD /* InAppPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 556FB1E9244FAF6A00EDF6BD /* InAppPresenter.swift */; };
2019
557AE6BF24A56E5E00B57750 /* Auth.swift in Sources */ = {isa = PBXBuildFile; fileRef = 557AE6BE24A56E5E00B57750 /* Auth.swift */; };
2120
5585DF8F22A73390000A32B9 /* IterableInboxViewControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5585DF8E22A73390000A32B9 /* IterableInboxViewControllerTests.swift */; };
@@ -397,7 +396,6 @@
397396
552A0AA6280E1FDA00A80963 /* DeepLinkManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeepLinkManager.swift; sourceTree = "<group>"; };
398397
5531CDAD22A9C992000D05E2 /* ClassExtensionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClassExtensionsTests.swift; sourceTree = "<group>"; };
399398
5536781E2576FF9000DB3652 /* IterableUtilTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IterableUtilTests.swift; sourceTree = "<group>"; };
400-
5555424F28BED1B400DB5D20 /* KeychainWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeychainWrapper.swift; sourceTree = "<group>"; };
401399
556FB1E9244FAF6A00EDF6BD /* InAppPresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InAppPresenter.swift; sourceTree = "<group>"; };
402400
557AE6BE24A56E5E00B57750 /* Auth.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Auth.swift; sourceTree = "<group>"; };
403401
5585DF8E22A73390000A32B9 /* IterableInboxViewControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IterableInboxViewControllerTests.swift; sourceTree = "<group>"; };
@@ -1265,11 +1263,10 @@
12651263
ACE34AB121376ACB00691224 /* Local Storage */ = {
12661264
isa = PBXGroup;
12671265
children = (
1268-
AC52C5B9272A8BC2000DCDCF /* IterableKeychain.swift */,
1269-
AC52C5B52729CE44000DCDCF /* IterableUserDefaults.swift */,
1270-
5555424F28BED1B400DB5D20 /* KeychainWrapper.swift */,
1271-
ACE34AB221376B1000691224 /* LocalStorage.swift */,
12721266
ACE34AB62139D70B00691224 /* LocalStorageProtocol.swift */,
1267+
ACE34AB221376B1000691224 /* LocalStorage.swift */,
1268+
AC52C5B52729CE44000DCDCF /* IterableUserDefaults.swift */,
1269+
AC52C5B9272A8BC2000DCDCF /* IterableKeychain.swift */,
12731270
);
12741271
name = "Local Storage";
12751272
sourceTree = "<group>";
@@ -1826,7 +1823,6 @@
18261823
AC84510922910A0C0052BB8F /* RequestCreator.swift in Sources */,
18271824
ACB8273F22372A5C00DB17D3 /* IterableHtmlMessageViewController.swift in Sources */,
18281825
AC5812F624F3A90F007E6D36 /* OfflineRequestProcessor.swift in Sources */,
1829-
5555425028BED1B400DB5D20 /* KeychainWrapper.swift in Sources */,
18301826
AC81918A22713A400014955E /* AbstractDiffCalculator.swift in Sources */,
18311827
ACA95D2D275494A100AF4666 /* InboxViewRepresentable.swift in Sources */,
18321828
AC684A88222F4FDD00F29749 /* InAppDisplayer.swift in Sources */,

swift-sdk/Constants.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ enum Const {
6363
static let email = "itbl_email"
6464
static let userId = "itbl_userid"
6565
static let authToken = "itbl_auth_token"
66-
static let lastPushPayload = "itbl_last_push_payload"
6766
}
6867
}
6968

swift-sdk/Internal/InternalIterableAPI.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
4141
}
4242

4343
var lastPushPayload: [AnyHashable: Any]? {
44-
localStorage.getLastPushPayload(dateProvider.currentDate)
44+
localStorage.getPayload(currentDate: dateProvider.currentDate)
4545
}
4646

4747
var attributionInfo: IterableAttributionInfo? {

swift-sdk/Internal/IterableKeychain.swift

Lines changed: 118 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,31 +59,134 @@ class IterableKeychain {
5959
}
6060
}
6161

62-
func getLastPushPayload(currentDate: Date) -> [AnyHashable: Any]? {
63-
// let data = wrapper.data(forKey: Const.Keychain.Key.lastPushPayload)
62+
init(wrapper: KeychainWrapper = KeychainWrapper()) {
63+
self.wrapper = wrapper
64+
}
65+
66+
private let wrapper: KeychainWrapper
67+
}
68+
69+
/// Basic wrapper for keychain
70+
/// This should have no dependency on Iterable classes
71+
class KeychainWrapper {
72+
init(serviceName: String = Const.Keychain.serviceName) {
73+
self.serviceName = serviceName
74+
}
75+
76+
@discardableResult
77+
func set(_ value: Data, forKey key: String) -> Bool {
78+
var keychainQueryDictionary: [String: Any] = setupKeychainQueryDictionary(forKey: key)
79+
80+
keychainQueryDictionary[SecValueData] = value
6481

82+
// Assign default protection - Protect the keychain entry so it's only valid when the device is unlocked
83+
keychainQueryDictionary[SecAttrAccessible] = SecAttrAccessibleWhenUnlocked
6584

85+
let status: OSStatus = SecItemAdd(keychainQueryDictionary as CFDictionary, nil)
6686

67-
return nil
87+
if status == errSecSuccess {
88+
return true
89+
} else if status == errSecDuplicateItem {
90+
return update(value, forKey: key)
91+
} else {
92+
return false
93+
}
94+
}
95+
96+
func data(forKey key: String) -> Data? {
97+
var keychainQueryDictionary = setupKeychainQueryDictionary(forKey: key)
98+
99+
// Limit search results to one
100+
keychainQueryDictionary[SecMatchLimit] = SecMatchLimitOne
101+
102+
// Specify we want Data/CFData returned
103+
keychainQueryDictionary[SecReturnData] = CFBooleanTrue
104+
105+
// Search
106+
var result: AnyObject?
107+
let status = SecItemCopyMatching(keychainQueryDictionary as CFDictionary, &result)
108+
109+
return status == noErr ? result as? Data : nil
68110
}
69111

70-
func setLastPushPayload(_ payload: [AnyHashable: Any]?, withExpiration expiration: Date?) {
71-
guard let value = payload?.jsonValue, JSONSerialization.isValidJSONObject(value) else {
72-
wrapper.removeValue(forKey: Const.Keychain.Key.lastPushPayload)
73-
return
112+
@discardableResult
113+
func removeValue(forKey key: String) -> Bool {
114+
let keychainQueryDictionary: [String: Any] = setupKeychainQueryDictionary(forKey: key)
115+
116+
// Delete
117+
let status: OSStatus = SecItemDelete(keychainQueryDictionary as CFDictionary)
118+
119+
if status == errSecSuccess {
120+
return true
121+
} else {
122+
return false
74123
}
124+
}
125+
126+
@discardableResult
127+
func removeAll() -> Bool {
128+
var keychainQueryDictionary: [String: Any] = [SecClass: SecClassGenericPassword]
129+
130+
keychainQueryDictionary[SecAttrService] = serviceName
131+
132+
let status: OSStatus = SecItemDelete(keychainQueryDictionary as CFDictionary)
75133

76-
do {
77-
let data = try JSONSerialization.data(withJSONObject: value, options: [])
78-
wrapper.set(data, forKey: Const.Keychain.Key.lastPushPayload)
79-
} catch {
80-
wrapper.removeValue(forKey: Const.Keychain.Key.lastPushPayload)
134+
if status == errSecSuccess {
135+
return true
136+
} else {
137+
return false
81138
}
82139
}
83140

84-
init(wrapper: KeychainWrapper = KeychainWrapper()) {
85-
self.wrapper = wrapper
141+
142+
private let serviceName: String
143+
144+
private func setupKeychainQueryDictionary(forKey key: String) -> [String: Any] {
145+
// Setup default access as generic password (rather than a certificate, internet password, etc)
146+
var keychainQueryDictionary: [String: Any] = [SecClass: SecClassGenericPassword]
147+
148+
// Uniquely identify this keychain accessor
149+
keychainQueryDictionary[SecAttrService] = serviceName
150+
151+
// Uniquely identify the account who will be accessing the keychain
152+
let encodedIdentifier: Data? = key.data(using: .utf8)
153+
154+
keychainQueryDictionary[SecAttrGeneric] = encodedIdentifier
155+
156+
keychainQueryDictionary[SecAttrAccount] = encodedIdentifier
157+
158+
keychainQueryDictionary[SecAttrSynchronizable] = CFBooleanFalse
159+
160+
return keychainQueryDictionary
86161
}
87162

88-
private let wrapper: KeychainWrapper
163+
private func update(_ value: Data, forKey key: String) -> Bool {
164+
let keychainQueryDictionary: [String: Any] = setupKeychainQueryDictionary(forKey: key)
165+
let updateDictionary = [SecValueData: value]
166+
167+
// Update
168+
let status: OSStatus = SecItemUpdate(keychainQueryDictionary as CFDictionary, updateDictionary as CFDictionary)
169+
170+
if status == errSecSuccess {
171+
return true
172+
} else {
173+
return false
174+
}
175+
}
176+
177+
private let SecValueData = kSecValueData as String
178+
private let SecAttrAccessible: String = kSecAttrAccessible as String
179+
private let SecAttrAccessibleWhenUnlocked = kSecAttrAccessibleWhenUnlocked
180+
private let SecClass: String = kSecClass as String
181+
private let SecClassGenericPassword = kSecClassGenericPassword
182+
private let SecAttrService: String = kSecAttrService as String
183+
private let SecAttrGeneric: String = kSecAttrGeneric as String
184+
private let SecAttrAccount: String = kSecAttrAccount as String
185+
private let SecAttrSynchronizable: String = kSecAttrSynchronizable as String
186+
private let CFBooleanTrue = kCFBooleanTrue
187+
private let CFBooleanFalse = kCFBooleanFalse
188+
private let SecMatchLimit: String = kSecMatchLimit as String
189+
private let SecMatchLimitOne = kSecMatchLimitOne
190+
private let SecReturnData: String = kSecReturnData as String
89191
}
192+

swift-sdk/Internal/IterableUserDefaults.swift

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,6 @@ class IterableUserDefaults {
8282
try? save(dict: payload, withKey: .payload, andExpiration: expiration)
8383
}
8484

85-
func getLastPushPayloadAndExpirationPair() -> (payload: [AnyHashable: Any]?, expiration: Date?)? {
86-
guard let encodedEnvelope = userDefaults.value(forKey: UserDefaultsKey.payload.value) as? Data else {
87-
return nil
88-
}
89-
90-
do {
91-
let envelope = try JSONDecoder().decode(Envelope.self, from: encodedEnvelope)
92-
let decoded = try JSONSerialization.jsonObject(with: envelope.payload, options: []) as? [AnyHashable: Any]
93-
94-
return (payload: decoded, envelope.expiration)
95-
} catch {
96-
return nil
97-
}
98-
}
99-
10085
// MARK: Private implementation
10186

10287
private let userDefaults: UserDefaults

swift-sdk/Internal/KeychainWrapper.swift

Lines changed: 0 additions & 116 deletions
This file was deleted.

swift-sdk/Internal/LocalStorage.swift

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,19 @@ struct LocalStorage: LocalStorageProtocol {
7575
iterableUserDefaults.save(attributionInfo: attributionInfo, withExpiration: expiration)
7676
}
7777

78-
func getLastPushPayload(_ currentDate: Date) -> [AnyHashable: Any]? {
79-
return keychain.getLastPushPayload(currentDate: currentDate)
78+
func getPayload(currentDate: Date) -> [AnyHashable: Any]? {
79+
iterableUserDefaults.getPayload(currentDate: currentDate)
8080
}
8181

82-
func saveLastPushPayload(_ payload: [AnyHashable: Any]?, withExpiration expiration: Date?) {
83-
keychain.setLastPushPayload(payload, withExpiration: expiration)
82+
func save(payload: [AnyHashable: Any]?, withExpiration expiration: Date?) {
83+
iterableUserDefaults.save(payload: payload, withExpiration: expiration)
8484
}
8585

8686
func upgrade() {
8787
ITBInfo()
8888

8989
/// moves `email`, `userId`, and `authToken` from `UserDefaults` to `IterableKeychain`
9090
moveAuthDataFromUserDefaultsToKeychain()
91-
92-
/// moves `lastPushPayload` from `UserDefaults` to `IterableKeychain`
93-
moveLastPushPayloadFromUserDefaultsToKeychain()
9491
}
9592

9693
// MARK: Private
@@ -120,13 +117,4 @@ struct LocalStorage: LocalStorageProtocol {
120117
ITBInfo("UPDATED: moved userId from UserDefaults to IterableKeychain")
121118
}
122119
}
123-
124-
private func moveLastPushPayloadFromUserDefaultsToKeychain() {
125-
// using current date rather than `DateProvider` for convenience
126-
if let (userDefaultLastPushPayload, expiration) = iterableUserDefaults.getLastPushPayloadAndExpirationPair() {
127-
keychain.setLastPushPayload(userDefaultLastPushPayload, withExpiration: expiration)
128-
129-
ITBInfo("UPDATED: moved lastPushPayload from UserDefaults to IterableKeychain")
130-
}
131-
}
132120
}

swift-sdk/Internal/LocalStorageProtocol.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ protocol LocalStorageProtocol {
1414
var offlineMode: Bool { get set }
1515
func getAttributionInfo(currentDate: Date) -> IterableAttributionInfo?
1616
func save(attributionInfo: IterableAttributionInfo?, withExpiration expiration: Date?)
17-
func getLastPushPayload(_ currentDate: Date) -> [AnyHashable: Any]?
18-
func saveLastPushPayload(_ payload: [AnyHashable: Any]?, withExpiration expiration: Date?)
17+
func getPayload(currentDate: Date) -> [AnyHashable: Any]?
18+
func save(payload: [AnyHashable: Any]?, withExpiration: Date?)
1919
func upgrade()
2020
}
2121

0 commit comments

Comments
 (0)