Skip to content

Commit 3df4166

Browse files
authored
Login: Fix failure to generate application password for site credential login (#16004)
2 parents df36749 + 8b852d3 commit 3df4166

File tree

5 files changed

+15
-52
lines changed

5 files changed

+15
-52
lines changed

Modules/Sources/NetworkingCore/ApplicationPassword/ApplicationPasswordUseCase.swift

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import Foundation
22
import enum Alamofire.AFError
33
import KeychainAccess
44

5+
#if canImport(UIKit)
6+
import UIKit
7+
#endif
8+
59
public enum ApplicationPasswordUseCaseError: Error {
610
case duplicateName
711
case applicationPasswordsDisabled
@@ -29,25 +33,6 @@ public protocol ApplicationPasswordUseCase {
2933
func deletePassword() async throws
3034
}
3135

32-
/// A wrapper for the `UIDevice` `model` and `identifierForVendor` properties.
33-
///
34-
/// This is necessary because `UIDevice` is part of UIKit which we cannot use when targeting watchOS.
35-
/// So, to keep this package compatible with watchOS, we need to abstract UIKit away and delegate it to the consumers to provide us
36-
/// with the device information.
37-
///
38-
/// This approach is feasible because only the `applicationPasswordName` method in
39-
/// `DefaultApplicationPasswordUseCase` needs access to the information and watchOS does not need to create application
40-
/// passwords. We can therefore pass a `nil` value to it to satisfy the compilation without issues for the user experience.
41-
public struct DeviceModelIdentifierInfo {
42-
let model: String
43-
let identifierForVendor: String
44-
45-
public init(model: String, identifierForVendor: String) {
46-
self.model = model
47-
self.identifierForVendor = identifierForVendor
48-
}
49-
}
50-
5136
final public class DefaultApplicationPasswordUseCase: ApplicationPasswordUseCase {
5237
/// Site Address
5338
///
@@ -65,31 +50,27 @@ final public class DefaultApplicationPasswordUseCase: ApplicationPasswordUseCase
6550
///
6651
private let storage: ApplicationPasswordStorage
6752

68-
private let deviceModelIdentifierInfo: DeviceModelIdentifierInfo?
69-
7053
/// Used to name the password in wpadmin.
7154
///
7255
private var applicationPasswordName: String {
73-
get {
74-
guard let deviceModelIdentifierInfo else {
75-
return "" // This is not needed on watchOS as the watch does not create application passwords.
76-
}
77-
78-
let bundleIdentifier = Bundle.main.bundleIdentifier ?? "Unknown"
79-
return "\(bundleIdentifier).ios-app-client.\(deviceModelIdentifierInfo.model).\(deviceModelIdentifierInfo.identifierForVendor)"
80-
}
56+
#if !os(watchOS)
57+
let bundleIdentifier = Bundle.main.bundleIdentifier ?? "Unknown"
58+
let model = UIDevice.current.model
59+
let identifierForVendor = UIDevice.current.identifierForVendor?.uuidString ?? ""
60+
return "\(bundleIdentifier).ios-app-client.\(model).\(identifierForVendor)"
61+
#else
62+
fatalError("Unexpected error: Application password should not be generated through watch app")
63+
#endif
8164
}
8265

8366
public init(username: String,
8467
password: String,
8568
siteAddress: String,
86-
deviceModelIdentifierInfo: DeviceModelIdentifierInfo? = nil,
8769
network: Network? = nil,
8870
keychain: Keychain = Keychain(service: WooConstants.keychainServiceName)) throws {
8971
self.siteAddress = siteAddress
9072
self.username = username
9173
self.storage = ApplicationPasswordStorage(keychain: keychain)
92-
self.deviceModelIdentifierInfo = deviceModelIdentifierInfo
9374

9475
if let network {
9576
self.network = network
@@ -154,7 +135,7 @@ final public class DefaultApplicationPasswordUseCase: ApplicationPasswordUseCase
154135
if let uuidFromLocalPassword {
155136
return uuidFromLocalPassword
156137
} else {
157-
return try await self.fetchUUIDForApplicationPassword(await applicationPasswordName)
138+
return try await self.fetchUUIDForApplicationPassword(applicationPasswordName)
158139
}
159140
}()
160141
try await deleteApplicationPassword(uuidToBeDeleted)
@@ -167,7 +148,7 @@ private extension DefaultApplicationPasswordUseCase {
167148
/// - Returns: Generated `ApplicationPassword`
168149
///
169150
func createApplicationPassword() async throws -> ApplicationPassword {
170-
let passwordName = await applicationPasswordName
151+
let passwordName = applicationPasswordName
171152

172153
let parameters = [ParameterKey.name: passwordName]
173154
let request = RESTRequest(siteURL: siteAddress, method: .post, path: Path.applicationPasswords, parameters: parameters)

WooCommerce/Classes/Authentication/AuthenticationManager.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,8 +752,7 @@ private extension AuthenticationManager {
752752
guard let useCase = try? DefaultApplicationPasswordUseCase(
753753
username: siteCredentials.username,
754754
password: siteCredentials.password,
755-
siteAddress: siteCredentials.siteURL,
756-
deviceModelIdentifierInfo: UIDevice.current.deviceModelIdentifierInfo
755+
siteAddress: siteCredentials.siteURL
757756
) else {
758757
return assertionFailure("⛔️ Error creating application password use case")
759758
}

WooCommerce/Classes/System/SessionManager.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ final class SessionManager: SessionManagerProtocol {
234234
return try? DefaultApplicationPasswordUseCase(username: username,
235235
password: password,
236236
siteAddress: siteAddress,
237-
deviceModelIdentifierInfo: UIDevice.current.deviceModelIdentifierInfo,
238237
keychain: keychain)
239238
case let .applicationPassword(_, _, siteAddress):
240239
return OneTimeApplicationPasswordUseCase(siteAddress: siteAddress, keychain: keychain)

WooCommerce/Classes/System/UIDevice+DeviceModelIdentifierInfo.swift

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

WooCommerce/WooCommerce.xcodeproj/project.pbxproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,6 @@
13001300
3F58701F281B947E004F7556 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 3F58701E281B947E004F7556 /* Main.storyboard */; };
13011301
3F587021281B9494004F7556 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 3F587020281B9494004F7556 /* LaunchScreen.storyboard */; };
13021302
3F587026281B9C19004F7556 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 3F587028281B9C19004F7556 /* InfoPlist.strings */; };
1303-
3F88EC3F2DF8D4BC0023A6F4 /* UIDevice+DeviceModelIdentifierInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F88EC3E2DF8D4BC0023A6F4 /* UIDevice+DeviceModelIdentifierInfo.swift */; };
13041303
4506BD712461965300FE6377 /* ProductVisibilityViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4506BD6F2461965300FE6377 /* ProductVisibilityViewController.swift */; };
13051304
4506BD722461965300FE6377 /* ProductVisibilityViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4506BD702461965300FE6377 /* ProductVisibilityViewController.xib */; };
13061305
4508BF622660E34A00707869 /* ShippingLabelCarrierAndRatesTopBanner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4508BF612660E34A00707869 /* ShippingLabelCarrierAndRatesTopBanner.swift */; };
@@ -4459,7 +4458,6 @@
44594458
3F587037281B9C50004F7556 /* es */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = es; path = es.lproj/InfoPlist.strings; sourceTree = "<group>"; };
44604459
3F587038281B9C52004F7556 /* sv */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = sv; path = sv.lproj/InfoPlist.strings; sourceTree = "<group>"; };
44614460
3F64F76C2C06A3A50085DEEF /* WooCommerce.release-alpha.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "WooCommerce.release-alpha.xcconfig"; sourceTree = "<group>"; };
4462-
3F88EC3E2DF8D4BC0023A6F4 /* UIDevice+DeviceModelIdentifierInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIDevice+DeviceModelIdentifierInfo.swift"; sourceTree = "<group>"; };
44634461
3FF314EF26FC784A0012E68E /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Platforms/iPhoneOS.platform/Developer/Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
44644462
4506BD6F2461965300FE6377 /* ProductVisibilityViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductVisibilityViewController.swift; sourceTree = "<group>"; };
44654463
4506BD702461965300FE6377 /* ProductVisibilityViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ProductVisibilityViewController.xib; sourceTree = "<group>"; };
@@ -11027,7 +11025,6 @@
1102711025
DE6906E627D74A1900735E3B /* WooSplitViewController.swift */,
1102811026
26A7C8782BE91F3D00382627 /* WatchDependenciesSynchronizer.swift */,
1102911027
26B249702BEC801400730730 /* WatchDependencies.swift */,
11030-
3F88EC3E2DF8D4BC0023A6F4 /* UIDevice+DeviceModelIdentifierInfo.swift */,
1103111028
);
1103211029
path = System;
1103311030
sourceTree = "<group>";
@@ -16209,7 +16206,6 @@
1620916206
205B7ECD2C19FD2F00D14A36 /* PointOfSaleCardPresentPaymentDisplayReaderMessageMessageViewModel.swift in Sources */,
1621016207
2DB877522E25466C0001B175 /* ShippingItemRowAccessibility.swift in Sources */,
1621116208
DEF8CF1F29AC870A00800A60 /* WPComEmailLoginViewModel.swift in Sources */,
16212-
3F88EC3F2DF8D4BC0023A6F4 /* UIDevice+DeviceModelIdentifierInfo.swift in Sources */,
1621316209
74A33D8021C3F234009E25DE /* LicensesViewController.swift in Sources */,
1621416210
454453CA27566CDE00464AC5 /* HubMenuViewModel.swift in Sources */,
1621516211
934CB123224EAB150005CCB9 /* main.swift in Sources */,

0 commit comments

Comments
 (0)