From bdc2c1ef1f17d6d3ea682ac6140d69bf5229746f Mon Sep 17 00:00:00 2001 From: jguz-pubnub Date: Fri, 6 Oct 2023 12:16:42 +0200 Subject: [PATCH] * Provide both cipherKey: and cryptorModule: for the PNConfig constructor --- Sources/PubNub/PubNubConfiguration.swift | 91 ++++-------------------- 1 file changed, 12 insertions(+), 79 deletions(-) diff --git a/Sources/PubNub/PubNubConfiguration.swift b/Sources/PubNub/PubNubConfiguration.swift index ef3d8aa8..6eda4383 100644 --- a/Sources/PubNub/PubNubConfiguration.swift +++ b/Sources/PubNub/PubNubConfiguration.swift @@ -74,6 +74,7 @@ public struct PubNubConfiguration: Hashable { /// - publishKey: The PubNub Publish Key to be used when publishing data to a channel /// - subscribeKey: The PubNub Subscribe Key to be used when getting data from a channel /// - userId: The unique identifier to be used as a device identifier + /// - cipherKey: ~~If set, all communication will be encrypted with this key~~ /// - cryptorModule: If set, all communication will be encrypted with this module /// - authKey: If Access Manager (PAM) is enabled, client will use `authToken` instead of `authKey` on all requests /// - authToken: If Access Manager (PAM) is enabled, client will use `authToken` instead of `authKey` on all requests @@ -92,7 +93,8 @@ public struct PubNubConfiguration: Hashable { publishKey: String?, subscribeKey: String, userId: String, - cryptorModule: CryptorModule?, + cipherKey: Crypto? = nil, + cryptorModule: CryptorModule? = nil, authKey: String? = nil, authToken: String? = nil, useSecureConnections: Bool = true, @@ -110,10 +112,16 @@ public struct PubNubConfiguration: Hashable { guard userId.trimmingCharacters(in: .whitespacesAndNewlines).count > 0 else { preconditionFailure("UserId should not be empty.") } + if let cryptorModule = cryptorModule { + self.cryptorModule = cryptorModule + } else { + if let cipherKey = cipherKey { + self.cryptorModule = CryptorModule.legacyCryptoModule(with: cipherKey.key, withRandomIV: cipherKey.randomizeIV) + } + } self.publishKey = publishKey self.subscribeKey = subscribeKey - self.cryptorModule = cryptorModule self.authKey = authKey self.authToken = authToken self.userId = userId @@ -130,80 +138,6 @@ public struct PubNubConfiguration: Hashable { self.filterExpression = filterExpression } - // swiftlint:disable:next line_length - @available(*, deprecated, renamed: "init(publishKey:subscribeKey:userId:cryptorModule:authKey:authToken:useSecureConnections:origin:useInstanceId:useRequestId:automaticRetry:urlSessionConfiguration:urlSessionConfiguration:durationUntilTimeout:heartbeatInterval:supressLeaveEvents:requestMessageCountThreshold:filterExpression:)", message: "Provide 'cryptorModule:' instead of 'cipherKey' for encrypted communication:") - /// Creates a configuration using the specified PubNub Publish and Subscribe Keys - /// - /// - Attention: It is recommended that you use this initializer only if you have a - /// custom way to pass your PubNub Publish and Subscribe keys without storing them - /// inside your source code/code repository. - /// - /// - Parameters: - /// - publishKey: The PubNub Publish Key to be used when publishing data to a channel - /// - subscribeKey: The PubNub Subscribe Key to be used when getting data from a channel - /// - userId: The unique identifier to be used as a device identifier - /// - cipherKey: If set, all communication will be encrypted with this key - /// - authKey: If Access Manager (PAM) is enabled, client will use `authToken` instead of `authKey` on all requests - /// - authToken: If Access Manager (PAM) is enabled, client will use `authToken` instead of `authKey` on all requests - /// - useSecureConnections: The PubNub Publish Key to be used when publishing data to a channel - /// - origin: Full origin (`subdomain`.`domain`) used for requests - /// - useInstanceId: Whether a PubNub object instanceId should be included on outgoing requests - /// - useRequestId: Whether a request identifier should be included on outgoing requests - /// - automaticRetry: Reconnection policy which will be used if/when a request fails - /// - urlSessionConfiguration: URLSessionConfiguration used for URLSession network events - /// - durationUntilTimeout: How long (in seconds) the server will consider the client alive for presence - /// - heartbeatInterval: How often (in seconds) the client will announce itself to server - /// - supressLeaveEvents: Whether to send out the leave requests - /// - requestMessageCountThreshold: The number of messages into the payload before emitting `RequestMessageCountExceeded` - /// - filterExpression: PSV2 feature to subscribe with a custom filter expression. - public init( - publishKey: String?, - subscribeKey: String, - userId: String, - cipherKey: Crypto? = nil, - authKey: String? = nil, - authToken: String? = nil, - useSecureConnections: Bool = true, - origin: String = "ps.pndsn.com", - useInstanceId: Bool = false, - useRequestId: Bool = false, - automaticRetry: AutomaticRetry? = nil, - urlSessionConfiguration: URLSessionConfiguration = .pubnub, - durationUntilTimeout: UInt = 300, - heartbeatInterval: UInt = 0, - supressLeaveEvents: Bool = false, - requestMessageCountThreshold: UInt = 100, - filterExpression: String? = nil - ) { - let cryptorModule: CryptorModule? - - if let cipherKey = cipherKey { - cryptorModule = CryptorModule.legacyCryptoModule(with: cipherKey.key, withRandomIV: cipherKey.randomizeIV) - } else { - cryptorModule = nil - } - - self.init( - publishKey: publishKey, - subscribeKey: subscribeKey, - userId: userId, - cryptorModule: cryptorModule, - authKey: authKey, - authToken: authToken, - useSecureConnections: useSecureConnections, - origin: origin, - useInstanceId: useInstanceId, - useRequestId: useRequestId, - automaticRetry: automaticRetry, - urlSessionConfiguration: urlSessionConfiguration, - durationUntilTimeout: durationUntilTimeout, - heartbeatInterval: heartbeatInterval, - supressLeaveEvents: supressLeaveEvents, - requestMessageCountThreshold: requestMessageCountThreshold, - filterExpression: filterExpression - ) - } - // swiftlint:disable:next line_length @available(*, deprecated, renamed: "init(publishKey:subscribeKey:userId:cipherKey:authKey:authToken:useSecureConnections:origin:useInstanceId:useRequestId:automaticRetry:urlSessionConfiguration:urlSessionConfiguration:durationUntilTimeout:heartbeatInterval:supressLeaveEvents:requestMessageCountThreshold:filterExpression:)") /// Creates a configuration using the specified PubNub Publish and Subscribe Keys @@ -219,7 +153,7 @@ public struct PubNubConfiguration: Hashable { publishKey: String?, subscribeKey: String, uuid: String, - cryptorModule: CryptorModule? = nil, + cipherKey: Crypto? = nil, authKey: String? = nil, authToken: String? = nil, useSecureConnections: Bool = true, @@ -242,7 +176,7 @@ public struct PubNubConfiguration: Hashable { publishKey: publishKey, subscribeKey: subscribeKey, userId: uuid, - cryptorModule: cryptorModule, + cipherKey: cipherKey, authKey: authKey, authToken: authToken, useSecureConnections: useSecureConnections, @@ -266,7 +200,6 @@ public struct PubNubConfiguration: Hashable { /// If set, all communication will be encrypted with this key public var cryptorModule: CryptorModule? /// If set, all communication will be encrypted with this key - /// - Warning: This parameter is deprecated. Use `cryptorModule` instead. @available(*, deprecated, message: "Use 'cryptorModule' instead") public var cipherKey: Crypto? /// If Access Manager (PAM) is enabled, client will use `authKey` on all requests