@@ -476,7 +476,7 @@ class ConnectionHandler: ChannelInboundHandler {
476476 if self . auth? . nkey != nil && self . auth? . nkeyPath != nil {
477477 throw NatsError . ConnectError. invalidConfig ( " cannot use both nkey and nkeyPath " )
478478 }
479- if let auth = self . auth, let credentialsPath = auth. credentialsPath {
479+ if let auth = self . auth, let credentialsPath = auth. credentialsPath, let nonce = self . serverInfo ? . nonce {
480480 let credentials = try await URLSession . shared. data ( from: credentialsPath) . 0
481481 guard let jwt = JwtUtils . parseDecoratedJWT ( contents: credentials) else {
482482 throw NatsError . ConnectError. invalidConfig (
@@ -486,17 +486,15 @@ class ConnectionHandler: ChannelInboundHandler {
486486 throw NatsError . ConnectError. invalidConfig (
487487 " failed to extract NKEY from credentials file " )
488488 }
489- guard let nonce = self . serverInfo? . nonce else {
490- throw NatsError . ConnectError. invalidConfig ( " missing nonce " )
491- }
489+ initialConnect. userJwt = String ( data: jwt, encoding: . utf8) !
490+
492491 let keypair = try KeyPair ( seed: String ( data: nkey, encoding: . utf8) !)
493492 let nonceData = nonce. data ( using: . utf8) !
494493 let sig = try keypair. sign ( input: nonceData)
495494 let base64sig = sig. base64EncodedURLSafeNotPadded ( )
496495 initialConnect. signature = base64sig
497- initialConnect. userJwt = String ( data: jwt, encoding: . utf8) !
498496 }
499- if let nkey = self . auth? . nkeyPath {
497+ if let nkey = self . auth? . nkeyPath, let nonce = self . serverInfo ? . nonce {
500498 let nkeyData = try await URLSession . shared. data ( from: nkey) . 0
501499
502500 guard let nkeyContent = String ( data: nkeyData, encoding: . utf8) else {
@@ -505,20 +503,14 @@ class ConnectionHandler: ChannelInboundHandler {
505503 let keypair = try KeyPair (
506504 seed: nkeyContent. trimmingCharacters ( in: . whitespacesAndNewlines)
507505 )
508-
509- guard let nonce = self . serverInfo? . nonce else {
510- throw NatsError . ConnectError. invalidConfig ( " missing nonce " )
511- }
506+ initialConnect. nkey = keypair. publicKeyEncoded
507+
512508 let sig = try keypair. sign ( input: nonce. data ( using: . utf8) !)
513509 let base64sig = sig. base64EncodedURLSafeNotPadded ( )
514510 initialConnect. signature = base64sig
515- initialConnect. nkey = keypair. publicKeyEncoded
516511 }
517- if let nkey = self . auth? . nkey {
512+ if let nkey = self . auth? . nkey, let nonce = self . serverInfo ? . nonce {
518513 let keypair = try KeyPair ( seed: nkey)
519- guard let nonce = self . serverInfo? . nonce else {
520- throw NatsError . ConnectError. invalidConfig ( " missing nonce " )
521- }
522514 let nonceData = nonce. data ( using: . utf8) !
523515 let sig = try keypair. sign ( input: nonceData)
524516 let base64sig = sig. base64EncodedURLSafeNotPadded ( )
0 commit comments