@@ -488,7 +488,7 @@ open class PusherConnection: NSObject {
488
488
} else if let eData = jsonObject [ " data " ] as? [ String : AnyObject ] {
489
489
globalChannel. handleErrorEvent ( name: eventName, data: eData)
490
490
}
491
- }
491
+ }
492
492
}
493
493
494
494
/**
@@ -506,68 +506,68 @@ open class PusherConnection: NSObject {
506
506
subscribeToNormalChannel ( channel)
507
507
return true
508
508
} else {
509
- if let socketID = self . socketId {
510
- switch self . options. authMethod {
511
- case . noMethod:
512
- let errorMessage = " Authentication method required for private / presence channels but none provided. "
513
- let error = NSError ( domain: " com.pusher.PusherSwift " , code: 0 , userInfo: [ NSLocalizedFailureReasonErrorKey: errorMessage] )
509
+ guard let socketId = self . socketId else {
510
+ print ( " socketId value not found. You may not be connected. " )
511
+ return false
512
+ }
514
513
515
- print ( errorMessage)
514
+ switch self . options. authMethod {
515
+ case . noMethod:
516
+ let errorMessage = " Authentication method required for private / presence channels but none provided. "
517
+ let error = NSError ( domain: " com.pusher.PusherSwift " , code: 0 , userInfo: [ NSLocalizedFailureReasonErrorKey: errorMessage] )
516
518
517
- handleAuthorizationError ( forChannel : channel . name , response : nil , data : nil , error : error )
519
+ print ( errorMessage )
518
520
519
- return false
520
- case . endpoint( authEndpoint: let authEndpoint) :
521
- let request = requestForAuthValue ( from: authEndpoint, socketID: socketID, channel: channel)
522
- sendAuthorisationRequest ( request: request, channel: channel, callback: callback)
523
- return true
521
+ handleAuthorizationError ( forChannel: channel. name, response: nil , data: nil , error: error)
524
522
525
- case . authRequestBuilder( authRequestBuilder: let builder) :
526
- if let request = builder. requestFor ? ( socketID: socketID, channel: channel) {
527
- sendAuthorisationRequest ( request: request as URLRequest , channel: channel, callback: callback)
523
+ return false
524
+ case . endpoint( authEndpoint: let authEndpoint) :
525
+ let request = requestForAuthValue ( from: authEndpoint, socketId: socketId, channelName: channel. name)
526
+ sendAuthorisationRequest ( request: request, channel: channel, callback: callback)
527
+ return true
528
528
529
- return true
530
- } else if let request = builder. requestFor ? ( socketID: socketID , channelName : channel. name ) {
531
- sendAuthorisationRequest ( request: request, channel: channel, callback: callback)
529
+ case . authRequestBuilder ( authRequestBuilder : let builder ) :
530
+ if let request = builder. requestFor ? ( socketID: socketId , channel : channel) {
531
+ sendAuthorisationRequest ( request: request as URLRequest , channel: channel, callback: callback)
532
532
533
- return true
534
- } else {
535
- let errorMessage = " Authentication request could not be built "
536
- let error = NSError ( domain: " com.pusher.PusherSwift " , code: 0 , userInfo: [ NSLocalizedFailureReasonErrorKey: errorMessage] )
533
+ return true
534
+ } else if let request = builder. requestFor ? ( socketID: socketId, channelName: channel. name) {
535
+ sendAuthorisationRequest ( request: request, channel: channel, callback: callback)
537
536
538
- handleAuthorizationError ( forChannel: channel. name, response: nil , data: nil , error: error)
537
+ return true
538
+ } else {
539
+ let errorMessage = " Authentication request could not be built "
540
+ let error = NSError ( domain: " com.pusher.PusherSwift " , code: 0 , userInfo: [ NSLocalizedFailureReasonErrorKey: errorMessage] )
539
541
540
- return false
541
- }
542
- case . inline( secret: let secret) :
543
- var msg = " "
544
- var channelData = " "
545
- if channel. type == . presence {
546
- channelData = getUserDataJSON ( )
547
- msg = " \( self . socketId!) : \( channel. name) : \( channelData) "
548
- } else {
549
- msg = " \( self . socketId!) : \( channel. name) "
550
- }
542
+ handleAuthorizationError ( forChannel: channel. name, response: nil , data: nil , error: error)
551
543
552
- let secretBuff : [ UInt8 ] = Array ( secret. utf8)
553
- let msgBuff : [ UInt8 ] = Array ( msg. utf8)
544
+ return false
545
+ }
546
+ case . inline( secret: let secret) :
547
+ var msg = " "
548
+ var channelData = " "
549
+ if channel. type == . presence {
550
+ channelData = getUserDataJSON ( )
551
+ msg = " \( self . socketId!) : \( channel. name) : \( channelData) "
552
+ } else {
553
+ msg = " \( self . socketId!) : \( channel. name) "
554
+ }
554
555
555
- if let hmac = try ? HMAC ( key: secretBuff, variant: . sha256) . authenticate ( msgBuff) {
556
- let signature = Data ( bytes: hmac) . toHexString ( )
557
- let auth = " \( self . key) : \( signature) " . lowercased ( )
556
+ let secretBuff : [ UInt8 ] = Array ( secret. utf8)
557
+ let msgBuff : [ UInt8 ] = Array ( msg. utf8)
558
558
559
- if channel. type == . private {
560
- self . handlePrivateChannelAuth ( authValue: auth, channel: channel, callback: callback)
561
- } else {
562
- self . handlePresenceChannelAuth ( authValue: auth, channel: channel, channelData: channelData, callback: callback)
563
- }
564
- }
559
+ if let hmac = try ? HMAC ( key: secretBuff, variant: . sha256) . authenticate ( msgBuff) {
560
+ let signature = Data ( bytes: hmac) . toHexString ( )
561
+ let auth = " \( self . key) : \( signature) " . lowercased ( )
565
562
566
- return true
563
+ if channel. type == . private {
564
+ self . handlePrivateChannelAuth ( authValue: auth, channel: channel, callback: callback)
565
+ } else {
566
+ self . handlePresenceChannelAuth ( authValue: auth, channel: channel, channelData: channelData, callback: callback)
567
+ }
567
568
}
568
- } else {
569
- print ( " socketId value not found. You may not be connected. " )
570
- return false
569
+
570
+ return true
571
571
}
572
572
}
573
573
}
@@ -614,18 +614,18 @@ open class PusherConnection: NSObject {
614
614
Creates an authentication request for the given authEndpoint
615
615
616
616
- parameter endpoint: The authEndpoint to which the request will be made
617
- - parameter socketID : The socketId of the connection's websocket
617
+ - parameter socketId : The socketId of the connection's websocket
618
618
- parameter channel: The PusherChannel to authenticate subsciption for
619
619
620
620
- returns: URLRequest object to be used by the function making the auth request
621
621
*/
622
- fileprivate func requestForAuthValue( from endpoint: String , socketID : String , channel : PusherChannel ) -> URLRequest {
622
+ fileprivate func requestForAuthValue( from endpoint: String , socketId : String , channelName : String ) -> URLRequest {
623
623
let allowedCharacterSet = CharacterSet ( charactersIn: " !*'();:@&=+$,/?%#[] " ) . inverted
624
- let encodedChannelName = channel . name . addingPercentEncoding ( withAllowedCharacters: allowedCharacterSet)
624
+ let encodedChannelName = channelName . addingPercentEncoding ( withAllowedCharacters: allowedCharacterSet) ?? channelName
625
625
626
626
var request = URLRequest ( url: URL ( string: endpoint) !)
627
627
request. httpMethod = " POST "
628
- request. httpBody = " socket_id= \( socketID ) &channel_name= \( encodedChannelName! ) " . data ( using: String . Encoding. utf8)
628
+ request. httpBody = " socket_id= \( socketId ) &channel_name= \( encodedChannelName) " . data ( using: String . Encoding. utf8)
629
629
630
630
return request
631
631
}
0 commit comments