Skip to content

Commit f6eb619

Browse files
committed
Moves 'PusherChannel' encryption helpers to extension methods
- Also renames the methods for best practices and readability
1 parent fd38e6e commit f6eb619

File tree

6 files changed

+35
-24
lines changed

6 files changed

+35
-24
lines changed

PusherSwift.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
53AF00C525D2AE760005621D /* EventFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53AF00C425D2AE760005621D /* EventFactory.swift */; };
5858
53AF00C925D2AEC70005621D /* EventQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53AF00C825D2AEC70005621D /* EventQueue.swift */; };
5959
53AF00D125D2B02D0005621D /* EventQueueDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53AF00D025D2B02D0005621D /* EventQueueDelegate.swift */; };
60+
53F08C4825ECEFFD00BDFFB3 /* PusherChannel+EncryptionHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53F08C4725ECEFFD00BDFFB3 /* PusherChannel+EncryptionHelpers.swift */; };
6061
736E53F5242A378B0052CC1B /* String+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 736E53F3242A35D90052CC1B /* String+Extensions.swift */; };
6162
736E53F7242A45AC0052CC1B /* XCTest+Assertions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 736E53F6242A45AC0052CC1B /* XCTest+Assertions.swift */; };
6263
E2498293231E612700CFBBD6 /* PusherError.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2498292231E612700CFBBD6 /* PusherError.swift */; };
@@ -134,6 +135,7 @@
134135
53AF00C425D2AE760005621D /* EventFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventFactory.swift; sourceTree = "<group>"; };
135136
53AF00C825D2AEC70005621D /* EventQueue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventQueue.swift; sourceTree = "<group>"; };
136137
53AF00D025D2B02D0005621D /* EventQueueDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventQueueDelegate.swift; sourceTree = "<group>"; };
138+
53F08C4725ECEFFD00BDFFB3 /* PusherChannel+EncryptionHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PusherChannel+EncryptionHelpers.swift"; sourceTree = "<group>"; };
137139
736E53F3242A35D90052CC1B /* String+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Extensions.swift"; sourceTree = "<group>"; };
138140
736E53F6242A45AC0052CC1B /* XCTest+Assertions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "XCTest+Assertions.swift"; sourceTree = "<group>"; };
139141
73D8A22C2435F381001FDE05 /* ChannelEventFactory+DecryptionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ChannelEventFactory+DecryptionTests.swift"; sourceTree = "<group>"; };
@@ -277,6 +279,7 @@
277279
5333ACFC24F80F5C006E8DF0 /* Extensions */ = {
278280
isa = PBXGroup;
279281
children = (
282+
53F08C4725ECEFFD00BDFFB3 /* PusherChannel+EncryptionHelpers.swift */,
280283
E2B21F0C243F5DC10049A35B /* PusherConnection+WebsocketDelegate.swift */,
281284
533CA05825D42FE400043763 /* URL+Pusher.swift */,
282285
);
@@ -587,6 +590,7 @@
587590
3390D1E81F054D1E00E1944D /* Authorizer.swift in Sources */,
588591
3389F5721CAEDDF300563F49 /* PusherChannels.swift in Sources */,
589592
E2CFE43122D79CA7004187C3 /* EventParser.swift in Sources */,
593+
53F08C4825ECEFFD00BDFFB3 /* PusherChannel+EncryptionHelpers.swift in Sources */,
590594
53A9AFCD25CDAFD8008B1736 /* EventError.swift in Sources */,
591595
3389F5761CAEDE2800563F49 /* PusherGlobalChannel.swift in Sources */,
592596
E2B21F01243F5B1E0049A35B /* ChannelEventFactory.swift in Sources */,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Foundation
2+
3+
extension PusherChannel {
4+
5+
/// Determines whether or not a message should be decrypted, based on channel and event attributes.
6+
/// - Parameters:
7+
/// - name: The name of the channel.
8+
/// - eventName: The name of the event received on the channel.
9+
/// - Returns: A `Bool` indicating whether the message should be decrypted or not.
10+
static func decryptsMessage(name: String?, eventName: String) -> Bool {
11+
return isEncrypted(name: name) && !isSystemEvent(eventName: eventName)
12+
}
13+
14+
/// Determines if a channel is a private encrypted or not, based on its name.
15+
/// - Parameter name: The name of the channel.
16+
/// - Returns: A `Bool` indicating whether the channel is encrypted or not.
17+
static func isEncrypted(name: String?) -> Bool {
18+
return name?.starts(with: "\(Constants.ChannelTypes.privateEncrypted)-") ?? false
19+
}
20+
21+
/// Determines if an event is a system event or not, based on its name.
22+
/// - Parameter eventName: The name of the event.
23+
/// - Returns: A `Bool` indicating whether the event is a system event or not.
24+
private static func isSystemEvent(eventName: String) -> Bool {
25+
return eventName.starts(with: "\(Constants.EventTypes.pusher):")
26+
|| eventName.starts(with: "\(Constants.EventTypes.pusherInternal):")
27+
}
28+
}

Sources/Helpers/Crypto.swift

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,6 @@ struct Crypto {
5555
return decryptedString
5656
}
5757

58-
/// Determines whether or not a message should be decrypted, based on event and channel attributes.
59-
/// - Parameters:
60-
/// - eventName: The name of the event.
61-
/// - channelName: The name of the channel associated with the event.
62-
/// - Returns: A `Bool` indicating whether the message should be decrypted or not.
63-
public static func shouldDecryptMessage(eventName: String, channelName: String?) -> Bool {
64-
return isEncryptedChannel(channelName: channelName) && !isPusherSystemEvent(eventName: eventName)
65-
}
66-
67-
/// Determines if a data sent over a channel are encrypted or not.
68-
/// - Parameter channelName: The name of the channel.
69-
/// - Returns: A `Bool` indicating whether the channel is encrypted or not.
70-
public static func isEncryptedChannel(channelName: String?) -> Bool {
71-
return channelName?.starts(with: "\(Constants.ChannelTypes.privateEncrypted)-") ?? false
72-
}
73-
7458
// MARK: - Private methods
7559

7660
private static func encryptedData(fromData data: String) throws -> EncryptedData {
@@ -105,9 +89,4 @@ struct Crypto {
10589

10690
return decodedDecryptionKey
10791
}
108-
109-
private static func isPusherSystemEvent(eventName: String) -> Bool {
110-
return eventName.starts(with: "\(Constants.EventTypes.pusher):")
111-
|| eventName.starts(with: "\(Constants.EventTypes.pusherInternal):")
112-
}
11392
}

Sources/Models/PusherChannel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ open class PusherChannel: NSObject {
164164
- parameter data: The data to be sent as the message payload
165165
*/
166166
open func trigger(eventName: String, data: Any) {
167-
if Crypto.isEncryptedChannel(channelName: self.name) {
167+
if PusherChannel.isEncrypted(name: self.name) {
168168
let context = "'\(self.name)'. Client event '\(eventName)' will not be sent"
169169
Logger.shared.error(for: .clientEventsNotSupported,
170170
context: context)

Sources/PusherSwift.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ let CLIENT_NAME = "pusher-websocket-swift"
5454
onMemberRemoved: ((PusherPresenceChannelMember) -> Void)? = nil
5555
) -> PusherChannel {
5656

57-
let isEncryptedChannel = Crypto.isEncryptedChannel(channelName: channelName)
57+
let isEncryptedChannel = PusherChannel.isEncrypted(name: channelName)
5858

5959
if isEncryptedChannel && auth != nil {
6060
Logger.shared.warning(for: .authValueOnSubscriptionNotSupported)

Sources/Services/ChannelEventFactory.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct ChannelEventFactory: EventFactory {
3131
decryptionKey: String?) throws -> String? {
3232
let data = json[Constants.JSONKeys.data] as? String
3333

34-
if Crypto.shouldDecryptMessage(eventName: eventName, channelName: channelName) {
34+
if PusherChannel.decryptsMessage(name: channelName, eventName: eventName) {
3535
return try Crypto.decrypt(data: data, decryptionKey: decryptionKey)
3636
} else {
3737
return data

0 commit comments

Comments
 (0)