Skip to content

Commit

Permalink
* Provide both cipherKey: and cryptorModule: for the PNConfig constru…
Browse files Browse the repository at this point in the history
…ctor
  • Loading branch information
jguz-pubnub committed Oct 6, 2023
1 parent facc3c4 commit bdc2c1e
Showing 1 changed file with 12 additions and 79 deletions.
91 changes: 12 additions & 79 deletions Sources/PubNub/PubNubConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -242,7 +176,7 @@ public struct PubNubConfiguration: Hashable {
publishKey: publishKey,
subscribeKey: subscribeKey,
userId: uuid,
cryptorModule: cryptorModule,
cipherKey: cipherKey,
authKey: authKey,
authToken: authToken,
useSecureConnections: useSecureConnections,
Expand All @@ -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
Expand Down

0 comments on commit bdc2c1e

Please sign in to comment.