Skip to content

Commit 4c8766e

Browse files
committed
Fix issue with application password name being empty
1 parent df36749 commit 4c8766e

File tree

3 files changed

+15
-36
lines changed

3 files changed

+15
-36
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)

0 commit comments

Comments
 (0)