@@ -2,6 +2,10 @@ import Foundation
22import enum Alamofire. AFError
33import KeychainAccess
44
5+ #if canImport(UIKit)
6+ import UIKit
7+ #endif
8+
59public 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-
5136final 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)
0 commit comments