Skip to content

Commit cd124c0

Browse files
authored
Merge pull request #397 from Iterable/MOB-2175-pending-auth-flag
[MOB-2175] pending auth flag
2 parents 6ff4079 + c9a9e77 commit cd124c0

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

swift-sdk/Internal/AuthManager.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,18 @@ 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: ((String?) -> Void)? = nil) {
46+
guard !pendingAuth else {
47+
return
48+
}
49+
4650
guard !self.hasFailedPriorAuth || !hasFailedPriorAuth else {
4751
return
4852
}
4953

5054
self.hasFailedPriorAuth = hasFailedPriorAuth
5155

56+
pendingAuth = true
57+
5258
delegate?.onAuthTokenRequested { [weak self] retrievedAuthToken in
5359
self?.onAuthTokenReceived(retrievedAuthToken: retrievedAuthToken, onSuccess: onSuccess)
5460
}
@@ -59,8 +65,7 @@ class AuthManager: IterableInternalAuthManagerProtocol {
5965

6066
storeAuthToken()
6167

62-
expirationRefreshTimer?.invalidate()
63-
expirationRefreshTimer = nil
68+
clearRefreshTimer()
6469
}
6570

6671
// MARK: - Private/Internal
@@ -69,6 +74,7 @@ class AuthManager: IterableInternalAuthManagerProtocol {
6974

7075
private var authToken: String?
7176

77+
private var pendingAuth: Bool = false
7278
private var hasFailedPriorAuth: Bool = false
7379

7480
private weak var delegate: IterableAuthDelegate?
@@ -87,6 +93,8 @@ class AuthManager: IterableInternalAuthManagerProtocol {
8793
}
8894

8995
private func onAuthTokenReceived(retrievedAuthToken: String?, onSuccess: ((String?) -> Void)?) {
96+
pendingAuth = false
97+
9098
authToken = retrievedAuthToken
9199

92100
storeAuthToken()
@@ -119,6 +127,11 @@ class AuthManager: IterableInternalAuthManagerProtocol {
119127
}
120128
}
121129

130+
private func clearRefreshTimer() {
131+
expirationRefreshTimer?.invalidate()
132+
expirationRefreshTimer = nil
133+
}
134+
122135
static func decodeExpirationDateFromAuthToken(_ authToken: String) -> Int? {
123136
let components = authToken.components(separatedBy: ".")
124137

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ class AuthTests: XCTestCase {
614614
onSuccess: { token in
615615
XCTAssertEqual(token, AuthTests.authToken)
616616
condition1.fulfill()
617-
})
617+
})
618618

619619
wait(for: [condition1], timeout: testExpectationTimeout)
620620
}
@@ -665,6 +665,44 @@ class AuthTests: XCTestCase {
665665
wait(for: [condition2], timeout: testExpectationTimeoutForInverted)
666666
}
667667

668+
func testRefreshTimerQueueRejection() {
669+
let condition1 = expectation(description: "\(#function) - first refresh timer never happened called")
670+
let condition2 = expectation(description: "\(#function) - second refresh timer should not have been called")
671+
condition2.isInverted = true
672+
673+
class AsyncAuthDelegate: IterableAuthDelegate {
674+
func onAuthTokenRequested(completion: @escaping AuthTokenRetrievalHandler) {
675+
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
676+
completion(AuthTests.authToken)
677+
}
678+
}
679+
}
680+
681+
let authDelegate = AsyncAuthDelegate()
682+
683+
let config = IterableConfig()
684+
config.authDelegate = authDelegate
685+
686+
let authManager = AuthManager(delegate: config.authDelegate,
687+
expirationRefreshPeriod: config.expiringAuthTokenRefreshPeriod,
688+
localStorage: MockLocalStorage(),
689+
dateProvider: MockDateProvider())
690+
691+
authManager.requestNewAuthToken(hasFailedPriorAuth: false,
692+
onSuccess: { token in
693+
XCTAssertEqual(token, AuthTests.authToken)
694+
condition1.fulfill()
695+
})
696+
697+
authManager.requestNewAuthToken(hasFailedPriorAuth: false,
698+
onSuccess: { token in
699+
condition2.fulfill()
700+
})
701+
702+
wait(for: [condition1], timeout: testExpectationTimeout)
703+
wait(for: [condition2], timeout: 3)
704+
}
705+
668706
// MARK: - Private
669707

670708
class DefaultAuthDelegate: IterableAuthDelegate {

0 commit comments

Comments
 (0)