Skip to content

Commit a6f3699

Browse files
committed
Switch to save unsupported list
1 parent 26ecbf3 commit a6f3699

File tree

5 files changed

+38
-20
lines changed

5 files changed

+38
-20
lines changed

Modules/Sources/NetworkingCore/ApplicationPassword/RequestAuthenticator.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ protocol RequestAuthenticator {
1010
///
1111
var credentials: Credentials? { get }
1212

13+
var jetpackSiteID: Int64? { get }
14+
1315
/// Authenticates the provided urlRequest using the `credentials`
1416
///
1517
/// - Parameter urlRequest: `URLRequest` to authenticate
@@ -37,6 +39,10 @@ public struct DefaultRequestAuthenticator: RequestAuthenticator {
3739
///
3840
let credentials: Credentials?
3941

42+
/// ID of current site if Jetpack site
43+
///
44+
let jetpackSiteID: Int64?
45+
4046
/// The use case to handle authentication with application passwords.
4147
///
4248
private let applicationPasswordUseCase: ApplicationPasswordUseCase?
@@ -86,6 +92,8 @@ public struct DefaultRequestAuthenticator: RequestAuthenticator {
8692
return selectedSite?.siteAddress
8793
}
8894
}()
95+
96+
jetpackSiteID = selectedSite?.siteID
8997
}
9098

9199
/// Authenticates the provided urlRequest using the `credentials`

Modules/Sources/NetworkingCore/ApplicationPassword/RequestProcessor.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Alamofire
22
import Foundation
33

44
protocol RequestProcessorDelegate: AnyObject {
5-
func didFailToAuthenticateRequestWithApplicationPassword()
5+
func didFailToAuthenticateRequestWithApplicationPassword(siteID: Int64)
66
}
77

88
/// Authenticates and retries requests
@@ -18,6 +18,8 @@ final class RequestProcessor: RequestInterceptor {
1818

1919
private var isAuthenticatedWithWPCom = false
2020

21+
private var currentSiteID: Int64?
22+
2123
weak var delegate: RequestProcessorDelegate?
2224

2325
init(requestAuthenticator: RequestAuthenticator,
@@ -34,6 +36,7 @@ final class RequestProcessor: RequestInterceptor {
3436
default: false
3537
}
3638
}()
39+
currentSiteID = authenticator.jetpackSiteID
3740
}
3841
}
3942

@@ -85,8 +88,8 @@ private extension RequestProcessor {
8588
// Post a notification for tracking
8689
notificationCenter.post(name: .ApplicationPasswordsGenerationFailed, object: error, userInfo: nil)
8790

88-
if isAuthenticatedWithWPCom {
89-
await retryPasswordGenerationIfNeeded(with: error)
91+
if isAuthenticatedWithWPCom, let currentSiteID {
92+
await retryPasswordGenerationIfNeeded(error: error, siteID: currentSiteID)
9093
} else {
9194
isAuthenticating = false
9295
completeRequests(false)
@@ -96,7 +99,7 @@ private extension RequestProcessor {
9699
}
97100

98101
@MainActor
99-
func retryPasswordGenerationIfNeeded(with error: Error) async {
102+
func retryPasswordGenerationIfNeeded(error: Error, siteID: Int64) async {
100103
defer {
101104
isAuthenticating = false
102105
}
@@ -112,7 +115,7 @@ private extension RequestProcessor {
112115
case AFError.responseValidationFailed(reason: .unacceptableStatusCode(code: 404)):
113116
/// Site doesn't support application password
114117
completeRequests(false)
115-
delegate?.didFailToAuthenticateRequestWithApplicationPassword()
118+
delegate?.didFailToAuthenticateRequestWithApplicationPassword(siteID: siteID)
116119
default:
117120
completeRequests(false)
118121
}

Modules/Sources/NetworkingCore/Network/AlamofireNetwork.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public class AlamofireNetwork: Network {
7979

8080
public func updateAppPasswordSwitching(enabled: Bool) {
8181
guard let credentials, case .wpcom = credentials else { return }
82-
if enabled, let selectedSite, !userDefaults.applicationPasswordUnsupported {
82+
if enabled, let selectedSite {
8383
observeSelectedSite(selectedSite)
8484
} else {
8585
requestConverter = RequestConverter(siteAddress: nil)
@@ -198,10 +198,12 @@ private extension AlamofireNetwork {
198198
func observeSelectedSite(_ selectedSite: AnyPublisher<JetpackSite?, Never>) {
199199
subscription = selectedSite
200200
.removeDuplicates()
201-
.combineLatest(userDefaults.publisher(for: \.applicationPasswordUnsupported))
202-
.sink { [weak self] site, appPasswordUnsupported in
201+
.combineLatest(userDefaults.publisher(for: \.applicationPasswordUnsupportedList))
202+
.print("")
203+
.sink { [weak self] site, unsupportedList in
203204
guard let self else { return }
204-
guard let site, site.applicationPasswordAvailable, !appPasswordUnsupported else {
205+
guard let site, site.applicationPasswordAvailable,
206+
unsupportedList.contains(site.siteID) == false else {
205207
requestConverter = RequestConverter(siteAddress: nil)
206208
requestAuthenticator.updateAuthenticator(DefaultRequestAuthenticator(credentials: credentials))
207209
requestAuthenticator.delegate = nil
@@ -221,8 +223,9 @@ private extension AlamofireNetwork {
221223
// MARK: `RequestProcessorDelegate` conformance
222224
//
223225
extension AlamofireNetwork: RequestProcessorDelegate {
224-
func didFailToAuthenticateRequestWithApplicationPassword() {
225-
userDefaults.applicationPasswordUnsupported = false
226+
func didFailToAuthenticateRequestWithApplicationPassword(siteID: Int64) {
227+
let currentList = userDefaults.applicationPasswordUnsupportedList
228+
userDefaults.applicationPasswordUnsupportedList = currentList + [siteID]
226229
}
227230
}
228231

@@ -271,12 +274,12 @@ extension Alamofire.DataResponse {
271274
// MARK: - Helper extension to save internal flag for app password availability
272275
//
273276
extension UserDefaults {
274-
@objc dynamic var applicationPasswordUnsupported: Bool {
275-
get { bool(forKey: Key.applicationPasswordUnsupported.rawValue) }
276-
set { setValue(newValue, forKey: Key.applicationPasswordUnsupported.rawValue) }
277+
@objc dynamic var applicationPasswordUnsupportedList: [Int64] {
278+
get { value(forKey: Key.applicationPasswordUnsupportedList.rawValue) as? [Int64] ?? [] }
279+
set { setValue(newValue, forKey: Key.applicationPasswordUnsupportedList.rawValue) }
277280
}
278281

279282
enum Key: String {
280-
case applicationPasswordUnsupported
283+
case applicationPasswordUnsupportedList
281284
}
282285
}

Modules/Tests/NetworkingTests/ApplicationPassword/RequestProcessorTests.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ final class RequestProcessorTests: XCTestCase {
302302
let request = try mockRequest()
303303
let wpcomCredentials = Networking.Credentials.wpcom(username: "test", authToken: "token", siteAddress: "test.com")
304304
mockRequestAuthenticator.credentials = wpcomCredentials
305+
mockRequestAuthenticator.jetpackSiteID = 123
305306
let mockDelegate = MockRequestProcessorDelegate()
306307
sut.updateAuthenticator(mockRequestAuthenticator)
307308
sut.delegate = mockDelegate
@@ -320,7 +321,7 @@ final class RequestProcessorTests: XCTestCase {
320321
// Then
321322
XCTAssertFalse(shouldRetry.retryRequired)
322323
waitUntil {
323-
mockDelegate.didFailToAuthenticateRequestWithApplicationPasswordCalled
324+
mockDelegate.didFailToAuthenticateRequestForSiteID == 123
324325
}
325326
}
326327

@@ -399,6 +400,7 @@ private class MockRequestAuthenticator: RequestAuthenticator {
399400
private(set) var deleteApplicationPasswordCalled = false
400401

401402
var credentials: Networking.Credentials? = nil
403+
var jetpackSiteID: Int64?
402404

403405
var mockErrorWhileGeneratingPassword: Error?
404406
var mockErrorWhileDeletingPassword: Error?
@@ -450,9 +452,9 @@ private class MockRequest: Alamofire.DataRequest, @unchecked Sendable {
450452
}
451453

452454
private class MockRequestProcessorDelegate: RequestProcessorDelegate {
453-
private(set) var didFailToAuthenticateRequestWithApplicationPasswordCalled = false
455+
private(set) var didFailToAuthenticateRequestForSiteID: Int64?
454456

455-
func didFailToAuthenticateRequestWithApplicationPassword() {
456-
didFailToAuthenticateRequestWithApplicationPasswordCalled = true
457+
func didFailToAuthenticateRequestWithApplicationPassword(siteID: Int64) {
458+
didFailToAuthenticateRequestForSiteID = siteID
457459
}
458460
}

WooCommerce/WooCommerceTests/Yosemite/StoresManagerTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,9 @@ final class StoresManagerTests: XCTestCase {
451451
MockNotificationCenter.testingInstance.post(name: .ApplicationPasswordsGenerationFailed, object: error, userInfo: nil)
452452

453453
// Assert
454-
XCTAssertFalse(manager.isAuthenticated)
454+
waitUntil {
455+
manager.isAuthenticated == false
456+
}
455457
XCTAssertEqual(isLoggedInValues, [false, true, false])
456458
}
457459

0 commit comments

Comments
 (0)