Skip to content

Commit 86b26f2

Browse files
authored
Merge pull request #430 from Iterable/MOB-2365-jwt-set-radio
[MOB-2365] account for more JWT cases
2 parents ebe8934 + 0a7ca04 commit 86b26f2

File tree

4 files changed

+105
-29
lines changed

4 files changed

+105
-29
lines changed

swift-sdk/Internal/AuthManager.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class AuthManager: IterableInternalAuthManagerProtocol {
4343

4444
// @objc attribute only needed for the pre-iOS 10 Timer constructor in queueAuthTokenExpirationRefresh
4545
@objc func requestNewAuthToken(hasFailedPriorAuth: Bool = false, onSuccess: AuthTokenRetrievalHandler? = nil) {
46+
ITBInfo()
47+
4648
guard !pendingAuth else {
4749
return
4850
}
@@ -61,6 +63,8 @@ class AuthManager: IterableInternalAuthManagerProtocol {
6163
}
6264

6365
func logoutUser() {
66+
ITBInfo()
67+
6468
authToken = nil
6569

6670
storeAuthToken()
@@ -70,9 +74,8 @@ class AuthManager: IterableInternalAuthManagerProtocol {
7074

7175
// MARK: - Private/Internal
7276

73-
private var expirationRefreshTimer: Timer?
74-
7577
private var authToken: String?
78+
private var expirationRefreshTimer: Timer?
7679

7780
private var pendingAuth: Bool = false
7881
private var hasFailedPriorAuth: Bool = false
@@ -107,6 +110,8 @@ class AuthManager: IterableInternalAuthManagerProtocol {
107110
}
108111

109112
private func queueAuthTokenExpirationRefresh(_ authToken: String?) {
113+
ITBInfo()
114+
110115
guard let authToken = authToken, let expirationDate = AuthManager.decodeExpirationDateFromAuthToken(authToken) else {
111116
return
112117
}

swift-sdk/Internal/InAppManager.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,9 @@ extension InAppManager: InAppNotifiable {
552552
func scheduleSync() -> Future<Bool, Error> {
553553
ITBInfo()
554554

555-
return InAppManager.getAppIsReady(applicationStateProvider: applicationStateProvider, displayer: displayer).flatMap { self.scheduleSync(appIsReady: $0) }
555+
return InAppManager.getAppIsReady(applicationStateProvider: applicationStateProvider,
556+
displayer: displayer)
557+
.flatMap { self.scheduleSync(appIsReady: $0) }
556558
}
557559

558560
private func scheduleSync(appIsReady: Bool) -> Future<Bool, Error> {

swift-sdk/Internal/IterableAPIInternal.swift

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -92,37 +92,51 @@ final class IterableAPIInternal: NSObject, PushTrackerProtocol, AuthProvider {
9292
}
9393

9494
func setEmail(_ email: String?) {
95-
if email != _email {
95+
ITBInfo()
96+
97+
if email == nil {
9698
logoutPreviousUser()
97-
98-
_email = email
99-
_userId = nil
100-
101-
storeIdentifierData()
102-
103-
authManager.requestNewAuthToken(hasFailedPriorAuth: false, onSuccess: { [weak self] authToken in
104-
_ = self?.inAppManager.scheduleSync()
105-
})
106-
107-
loginNewUser()
99+
return
100+
}
101+
102+
if _email == email {
103+
requestNewAuthToken()
104+
return
108105
}
106+
107+
logoutPreviousUser()
108+
109+
_email = email
110+
_userId = nil
111+
112+
storeIdentifierData()
113+
114+
requestNewAuthToken()
115+
loginNewUser()
109116
}
110117

111118
func setUserId(_ userId: String?) {
112-
if userId != _userId {
119+
ITBInfo()
120+
121+
if userId == nil {
113122
logoutPreviousUser()
114-
115-
_email = nil
116-
_userId = userId
117-
118-
storeIdentifierData()
119-
120-
authManager.requestNewAuthToken(hasFailedPriorAuth: false, onSuccess: { [weak self] authToken in
121-
_ = self?.inAppManager.scheduleSync()
122-
})
123-
124-
loginNewUser()
123+
return
125124
}
125+
126+
if _userId == userId {
127+
requestNewAuthToken()
128+
return
129+
}
130+
131+
logoutPreviousUser()
132+
133+
_email = nil
134+
_userId = userId
135+
136+
storeIdentifierData()
137+
138+
requestNewAuthToken()
139+
loginNewUser()
126140
}
127141

128142
func logoutUser() {
@@ -425,6 +439,12 @@ final class IterableAPIInternal: NSObject, PushTrackerProtocol, AuthProvider {
425439
IterableUtil.isNotNullOrEmpty(string: _email) || IterableUtil.isNotNullOrEmpty(string: _userId)
426440
}
427441

442+
private func requestNewAuthToken() {
443+
authManager.requestNewAuthToken(hasFailedPriorAuth: false, onSuccess: { [weak self] authToken in
444+
_ = self?.inAppManager.scheduleSync()
445+
})
446+
}
447+
428448
private func logoutPreviousUser() {
429449
ITBInfo()
430450

@@ -439,10 +459,10 @@ final class IterableAPIInternal: NSObject, PushTrackerProtocol, AuthProvider {
439459
_email = nil
440460
_userId = nil
441461

442-
authManager.logoutUser()
443-
444462
storeIdentifierData()
445463

464+
authManager.logoutUser()
465+
446466
_ = inAppManager.reset()
447467

448468
try? requestHandler.handleLogout()

tests/swift-sdk-swift-tests/AuthTests.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,55 @@ class AuthTests: XCTestCase {
702702
wait(for: [condition2], timeout: 3)
703703
}
704704

705+
func testAuthTokenRequestingForAlreadyExistingEmail() {
706+
let condition1 = expectation(description: "auth handler didn't get called")
707+
708+
let localStorage = MockLocalStorage()
709+
localStorage.email = AuthTests.email
710+
711+
let authDelegate = createAuthDelegate({ handler in
712+
condition1.fulfill()
713+
})
714+
715+
let config = IterableConfig()
716+
config.authDelegate = authDelegate
717+
718+
let internalAPI = IterableAPIInternal.initializeForTesting(config: config,
719+
localStorage: localStorage)
720+
721+
XCTAssertNotNil(internalAPI.email)
722+
XCTAssertNil(internalAPI.authManager.getAuthToken())
723+
724+
internalAPI.email = AuthTests.email
725+
726+
wait(for: [condition1], timeout: testExpectationTimeout)
727+
}
728+
729+
func testLoggedOutAuthTokenRequest() {
730+
let condition1 = expectation(description: "auth handler was called")
731+
condition1.isInverted = true
732+
733+
let localStorage = MockLocalStorage()
734+
localStorage.email = AuthTests.email
735+
736+
let authDelegate = createAuthDelegate({ handler in
737+
condition1.fulfill()
738+
})
739+
740+
let config = IterableConfig()
741+
config.authDelegate = authDelegate
742+
743+
let internalAPI = IterableAPIInternal.initializeForTesting(config: config,
744+
localStorage: localStorage)
745+
746+
XCTAssertNotNil(internalAPI.email)
747+
XCTAssertNil(internalAPI.authManager.getAuthToken())
748+
749+
internalAPI.logoutUser()
750+
751+
wait(for: [condition1], timeout: testExpectationTimeoutForInverted)
752+
}
753+
705754
// MARK: - Private
706755

707756
class DefaultAuthDelegate: IterableAuthDelegate {

0 commit comments

Comments
 (0)